import {z} from 'zod' export type GetApiWhatsappWebhooksWebhookIdOptions = {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 }; /** * Get project webhook * * GET /whatsapp/webhooks/{webhook_id} */ export const getApiWhatsappWebhooksWebhookId = async (options?: GetApiWhatsappWebhooksWebhookIdOptions) => { const {baseUrl = "https://api.kapso.ai/platform/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) 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()) };