import {z} from 'zod' export type WhatsappFlowVersionCreateRequest = {flow_json: {[key: string]: unknown}, phone_number_id?: string | undefined}; export type CreateApiWhatsappFlowsFlowIdVersionsArgs = {body: WhatsappFlowVersionCreateRequest}; export type CreateApiWhatsappFlowsFlowIdVersionsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const whatsappFlowVersionDetail = z.object({id: z.string(), version_label: z.string(), status: z.enum(["draft", "published"]), flow_json_sha: z.string().nullable().optional(), published_at: z.string().nullable().optional(), validation_errors: z.array(z.record(z.string(), z.unknown())).nullable().optional(), created_at: z.string(), updated_at: z.string(), flow_json: z.record(z.string(), z.unknown()).optional()}); export const whatsappFlowVersionDetailResponse = z.object({data: whatsappFlowVersionDetail}); 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 flow version * * POST /whatsapp/flows/{flow_id}/versions */ export const createApiWhatsappFlowsFlowIdVersions = async (args: CreateApiWhatsappFlowsFlowIdVersionsArgs, options?: CreateApiWhatsappFlowsFlowIdVersionsOptions) => { 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/flows/${encodeURIComponent(String(args.flow_id))}/versions${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(whatsappFlowVersionDetailResponse, await res.json()) };