import {z} from 'zod' export type GetApiApiV1AppAppIdPollerSinkIdArgs = {app_id: string, sink_id: string, limit?: number | undefined, iterator?: (string | null) | undefined, event_type?: (string | null) | undefined, channel?: (string | null) | undefined, after?: (string | null) | undefined}; export type GetApiApiV1AppAppIdPollerSinkIdOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pollingEndpointMessageOut = z.object({channels: z.array(z.string().max(128)).nullable().optional(), deliverAt: z.string().nullable().optional(), eventId: z.string().min(1).max(256).nullable().optional(), eventType: z.string().max(256), headers: z.record(z.string(), z.string()).nullable().optional(), id: z.string().min(31).max(31), payload: z.object({}), tags: z.array(z.string().max(128)).nullable().optional(), timestamp: z.string()}); export const pollingEndpointOut = z.object({data: z.array(pollingEndpointMessageOut), done: z.boolean(), iterator: z.string()}); 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 }; /** * Poller Poll * * GET /api/v1/app/{app_id}/poller/{sink_id} */ export const getApiApiV1AppAppIdPollerSinkId = async (args: GetApiApiV1AppAppIdPollerSinkIdArgs, options?: GetApiApiV1AppAppIdPollerSinkIdOptions) => { const {baseUrl = "https://api.eu.svix.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.iterator != null) query.append("iterator", String(args.iterator)) if (args.event_type != null) query.append("event_type", String(args.event_type)) if (args.channel != null) query.append("channel", String(args.channel)) if (args.after != null) query.append("after", String(args.after)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/app/${encodeURIComponent(String(args.app_id))}/poller/${encodeURIComponent(String(args.sink_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(pollingEndpointOut, await res.json()) };