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 EmailString = string; export type StringId = string; export type AttachmentInput = {url: string}; export type CreateHistoricalMessageInput = {channel?: Channel | undefined, direction?: Direction | undefined, from_email?: EmailString | undefined, to_email?: EmailString | undefined, text?: string | undefined, author_id?: StringId | undefined, attachments?: Array | undefined}; export type CreateApiV1ConversationsCsidMessagesHistoricalArgs = {csid: number, body: CreateHistoricalMessageInput}; export type CreateApiV1ConversationsCsidMessagesHistoricalOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const stringId = z.string(); export const timestamp = z.string(); export const channel = z.enum(["widget_chat", "phone", "voice_mail", "email", "contact_form", "callback", "facebook_messenger"]); export const messageAttributes = z.object({text: z.string().optional(), to_emails: z.string().optional(), cc_email: z.string().optional(), from_email: z.string().optional(), inline_images: z.string().optional(), attachments: z.string().optional(), direction: z.string().optional(), a_phone_number: z.string().optional(), b_phone_number: z.string().optional(), is_automated_message: z.string().optional(), is_auto_reply: z.string().optional()}); export const message = z.object({id: stringId, created_at: timestamp, author_id: stringId, channel: channel, direction: channel.optional(), attributes: messageAttributes.optional()}); export const createApiV1ConversationsCsidMessagesHistoricalResponse = z.object({data: message}); 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 }; /** * Add a historical message to a conversation * * POST /v1/conversations/{csid}/messages/historical * * Also accepts auth: BearerAuth */ export const createApiV1ConversationsCsidMessagesHistorical = async (args: CreateApiV1ConversationsCsidMessagesHistoricalArgs, options: CreateApiV1ConversationsCsidMessagesHistoricalOptions) => { 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/${encodeURIComponent(String(args.csid))}/messages/historical${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(createApiV1ConversationsCsidMessagesHistoricalResponse, await res.json()) };