import {z} from 'zod' export type GetApiWhatsappFlowsArgs = {status?: ('draft' | 'published') | undefined, business_account_id?: string | undefined, phone_number_id?: string | undefined, name_contains?: string | undefined, created_after?: string | undefined, created_before?: string | undefined, per_page?: number | undefined, page?: number | undefined}; export type GetApiWhatsappFlowsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const whatsappFlow = z.object({id: z.string(), meta_flow_id: z.string(), name: z.string(), status: z.enum(["draft", "published"]), json_version: z.string().nullable().optional(), data_api_version: z.string().nullable().optional(), business_account_id: z.string().nullable().optional(), preview_url: z.string().nullable().optional(), published_at: z.string().nullable().optional(), last_synced_at: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string(), data_endpoint_function_id: z.string().nullable().optional(), phone_number_id: z.string().nullable().optional(), data_endpoint_url: z.string().nullable().optional(), has_data_endpoint: z.boolean().optional(), flows_encryption_configured: z.boolean().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 whatsappFlowListResponse = z.object({data: z.array(whatsappFlow), 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 flows * * GET /whatsapp/flows */ export const getApiWhatsappFlows = async (args: GetApiWhatsappFlowsArgs, options?: GetApiWhatsappFlowsOptions) => { 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.status != null) query.append("status", String(args.status)) if (args.business_account_id != null) query.append("business_account_id", String(args.business_account_id)) if (args.phone_number_id != null) query.append("phone_number_id", String(args.phone_number_id)) if (args.name_contains != null) query.append("name_contains", String(args.name_contains)) 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/flows${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(whatsappFlowListResponse, await res.json()) };