import {z} from 'zod' export type GetApiApiV1WebhooksDeliveriesArgs = {'page[size]'?: number | undefined, 'page[after]'?: string | undefined, 'page[before]'?: string | undefined, 'filter[endpoint]'?: string | undefined, 'filter[status]'?: string | undefined, 'filter[period]'?: string | undefined, 'filter[from]'?: string | undefined, 'filter[to]'?: string | undefined}; export type GetApiApiV1WebhooksDeliveriesOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookDeliveryStatus = z.enum(["pending", "processing", "succeeded", "failed", "filtered", "unmatched"]); export const webhookDeliveryListItem = z.object({id: z.string(), endpoint: z.object({id: z.string(), declaredId: z.string()}).nullable(), status: webhookDeliveryStatus, externalDeliveryId: z.string(), isTest: z.boolean(), createdAt: z.string(), processedAt: z.string().nullable()}); export const listWebhookDeliveriesResult = z.object({data: z.array(webhookDeliveryListItem), pagination: z.object({next: z.string().optional(), previous: z.string().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 deliveries * * GET /api/v1/webhooks/deliveries */ export const getApiApiV1WebhooksDeliveries = async (args: GetApiApiV1WebhooksDeliveriesArgs, options?: GetApiApiV1WebhooksDeliveriesOptions) => { const {baseUrl = "https://api.trigger.dev", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['filter[endpoint]'] != null) query.append("filter[endpoint]", String(args['filter[endpoint]'])) if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args['filter[period]'] != null) query.append("filter[period]", String(args['filter[period]'])) if (args['filter[from]'] != null) query.append("filter[from]", String(args['filter[from]'])) if (args['filter[to]'] != null) query.append("filter[to]", String(args['filter[to]'])) if (secretKey !== undefined) headers.set('authorization', `Bearer ${secretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/webhooks/deliveries${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(listWebhookDeliveriesResult, await res.json()) };