import {z} from 'zod' export type GetApiWebhooksArgs = {page?: number | undefined, page_size?: number | undefined, enabled?: ('0' | '1') | undefined, url?: string | undefined, event?: string | undefined, created_after?: string | undefined, created_before?: string | undefined, sort_by?: ('url' | 'enabled' | 'created_on' | 'last_changed_on' | 'consecutive_failures') | undefined, sort_order?: ('asc' | 'desc') | undefined, workspace_id?: string | undefined}; export type GetApiWebhooksOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhook = z.object({id: z.string(), url: z.string(), description: z.string().nullable().optional(), events: z.array(z.string()), enabled: z.boolean(), consecutive_failures: z.number().int().optional(), auto_disabled_at: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string().optional()}); export const pagination = z.object({current_page: z.number().int(), page_size: z.number().int(), total_count: z.number().int(), total_pages: z.number().int()}); export const webhookListResponse = z.object({results: z.array(webhook), pagination: pagination}); 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 Webhooks * * GET /webhooks */ export const getApiWebhooks = async (args: GetApiWebhooksArgs, options?: GetApiWebhooksOptions) => { const {baseUrl = "https://api.firma.dev/functions/v1/signing-request-api", fetch: fetchFn = fetch, ApiKeyAuth} = 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.enabled != null) query.append("enabled", String(args.enabled)) if (args.url != null) query.append("url", String(args.url)) if (args.event != null) query.append("event", String(args.event)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.workspace_id != null) query.append("workspace_id", String(args.workspace_id)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/webhooks${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(webhookListResponse, await res.json()) };