import {z} from 'zod' export type WebhooksWebhookEventStatus = 'all' | 'failed' | 'successful'; export type WebhooksWebhookEventType = 'company' | 'item' | 'payable' | 'payment_method' | 'receivable' | 'risk_report' | 'tax_form' | 'test'; export type GetApiV1WebhooksEventsArgs = {page?: number | undefined, page_size?: number | undefined, 'created_at.gte'?: string | undefined, 'created_at.lte'?: string | undefined, status?: Array | undefined, type?: WebhooksWebhookEventType | undefined}; export type GetApiV1WebhooksEventsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sharedSelfLink = z.string(); export const sharedListLinks = z.object({self: sharedSelfLink, next: z.string().nullable().optional(), prev: z.string().nullable().optional()}); export const webhooksWebhookEventType = z.enum(["company", "item", "payable", "payment_method", "receivable", "risk_report", "tax_form", "test"]); export const webhooksWebhookEventLinks = z.object({self: sharedSelfLink, resource: z.string().nullable()}); export const webhooksWebhookEvent = z.object({object: z.literal("WebhookEvent"), id: z.string(), created_at: z.string(), detail: z.object({company_id: z.string().optional(), errors: z.object({}).nullable().optional(), external_id: z.string().nullable().optional(), idempotency_key: z.string().nullable().optional(), request_id: z.string().nullable().optional()}).optional(), event_name: z.string(), event_resource: webhooksWebhookEventType, is_paused: z.boolean(), is_successful: z.boolean(), last_attempt_at: z.string().optional(), last_status: z.number().int(), next_attempt_at: z.string().optional(), object_id: z.string(), tries: z.number().int(), links: webhooksWebhookEventLinks}); export const webhooksWebhookEventList = z.object({object: z.literal("List"), links: sharedListLinks, results: z.array(webhooksWebhookEvent)}); 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 Events * * GET /v1/webhooks/events */ export const getApiV1WebhooksEvents = async (args: GetApiV1WebhooksEventsArgs, options?: GetApiV1WebhooksEventsOptions) => { const {baseUrl = "https://api.routable.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args['created_at.gte'] != null) query.append("created_at.gte", String(args['created_at.gte'])) if (args['created_at.lte'] != null) query.append("created_at.lte", String(args['created_at.lte'])) for (const item of args.status ?? []) query.append("status", String(item)) if (args.type != null) query.append("type", String(args.type)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/webhooks/events${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(webhooksWebhookEventList, await res.json()) };