import {z} from 'zod' export type Html = {value: string}; export type Markdown = {value: string}; export type Text = {value: string}; export type Content = Html | Markdown | Text; export type File = {url: string, prettyName?: string | undefined}; export type InboundImport = {content: Content, attachments?: Array | undefined, externalId?: string | undefined, createdAt: string, attributes: unknown}; export type OutboundImport = {content: Content, agentId: string, attachments?: Array | undefined, externalId?: string | undefined, createdAt: string, attributes: unknown}; export type ImportMessageInput = InboundImport | OutboundImport; export type Assignment = {agentId: string, assignedAt: string}; export type Closing = {closedAt: string, closedBy: string}; export type GenericConversationImport = {genericChannelName: string, requesterId: string, requesterConnectionStatus?: string | undefined, direction: string, messages?: Array | undefined, createdAt: string, assignment?: Assignment | undefined, externalId?: string | undefined, closing?: Closing | undefined, attributes: unknown}; export type CreateApiBetaConversationsImportArgs = {body: GenericConversationImport}; export type CreateApiBetaConversationsImportOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const partialError = z.object({message: z.string()}); export const importConversationResponse = z.object({id: z.number().int(), partialErrors: z.array(partialError).optional()}); export const importConversationOutput = z.object({data: importConversationResponse}); 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 }; /** * Import conversation * * POST /beta/conversations/import */ export const createApiBetaConversationsImport = async (args: CreateApiBetaConversationsImportArgs, options?: CreateApiBetaConversationsImportOptions) => { const {baseUrl = "https://dev.dixa.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/beta/conversations/import${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(importConversationOutput, await res.json()) };