import {z} from 'zod' export type GetApiApiPublicAnalyticsContactsByStatusArgs = {page: string, orderBy?: ('contact_created_at' | 'ticket_created_at' | 'last_inbound_message' | 'last_outbound_message' | 'id') | undefined, channel: 'whatsapp' | 'playground' | 'instagram' | 'webchat' | 'web-whatsapp' | 'messenger' | 'sms', pipelineId: string, status: 'unattended' | 'attended' | 'ai-attended' | 'ai-away', 'x-api-key': string}; export type GetApiApiPublicAnalyticsContactsByStatusOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPublicAnalyticsContactsByStatusResponse = z.array(z.object({id: z.string().optional(), name: z.string().optional(), created_at: z.string().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 }; /** * Get contacts by status * * GET /api/public/analytics/contacts/by-status */ export const getApiApiPublicAnalyticsContactsByStatus = async (args: GetApiApiPublicAnalyticsContactsByStatusArgs, options?: GetApiApiPublicAnalyticsContactsByStatusOptions) => { const {baseUrl = "https://api.vambe.me", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.channel != null) query.append("channel", String(args.channel)) if (args.pipelineId != null) query.append("pipelineId", String(args.pipelineId)) if (args.status != null) query.append("status", String(args.status)) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/public/analytics/contacts/by-status${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(getApiApiPublicAnalyticsContactsByStatusResponse, await res.json()) };