import {z} from 'zod' export type GetApiV1ContactsArgs = {q?: string | undefined, lifecycle?: ('ALL' | 'NEW' | 'ENGAGED' | 'SILENT' | 'DORMANT' | 'OPTED_OUT') | undefined, page?: number | undefined, size?: number | undefined, 'X-Waba-Id'?: string | undefined}; export type GetApiV1ContactsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const contactResponse = z.object({id: z.string(), name: z.string(), phone: z.string(), email: z.string().nullable().optional(), attributes: z.record(z.string(), z.unknown()).nullable().optional(), tags: z.array(z.string()), createdAt: z.string(), lifecycle: z.string(), source: z.string(), outboundCount: z.number().int(), inboundCount: z.number().int(), firstSeenAt: z.string().nullable().optional(), lastOutboundAt: z.string().nullable().optional(), lastInboundAt: z.string().nullable().optional(), lastMessageAt: z.string().nullable().optional(), optOutAt: z.string().nullable().optional(), lastTemplateName: z.string().nullable().optional()}); export const contactsListResponse = z.object({contacts: z.array(contactResponse), total: z.number().int(), page: z.number().int(), size: 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 contatos * * GET /v1/contacts */ export const getApiV1Contacts = async (args: GetApiV1ContactsArgs, options?: GetApiV1ContactsOptions) => { 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.q != null) query.append("q", String(args.q)) if (args.lifecycle != null) query.append("lifecycle", String(args.lifecycle)) 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/contacts${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(contactsListResponse, await res.json()) };