import {z} from 'zod' export type EtcdserverpbWatchCancelRequest = {watch_id?: unknown | undefined}; export type WatchCreateRequestFilterType = 'NOPUT' | 'NODELETE'; export type EtcdserverpbWatchCreateRequest = {filters?: Array | undefined, fragment?: boolean | undefined, key?: string | undefined, prev_kv?: boolean | undefined, progress_notify?: boolean | undefined, range_end?: string | undefined, start_revision?: unknown | undefined, watch_id?: unknown | undefined}; export type EtcdserverpbWatchProgressRequest = Record; export type EtcdserverpbWatchRequest = {cancel_request?: EtcdserverpbWatchCancelRequest | undefined, create_request?: EtcdserverpbWatchCreateRequest | undefined, progress_request?: EtcdserverpbWatchProgressRequest | undefined}; export type CreateApiV3WatchArgs = {body: EtcdserverpbWatchRequest}; export type CreateApiV3WatchOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const protobufAny = z.object({type_url: z.string().optional(), value: z.string().optional()}); export const runtimeStreamError = z.object({details: z.array(protobufAny).optional(), grpc_code: z.number().int().optional(), http_code: z.number().int().optional(), http_status: z.string().optional(), message: 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 eventEventType = z.enum(["PUT", "DELETE"]); export const mvccpbEvent = z.object({kv: mvccpbKeyValue.optional(), prev_kv: mvccpbKeyValue.optional(), type: eventEventType.optional()}); 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 etcdserverpbWatchResponse = z.object({cancel_reason: z.string().optional(), canceled: z.boolean().optional(), compact_revision: z.string().optional(), created: z.boolean().optional(), events: z.array(mvccpbEvent).optional(), fragment: z.boolean().optional(), header: etcdserverpbResponseHeader.optional(), watch_id: z.string().optional()}); export const createApiV3WatchResponse = z.object({error: runtimeStreamError.optional(), result: etcdserverpbWatchResponse.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 }; /** * Watch watches for events happening or that have happened. Both input and output * are streams; the input stream is for creating and canceling watchers and the output * stream sends events. One watch RPC can watch on multiple key ranges, streaming events * for several watches at once. The entire event history can be watched starting from the * last compaction revision. * * POST /v3/watch */ export const createApiV3Watch = async (args: CreateApiV3WatchArgs, options: CreateApiV3WatchOptions) => { 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/watch${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(createApiV3WatchResponse, await res.json()) };