import {z} from 'zod' export type GetApiWhatsappContactsArgs = {customer_id?: string | undefined, customer_external_id?: string | undefined, has_customer?: boolean | undefined, profile_name_contains?: string | undefined, wa_id_contains?: string | undefined, business_scoped_user_id?: string | undefined, created_after?: string | undefined, created_before?: string | undefined, page?: number | undefined, per_page?: number | undefined, limit?: number | undefined, before?: string | undefined, after?: string | undefined}; export type GetApiWhatsappContactsOptions = {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 paginationMeta = z.object({page: z.number().int().gte(1), per_page: z.number().int().gte(1), total_pages: z.number().int().gte(0), total_count: z.number().int().gte(0)}); export const paginationCursor = z.object({before: z.string().optional(), after: z.string().optional()}); export const paging = z.object({cursors: paginationCursor.optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional()}); export const whatsappContactListResponse = z.object({data: z.array(whatsappContact), meta: paginationMeta.optional(), paging: paging.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 }; /** * List contacts * * GET /whatsapp/contacts */ export const getApiWhatsappContacts = async (args: GetApiWhatsappContactsArgs, options?: GetApiWhatsappContactsOptions) => { 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 (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.customer_external_id != null) query.append("customer_external_id", String(args.customer_external_id)) if (args.has_customer != null) query.append("has_customer", String(args.has_customer)) if (args.profile_name_contains != null) query.append("profile_name_contains", String(args.profile_name_contains)) if (args.wa_id_contains != null) query.append("wa_id_contains", String(args.wa_id_contains)) if (args.business_scoped_user_id != null) query.append("business_scoped_user_id", String(args.business_scoped_user_id)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(whatsappContactListResponse, await res.json()) };