import {z} from 'zod' export type CreateWebhookRequest = {url: string, events: Array<'extraction.completed' | 'extraction.failed' | 'validation.completed' | 'validation.failed' | 'merge.completed' | 'merge.failed' | 'document.uploaded' | 'document.deleted' | 'shipment.created' | 'shipment.updated' | 'source.processing.completed' | 'source.processing.failed' | 'tariff.changed' | 'webhook.test'>, description?: string | undefined}; export type CreateApiApiV1ExternalWebhooksArgs = {body: CreateWebhookRequest}; export type CreateApiApiV1ExternalWebhooksOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1ExternalWebhooksResponse = z.object({success: z.boolean().optional(), data: z.object({id: z.string().optional(), url: z.string().optional(), events: z.array(z.enum(["extraction.completed", "extraction.failed", "validation.completed", "validation.failed", "merge.completed", "merge.failed", "document.uploaded", "document.deleted", "shipment.created", "shipment.updated", "source.processing.completed", "source.processing.failed", "tariff.changed", "webhook.test"])).optional(), description: z.string().optional(), isActive: z.boolean().optional(), createdAt: z.string().optional(), lastTriggeredAt: z.string().optional(), secret: z.string().optional()}).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 }; /** * Create webhook * * POST /api/v1/external/webhooks */ export const createApiApiV1ExternalWebhooks = async (args: CreateApiApiV1ExternalWebhooksArgs, options?: CreateApiApiV1ExternalWebhooksOptions) => { const {baseUrl = "https://api.wove.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/external/webhooks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiApiV1ExternalWebhooksResponse, await res.json()) };