import {z} from 'zod' export type WhatsappWebhookUpdateRequest = {whatsapp_webhook: {url?: string | undefined, kind?: ('kapso' | 'meta') | undefined, secret_key?: (string | null) | undefined, active?: boolean | undefined, buffer_enabled?: boolean | undefined, buffer_window_seconds?: (number | null) | undefined, max_buffer_size?: (number | null) | undefined, inactivity_minutes?: (number | null) | undefined, events?: Array | undefined, buffer_events?: Array | undefined, headers?: ({[key: string]: unknown} | null) | undefined, payload_version?: (string | null) | undefined}}; export type PatchApiWhatsappWebhooksWebhookIdArgs = {body: WhatsappWebhookUpdateRequest}; export type PatchApiWhatsappWebhooksWebhookIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const whatsappWebhook = z.object({id: z.string(), url: z.string(), kind: z.enum(["kapso", "meta"]), events: z.array(z.string()), active: z.boolean(), created_at: z.string(), updated_at: z.string(), project_id: z.string(), phone_number_id: z.string().nullable().optional(), secret_key: z.string().nullable().optional(), headers: z.record(z.string(), z.unknown()).nullable().optional(), buffer_enabled: z.boolean().nullable().optional(), buffer_window_seconds: z.number().int().nullable().optional(), max_buffer_size: z.number().int().nullable().optional(), buffer_events: z.array(z.string()).optional(), inactivity_minutes: z.number().int().nullable().optional(), payload_version: z.string().nullable().optional()}); export const whatsappWebhookResponse = z.object({data: whatsappWebhook}); 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 project webhook * * PATCH /whatsapp/webhooks/{webhook_id} */ export const patchApiWhatsappWebhooksWebhookId = async (args: PatchApiWhatsappWebhooksWebhookIdArgs, options?: PatchApiWhatsappWebhooksWebhookIdOptions) => { const {baseUrl = "https://api.kapso.ai/platform/v1", 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 (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/whatsapp/webhooks/${encodeURIComponent(String(args.webhook_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(whatsappWebhookResponse, await res.json()) };