import {z} from 'zod' export type GetApiV1ConversationExportArgs = {closed_after?: string | undefined, closed_before?: string | undefined, created_after?: string | undefined, created_before?: string | undefined, updated_after?: string | undefined, updated_before?: string | undefined, last_message_created_after?: string | undefined, last_message_created_before?: string | undefined, csids?: Array | undefined}; export type GetApiV1ConversationExportOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rating = z.object({rating_score: z.number().int().optional(), rating_message: z.string().optional(), rating_type: z.string().optional(), rating_created_timestamp: z.number().int().optional(), rating_offered_timestamp: z.number().int().optional(), rating_status: z.string().optional(), rating_language: z.string().optional(), rating_modified_timestamp: z.number().int().optional(), rating_rated_timestamp: z.number().int().optional(), rating_scheduled_timestamp: z.number().int().optional(), rating_scheduled_for_timestamp: z.number().int().optional(), rating_unscheduled_timestamp: z.number().int().optional(), rating_cancelled_timestamp: z.number().int().optional()}); export const customAttribute = z.object({attribute_id: z.string().optional(), identifier: z.string().optional(), value: z.string().optional(), data_type: z.string().optional(), archived: z.boolean().optional()}); export const conversationInfo = z.object({id: z.number().int(), created_at: z.number().int(), initial_channel: z.string(), requester_id: z.string(), requester_name: z.string().optional(), requester_email: z.string().optional(), requester_additional_emails: z.array(z.string()).optional(), requester_phone_number: z.string().optional(), requester_additional_phone_numbers: z.array(z.string()).optional(), queued_at: z.number().optional(), queue_id: z.string().optional(), queue_name: z.string().optional(), closed_at: z.number().optional(), rating_score: z.union([z.literal(0), z.literal(1)]).optional(), rating_message: z.string().optional(), ratings: z.array(rating).optional(), direction: z.enum(["inbound", "outbound"]).optional(), assigned_at: z.number().int().optional(), assignee_id: z.string().optional(), assignee_name: z.string().optional(), assignee_email: z.string().optional(), assignee_phone_number: z.string().optional(), to_provisioned_phone_number_id: z.string().optional(), to_provisioned_phone_number_name: z.string().optional(), total_duration: z.number().optional(), handling_duration: z.number().optional(), dixa_email_integration_id: z.string().optional(), dixa_email_integration_sender_name: z.string().optional(), forwarding_email: z.string().optional(), facebook_page_id: z.string().optional(), facebook_page_name: z.string().optional(), widget_id: z.string().optional(), widget_name: z.string().optional(), tags: z.array(z.string()).optional(), tags_info: z.array(z.string()).optional(), conversation_wrapup_notes: z.array(z.string()).optional(), transferee_name: z.string().optional(), transfer_time: z.number().optional(), originating_country: z.string().optional(), updated_at: z.number().optional(), last_message_created_at: z.number().optional(), status: z.enum(["open", "closed", "pending", "awaitingpending"]).optional(), transferee_number: z.string().optional(), from_provisioned_phone_number_id: z.string().optional(), from_provisioned_phone_number_name: z.string().optional(), subject: z.string().optional(), anonymized_at: z.number().optional(), custom_fields: z.array(customAttribute).optional(), exports_handling_duration: z.number().optional(), link_type: z.enum(["email_forward", "follow_up", "side", "related"]).optional(), linked_at: z.number().int().optional(), linked_to: z.number().int().optional()}); export const getApiV1ConversationExportResponse = z.array(conversationInfo); 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 conversations. * * GET /v1/conversation_export */ export const getApiV1ConversationExport = async (args: GetApiV1ConversationExportArgs, options?: GetApiV1ConversationExportOptions) => { 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.closed_after != null) query.append("closed_after", String(args.closed_after)) if (args.closed_before != null) query.append("closed_before", String(args.closed_before)) 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 (args.updated_after != null) query.append("updated_after", String(args.updated_after)) if (args.updated_before != null) query.append("updated_before", String(args.updated_before)) if (args.last_message_created_after != null) query.append("last_message_created_after", String(args.last_message_created_after)) if (args.last_message_created_before != null) query.append("last_message_created_before", String(args.last_message_created_before)) for (const item of args.csids ?? []) query.append("csids", String(item)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/conversation_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(getApiV1ConversationExportResponse, await res.json()) };