import {z} from 'zod' export type RangeRequestSortOrder = 'NONE' | 'ASCEND' | 'DESCEND'; export type RangeRequestSortTarget = 'KEY' | 'VERSION' | 'CREATE' | 'MOD' | 'VALUE'; export type EtcdserverpbRangeRequest = {count_only?: boolean | undefined, key?: string | undefined, keys_only?: boolean | undefined, limit?: unknown | undefined, max_create_revision?: unknown | undefined, max_mod_revision?: unknown | undefined, min_create_revision?: unknown | undefined, min_mod_revision?: unknown | undefined, range_end?: string | undefined, revision?: unknown | undefined, serializable?: boolean | undefined, sort_order?: RangeRequestSortOrder | undefined, sort_target?: RangeRequestSortTarget | undefined}; export type CreateApiV3KvRangeArgs = {body: EtcdserverpbRangeRequest}; export type CreateApiV3KvRangeOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const etcdserverpbResponseHeader = z.object({cluster_id: z.string().optional(), member_id: z.string().optional(), raft_term: z.string().optional(), revision: z.string().optional()}); export const mvccpbKeyValue = z.object({create_revision: z.string().optional(), key: z.string().optional(), lease: z.string().optional(), mod_revision: z.string().optional(), value: z.string().optional(), version: z.string().optional()}); export const etcdserverpbRangeResponse = z.object({count: z.string().optional(), header: etcdserverpbResponseHeader.optional(), kvs: z.array(mvccpbKeyValue).optional(), more: z.boolean().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Range gets the keys in the range from the key-value store. * * POST /v3/kv/range */ export const createApiV3KvRange = async (args: CreateApiV3KvRangeArgs, options: CreateApiV3KvRangeOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v3/kv/range${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(etcdserverpbRangeResponse, await res.json()) };