import {z} from 'zod' export type GetApiV1ConversationsArgs = {status?: ('OPEN' | 'CLOSED' | 'ARCHIVED') | undefined, leadStatus?: string | undefined, window?: ('OPEN' | 'EXPIRED') | undefined, page?: number | undefined, size?: number | undefined, 'X-Waba-Id'?: string | undefined}; export type GetApiV1ConversationsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const conversationListItem = z.object({id: z.string().optional(), customerPhone: z.string().optional(), customerName: z.string().nullable().optional(), status: z.string().optional(), lastInteractionAt: z.string().nullable().optional(), windowExpiresAt: z.string().nullable().optional(), isWindowOpen: z.boolean().optional(), createdAt: z.string().nullable().optional(), leadStatus: z.string().nullable().optional(), leadScore: z.number().int().nullable().optional(), leadSummary: z.string().nullable().optional(), originatingCampaignId: z.string().nullable().optional(), qualifierCompletedAt: z.string().nullable().optional(), lastMessagePreview: z.string().nullable().optional(), lastMessageDirection: z.string().nullable().optional()}); export const conversationListResponse = z.object({content: z.array(conversationListItem), page: z.number().int(), size: z.number().int(), totalElements: z.number().int(), totalPages: z.number().int()}); 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 }; /** * Listar conversas * * GET /v1/conversations */ export const getApiV1Conversations = async (args: GetApiV1ConversationsArgs, options?: GetApiV1ConversationsOptions) => { const {baseUrl = "https://api.ararahq.com", fetch: fetchFn = fetch, bearerAuth} = 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.leadStatus != null) query.append("leadStatus", String(args.leadStatus)) if (args.window != null) query.append("window", String(args.window)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args['X-Waba-Id'] != null) headers.set("X-Waba-Id", String(args['X-Waba-Id'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/conversations${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(conversationListResponse, await res.json()) };