import {z} from 'zod' export type GetApiWebhookV1ProfileArgs = {fields?: string | undefined, 'If-None-Match'?: string | undefined}; export type GetApiWebhookV1ProfileOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiWebhookV1ProfileResponse = z.object({id: z.string().optional(), rev: z.string().min(1).optional(), classification: z.literal("webhook-profile").optional(), creatorId: z.string().min(1).optional(), created: z.string().min(1).optional(), lastModifierId: z.string().min(1).optional(), lastModified: z.string().min(1).optional(), webhooks: z.array(z.object({url: z.string(), secret: z.string().optional(), secretId: z.string().optional(), active: z.boolean().optional(), filter: z.object({eventType: z.array(z.enum(["create", "update", "delete"])).optional(), classification: z.array(z.enum(["asset", "category", "comment", "content", "content-type", "default-content", "image-profile", "layout", "layout-mapping", "project", "review", "taxonomy"])).optional(), status: z.array(z.enum(["draft", "ready", "retired"])).optional(), tags: z.array(z.string()).optional()}).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 }; /** * Retrieves the webhook profile for the current tenant * * GET /webhook/v1/profile */ export const getApiWebhookV1Profile = async (args: GetApiWebhookV1ProfileArgs, options: GetApiWebhookV1ProfileOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fields != null) query.append("fields", String(args.fields)) if (args['If-None-Match'] != null) headers.set("If-None-Match", String(args['If-None-Match'])) const url = `${baseUrl.replace(/\/+$/, '')}/webhook/v1/profile${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(getApiWebhookV1ProfileResponse, await res.json()) };