import {z} from 'zod' export type Channel = 'widget_chat' | 'phone' | 'voice_mail' | 'email' | 'contact_form' | 'callback' | 'facebook_messenger'; export type Direction = 'outbound' | 'inbound' | 'internal'; export type StringId = string; export type CreateCallbackConversationInput = {channel: Channel, direction: Direction, queue_id: StringId, requester_id: StringId, endpoint_id: StringId, tags?: Array | undefined}; export type CreateContactFormConversationInput = {channel: Channel, requester_id: StringId, email_integration_id: StringId, tags?: Array | undefined, subject?: string | undefined, message?: ({text: string, content_type?: ('Text' | 'Markdown' | 'Html') | undefined}) | undefined}; export type CreateConversationInput = CreateCallbackConversationInput | CreateContactFormConversationInput; export type CreateApiV1ConversationsArgs = {body: CreateConversationInput}; export type CreateApiV1ConversationsOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const timestamp = z.string(); export const stringId = z.string(); export const channel = z.enum(["widget_chat", "phone", "voice_mail", "email", "contact_form", "callback", "facebook_messenger"]); export const direction = z.enum(["outbound", "inbound", "internal"]); export const status = z.enum(["open", "closed", "pending", "awaiting_pending"]); export const conversation = z.object({id: z.number().int(), created_at: timestamp, requester_id: stringId, channel: channel, direction: direction, queued_at: timestamp.optional(), queue_id: stringId.optional(), status: status, status_updated_at: timestamp.optional(), attributes: z.object({}).optional(), assigned_to: stringId.optional(), assigned_at: timestamp.optional()}); export const createApiV1ConversationsResponse = z.object({data: conversation}); 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 a conversation * * POST /v1/conversations * * Also accepts auth: BearerAuth */ export const createApiV1Conversations = async (args: CreateApiV1ConversationsArgs, options: CreateApiV1ConversationsOptions) => { const {baseUrl, 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(/\/+$/, '')}/v1/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(createApiV1ConversationsResponse, await res.json()) };