import {z} from 'zod' export type RegisterPublicWebhookDtoV2 = {url: string, method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD', headers?: {[key: string]: unknown} | undefined, body?: {[key: string]: unknown} | undefined, query?: {[key: string]: unknown} | undefined, bodyFormat?: ('JSON' | 'FORM_DATA') | undefined, authorization?: ({type: 'BASIC' | 'BEARER', payload: string}) | undefined, webhookEvents: Array<'MESSAGE_RECEIVED' | 'MESSAGE_STATUS_UPDATE' | 'CHAT_ASSIGNED_TO_AGENT' | 'ASSIGNED_AGENT_STATUS_CHANGED' | 'CHAT_UNASSIGNED' | 'UPDATE_CUSTOMER_CUSTOM_FIELD' | 'WIDGET_LEAD_RECEIVED' | 'VERIFIED_WIDGET_LEAD_RECEIVED' | 'NEW_LEAD' | 'RAW_CLOUD_API_WEBHOOK' | 'CLOSE_CONVERSATION' | 'TEMPLATE_UPDATE' | 'ADD_TAG' | 'REMOVE_TAG' | 'CALL_TO_WHATSAPP_MESSAGE_RECEIVED' | 'CONVERSATION_OPENED' | 'CUSTOMER_BUSINESS_CHAT_OPEN' | 'CUSTOMER_NOTE_CREATED' | 'CUSTOMER_NOTE_UPDATED' | 'CUSTOMER_NOTE_DELETED'>, retryOnTimeout?: boolean | undefined, name: string, wabaNumbers: Array}; export type CreateApiV2WebhookWebhookIdArgs = {webhookId: string, body: RegisterPublicWebhookDtoV2}; export type CreateApiV2WebhookWebhookIdOptions = {baseUrl?: string | undefined, basic?: string | undefined, fetch?: typeof fetch | undefined}; export const publicWebhook = z.object({wabaNumber: z.string(), webhookEventType: z.enum(["MESSAGE_RECEIVED", "MESSAGE_STATUS_UPDATE", "CHAT_ASSIGNED_TO_AGENT", "ASSIGNED_AGENT_STATUS_CHANGED", "CHAT_UNASSIGNED", "UPDATE_CUSTOMER_CUSTOM_FIELD", "WIDGET_LEAD_RECEIVED", "VERIFIED_WIDGET_LEAD_RECEIVED", "NEW_LEAD", "RAW_CLOUD_API_WEBHOOK", "CLOSE_CONVERSATION", "TEMPLATE_UPDATE", "ADD_TAG", "REMOVE_TAG", "CALL_TO_WHATSAPP_MESSAGE_RECEIVED", "CONVERSATION_OPENED", "CUSTOMER_BUSINESS_CHAT_OPEN"])}); export const registerPublicWebhookResponseV2 = z.object({validWebhooks: z.array(publicWebhook), invalidWebhooks: z.array(publicWebhook), invalidWabaNumbers: z.array(z.string())}); 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 }; /** * Edit Webhooks * * POST /v2/webhook/{webhookId} */ export const createApiV2WebhookWebhookId = async (args: CreateApiV2WebhookWebhookIdArgs, options?: CreateApiV2WebhookWebhookIdOptions) => { const {baseUrl = "https://public.doubletick.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set("Authorization", basic) const url = `${baseUrl.replace(/\/+$/, '')}/v2/webhook/${encodeURIComponent(String(args.webhookId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(registerPublicWebhookResponseV2, await res.json()) };