import {z} from 'zod' export type WebhookEventTriggerEnum = 'recipient_completed' | 'document_updated' | 'document_deleted' | 'document_state_changed' | 'document_creation_failed' | 'document_completed_pdf_ready' | 'document_section_added' | 'quote_updated' | 'template_created' | 'template_updated' | 'template_deleted' | 'content_library_item_created' | 'content_library_item_creation_failed' | 'recipient_qes_verified'; export type WebhookEventHttpStatusCodeGroupEnum = 100 | 200 | 300 | 400 | 500; export type WebhookEventErrorEnum = 'INTERNAL_ERROR' | 'NOT_VALID_URL' | 'CONNECT_ERROR' | 'TIMEOUT_ERROR' | null; export type GetApiPublicV1WebhookEventsArgs = {count: number, page: number, since?: string | undefined, to?: string | undefined, type?: Array | undefined, http_status_code?: Array | undefined, error?: Array | undefined}; export type GetApiPublicV1WebhookEventsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookEventTriggerEnum = z.enum(["recipient_completed", "document_updated", "document_deleted", "document_state_changed", "document_creation_failed", "document_completed_pdf_ready", "document_section_added", "quote_updated", "template_created", "template_updated", "template_deleted", "content_library_item_created", "content_library_item_creation_failed", "recipient_qes_verified"]); export const webhookEventErrorEnum = z.enum(["INTERNAL_ERROR", "NOT_VALID_URL", "CONNECT_ERROR", "TIMEOUT_ERROR"]).nullable(); export const webhookEventItemResponse = z.object({uuid: z.string().nullable().optional(), name: z.string().optional(), type: webhookEventTriggerEnum.optional(), http_status_code: z.number().int().nullable().optional(), error: webhookEventErrorEnum.optional(), delivery_time: z.string().optional()}); export const webhookEventPageResponse = z.object({items: z.array(webhookEventItemResponse).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 Events * * GET /public/v1/webhook-events * * Also accepts auth: oauth2 */ export const getApiPublicV1WebhookEvents = async (args: GetApiPublicV1WebhookEventsArgs, options?: GetApiPublicV1WebhookEventsOptions) => { const {baseUrl = "https://api.pandadoc.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.count != null) query.append("count", String(args.count)) if (args.page != null) query.append("page", String(args.page)) if (args.since != null) query.append("since", String(args.since)) if (args.to != null) query.append("to", String(args.to)) for (const item of args.type ?? []) query.append("type", String(item)) for (const item of args.http_status_code ?? []) query.append("http_status_code", String(item)) for (const item of args.error ?? []) query.append("error", String(item)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/webhook-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(webhookEventPageResponse, await res.json()) };