import {z} from 'zod' export type GetApiDashboardMessagesArgs = {status?: ('all' | 'delivered' | 'sent' | 'read' | 'failed' | 'pending') | undefined, q?: string | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiDashboardMessagesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dashboardMessageItem = z.object({id: z.string().optional(), status: z.enum(["PENDING", "SCHEDULED", "PROCESSING", "SENT_TO_PROVIDER", "SENT", "DELIVERED", "READ", "FAILED", "CANCELED"]).optional(), mode: z.string().optional(), sender: z.string().optional(), receiver: z.string().optional(), messageType: z.string().optional(), templateName: z.string().optional(), templateSid: z.string().optional(), templateLanguage: z.string().optional(), providerSid: z.string().optional(), errorDetails: z.string().optional(), scheduledAt: z.string().optional(), externalReference: z.string().optional(), deliveredAt: z.string().optional(), readAt: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), cost: z.number().optional()}); export const dashboardMessagesResponse = z.object({messages: z.array(dashboardMessageItem), 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 histórico de mensagens * * GET /dashboard/messages */ export const getApiDashboardMessages = async (args: GetApiDashboardMessagesArgs, options?: GetApiDashboardMessagesOptions) => { 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.q != null) query.append("q", String(args.q)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/dashboard/messages${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(dashboardMessagesResponse, await res.json()) };