import {z} from 'zod' export type GetApiV1MessageExportArgs = {created_after?: string | undefined, created_before?: string | undefined}; export type GetApiV1MessageExportOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const messageInfo = z.object({id: z.string().optional(), csid: z.number().int().optional(), created_at: z.number().int().optional(), initial_channel: z.string().optional(), author_name: z.string().optional(), author_email: z.string().optional(), direction: z.enum(["inbound", "outbound"]).optional(), text: z.string().optional(), from_phone_number: z.string().optional(), to_phone_number: z.string().optional(), duration: z.number().optional(), recording_url: z.string().optional(), voicemail_url: z.string().optional(), to: z.array(z.string()).optional(), from: z.string().optional(), cc: z.array(z.string()).optional(), bcc: z.array(z.string()).optional(), attached_files: z.array(z.string()).optional(), is_automated_message: z.boolean().optional(), chat_input_question: z.string().optional(), chat_input_answer: z.string().optional(), chat_menu_text: z.string().optional(), form_submission: z.object({}).optional()}); export const getApiV1MessageExportResponse = z.array(messageInfo); 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 }; /** * Returns a list of messages. * * GET /v1/message_export */ export const getApiV1MessageExport = async (args: GetApiV1MessageExportArgs, options?: GetApiV1MessageExportOptions) => { const {baseUrl = "https://exports.dixa.io", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/message_export${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(getApiV1MessageExportResponse, await res.json()) };