import {z} from 'zod' export type CreateApiWhatsappContactsArgs = {body: {contact: {wa_id: string, profile_name?: string | undefined, display_name?: string | undefined, customer_id?: string | undefined, metadata?: {[key: string]: unknown} | undefined}}}; export type CreateApiWhatsappContactsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const whatsappContact = z.object({id: z.string().optional(), wa_id: z.string().nullable().optional(), business_scoped_user_id: z.string().nullable().optional(), parent_business_scoped_user_id: z.string().nullable().optional(), username: z.string().nullable().optional(), profile_name: z.string().optional(), display_name: z.string().nullable().optional(), customer_id: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const whatsappContactResponse = z.object({data: whatsappContact}); 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 }; /** * Create contact * * POST /whatsapp/contacts */ export const createApiWhatsappContacts = async (args: CreateApiWhatsappContactsArgs, options?: CreateApiWhatsappContactsOptions) => { 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/contacts${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(whatsappContactResponse, await res.json()) };