import {z} from 'zod' export type WebhookConfigurationMethod = 'POST'; export type WebhookConfigurationTrigger = 'document:publish' | 'document:unpublish' | 'page:publish' | 'page:unpublish'; export type WebhookConfigurationHeader = {name: string, value: boolean, secret?: boolean | undefined}; export type System = {createdBy?: string | undefined, createdAt?: string | undefined, updatedBy?: string | undefined, updatedAt?: string | undefined}; export type WebhookConfiguration = {id?: string | undefined, name: string, enabled?: boolean | undefined, url: string, method: WebhookConfigurationMethod, triggers: Array, headers?: Array | undefined, system?: System | undefined}; export type UpdateApiConfigurationsWebhookConfigurationIdArgs = {'X-Resource-Version'?: string | undefined, webhook_configuration_id: string, body: WebhookConfiguration}; export type UpdateApiConfigurationsWebhookConfigurationIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookConfigurationMethod = z.literal("POST"); export const webhookConfigurationTrigger = z.enum(["document:publish", "document:unpublish", "page:publish", "page:unpublish"]); export const webhookConfigurationHeader = z.object({name: z.string(), value: z.boolean(), secret: z.boolean().optional()}); export const system = z.object({createdBy: z.string().optional(), createdAt: z.string().optional(), updatedBy: z.string().optional(), updatedAt: z.string().optional()}); export const webhookConfiguration = z.object({id: z.string().optional(), name: z.string(), enabled: z.boolean().optional(), url: z.string(), method: webhookConfigurationMethod, triggers: z.array(webhookConfigurationTrigger), headers: z.array(webhookConfigurationHeader).optional(), system: system.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 }; /** * Update a webhook configuration * * PUT /configurations/{webhook_configuration_id} */ export const updateApiConfigurationsWebhookConfigurationId = async (args: UpdateApiConfigurationsWebhookConfigurationIdArgs, options?: UpdateApiConfigurationsWebhookConfigurationIdOptions) => { const {baseUrl = "https://developers.bloomreach.io/delivery/site/v2/documents", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Resource-Version'] != null) headers.set("X-Resource-Version", String(args['X-Resource-Version'])) const url = `${baseUrl.replace(/\/+$/, '')}/configurations/${encodeURIComponent(String(args.webhook_configuration_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(webhookConfiguration, await res.json()) };