import {z} from 'zod' export type Callback = {requesterId: string, direction: string, contactEndpointId: string, queueId: string}; 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 Inbound = {content: Content, attachments?: Array | undefined, integrationEmail?: string | undefined, externalId?: string | undefined}; export type Outbound = {content?: Content | undefined, agentId: string, attachments?: Array | undefined, integrationEmail?: string | undefined, externalId?: string | undefined, cc?: Array | undefined, bcc?: Array | undefined}; export type CreateMessageInput = Inbound | Outbound; export type BrowserInfo = {name: string, version?: string | undefined, ipAddress?: string | undefined, originatingUrl?: string | undefined}; export type Chat = {requesterId: string, widgetId: string, message: CreateMessageInput, browserInfo?: BrowserInfo | undefined, language?: string | undefined}; export type ContactForm = {requesterId: string, emailIntegrationId: string, subject: string, message: CreateMessageInput, language?: string | undefined}; export type Email = {requesterId: string, emailIntegrationId: string, subject: string, message: CreateMessageInput, language?: string | undefined}; export type Sms = {requesterId: string, contactEndpointId: string, message: CreateMessageInput}; export type MapString = {[key: string]: string}; export type TemplateInput = {templateId: string, templateVariableMap?: MapString | undefined}; export type WhatsappOutboundMessage = {agentId: string, template: TemplateInput}; export type Whatsapp = {requesterId: string, contactEndpointId: string, message: WhatsappOutboundMessage}; export type CreateConversationInput = Callback | Chat | ContactForm | Email | Sms | Whatsapp; export type CreateApiBetaConversationsArgs = {body: CreateConversationInput}; export type CreateApiBetaConversationsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const conversationResponse = z.object({id: z.number().int()}); export const createConversationOutput = z.object({data: conversationResponse}); 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 }; /** * Create conversation * * POST /beta/conversations */ export const createApiBetaConversations = async (args: CreateApiBetaConversationsArgs, options?: CreateApiBetaConversationsOptions) => { 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${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(createConversationOutput, await res.json()) };