import {z} from 'zod' export type GetApiWhatsappBroadcastsArgs = {phone_number_id?: string | undefined, status?: ('draft' | 'sending' | 'completed' | 'failed') | undefined, created_after?: string | undefined, created_before?: string | undefined, per_page?: number | undefined, page?: number | undefined}; export type GetApiWhatsappBroadcastsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const whatsappBroadcastTemplate = z.object({id: z.string(), meta_template_id: z.string().nullable().optional(), name: z.string(), language_code: z.string(), category: z.string(), status: z.string(), components: z.array(z.record(z.string(), z.unknown())).optional()}); export const whatsappBroadcast = z.object({id: z.string(), name: z.string(), status: z.enum(["draft", "scheduled", "sending", "stopped", "completed", "failed"]), scheduled_at: z.string().nullable().optional(), started_at: z.string().nullable().optional(), stopped_at: z.string().nullable().optional(), completed_at: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string(), sent_count: z.number().int().gte(0).optional(), failed_count: z.number().int().gte(0).optional(), delivered_count: z.number().int().gte(0).optional(), read_count: z.number().int().gte(0).optional(), pending_count: z.number().int().gte(0).optional(), responded_count: z.number().int().gte(0).optional(), response_rate: z.number().optional(), total_recipients: z.number().int().gte(0), phone_number_id: z.string().nullable().optional(), whatsapp_template: whatsappBroadcastTemplate.nullable().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 whatsappBroadcastListResponse = z.object({data: z.array(whatsappBroadcast), meta: paginationMeta}); 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 broadcasts * * GET /whatsapp/broadcasts */ export const getApiWhatsappBroadcasts = async (args: GetApiWhatsappBroadcastsArgs, options?: GetApiWhatsappBroadcastsOptions) => { 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.phone_number_id != null) query.append("phone_number_id", String(args.phone_number_id)) if (args.status != null) query.append("status", String(args.status)) 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.per_page != null) query.append("per_page", String(args.per_page)) if (args.page != null) query.append("page", String(args.page)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/whatsapp/broadcasts${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(whatsappBroadcastListResponse, await res.json()) };