import {z} from 'zod' export type GetApiApiV2WebhooksWebhookIdInvocationsArgs = {webhook_id: string, 'filter[from_ts]'?: string | undefined, 'filter[status]'?: ('unknown' | 'accepted' | 'success' | 'failed' | 'timeout' | 'circuit broken' | 'throttled' | 'client error' | 'server error') | undefined, 'filter[to_ts]'?: string | undefined, 'page[before]'?: string | undefined, 'page[after]'?: string | undefined, 'page[size]'?: number | undefined, sort?: ('latest_completed_at' | '-latest_completed_at') | undefined}; export type GetApiApiV2WebhooksWebhookIdInvocationsOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const webhookInvocation = z.object({id: z.string().optional(), webhook_id: z.string().optional(), status: z.enum(["unknown", "accepted", "success", "failed", "timeout", "circuit broken", "throttled", "client error", "server error"]).optional(), created_at: z.string().optional(), latest_completed_at: z.string().optional(), event_type: z.string().optional(), attempts_count: z.number().int().optional()}); export const paginationMeta = z.object({has_more: z.boolean().optional(), after_cursor: z.string().optional(), before_cursor: z.string().optional(), count: z.number().int().optional()}); export const paginationLinks = z.object({prev: z.string().optional(), next: z.string().optional()}); export const webhookInvocationListResponse = z.object({invocations: z.array(webhookInvocation).optional(), meta: paginationMeta.optional(), links: paginationLinks.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 }; /** * List Webhook Invocations * * GET /api/v2/webhooks/{webhook_id}/invocations */ export const getApiApiV2WebhooksWebhookIdInvocations = async (args: GetApiApiV2WebhooksWebhookIdInvocationsArgs, options?: GetApiApiV2WebhooksWebhookIdInvocationsOptions) => { const {baseUrl = "https://example.zendesk.com", fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[from_ts]'] != null) query.append("filter[from_ts]", String(args['filter[from_ts]'])) if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args['filter[to_ts]'] != null) query.append("filter[to_ts]", String(args['filter[to_ts]'])) if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/webhooks/${encodeURIComponent(String(args.webhook_id))}/invocations${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(webhookInvocationListResponse, await res.json()) };