import {z} from 'zod' export type GetApiWebhookArgs = {http_method?: ('get' | 'post') | undefined, subscription_event?: ('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') | undefined, url?: string | undefined, page?: number | undefined, per_page?: number | undefined, order?: ('events' | 'http_method' | 'notification_emails' | 'signing_keys' | 'title' | 'updated' | 'url') | undefined, dir?: ('asc' | 'desc') | undefined, Accept: 'application/vnd.Creative Force.v2.3+json' | 'application/vnd.Creative Force.v2.3+xml'}; export type GetApiWebhookOptions = {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 webhookList = z.object({current_page: z.number().int().optional(), first_page_url: z.string().optional(), from: z.number().int().nullable().optional(), last_page: z.number().int().optional(), last_page_url: z.string().optional(), next_page_url: z.string().optional(), path: z.string().optional(), per_page: z.number().int().gte(1).lte(100).optional(), prev_page_url: z.string().optional(), to: z.number().int().nullable().optional(), total: z.number().int().optional(), data: z.array(webhook).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 }; /** * Retrieve webhooks * * GET /webhook */ export const getApiWebhook = async (args: GetApiWebhookArgs, options?: GetApiWebhookOptions) => { const {baseUrl = "https://api.us.cr4ce.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.http_method != null) query.append("http_method", String(args.http_method)) if (args.subscription_event != null) query.append("subscription_event", String(args.subscription_event)) if (args.url != null) query.append("url", String(args.url)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.order != null) query.append("order", String(args.order)) if (args.dir != null) query.append("dir", String(args.dir)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(webhookList, await res.json()) };