import {z} from 'zod' export type GetApiV1WebhooksEventsIdArgs = {id: string}; export type GetApiV1WebhooksEventsIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhooksWebhookEventType = z.enum(["company", "item", "payable", "payment_method", "receivable", "risk_report", "tax_form", "test"]); export const sharedSelfLink = z.string(); 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 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 }; /** * Retrieve a Webhook Event * * GET /v1/webhooks/events/{id} */ export const getApiV1WebhooksEventsId = async (args: GetApiV1WebhooksEventsIdArgs, options?: GetApiV1WebhooksEventsIdOptions) => { 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/webhooks/events/${encodeURIComponent(String(args.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(webhooksWebhookEvent, await res.json()) };