import {z} from 'zod' export type CreateApiWebhookArgs = {Accept: 'application/vnd.Creative Force.v2.3+json' | 'application/vnd.Creative Force.v2.3+xml', body: {method: 'get' | 'post', name: string, url: string, signing_key?: string | undefined, notification_emails?: (string | Array) | undefined, form?: (string | null) | undefined, fields?: Array | undefined, events: Array<'allocation_created' | 'allocation_payment_created' | 'allocation_payment_deleted' | 'allocation_payment_status_changed' | 'allocation_payment_updated' | 'assignment_completed' | 'assignment_created' | 'collaborator_invited' | 'document_created' | 'document_deleted' | 'document_updated' | 'entry_category_changed' | 'entry_chapter_changed' | 'entry_copied' | 'entry_created' | 'entry_deleted' | 'entry_eligible' | 'entry_ineligible' | 'entry_moderated' | 'entry_resubmitted' | 'entry_submitted' | 'entry_tagged' | 'entry_updated' | 'field_value_updated' | 'membership_registered' | 'payment_pending' | 'payment_success' | 'review_task_created' | 'review_task_submitted' | 'role_granted' | 'user_updated'>}}; export type CreateApiWebhookOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhook = z.object({events: z.array(z.string()).optional(), fields: z.array(z.object({slug: z.string().optional(), link: z.string().optional(), title: z.record(z.string(), z.string()).optional()})).optional(), form: z.string().nullable().optional(), method: z.enum(["get", "post"]).optional(), name: z.string().optional(), slug: z.string().optional(), signing_key: z.string().optional(), url: z.string().optional(), created: z.string().optional(), updated: z.string().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 a webhook * * POST /webhook */ export const createApiWebhook = async (args: CreateApiWebhookArgs, options?: CreateApiWebhookOptions) => { const {baseUrl = "https://api.us.cr4ce.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/webhook${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(webhook, await res.json()) };