import {z} from 'zod' export type RequestInfo = {requestId: string}; export type EventUser = {displayName: string, corporateEmail: string, phone?: string | undefined, companyName?: string | undefined, userType?: ('initiator' | 'initiatedOBO' | 'affectedUser' | 'recipient') | undefined, identityType?: ('Internal' | 'External' | 'Bot') | undefined}; export type ConversationProperty = {name: string, value: string, xheader?: boolean | undefined, hidden?: boolean | undefined}; export type ConversationOverview = {conversationId: string, initiatorName: string, initiatorEmail: string, title?: string | undefined, description?: string | undefined, isExternal?: boolean | undefined, isPrivate?: boolean | undefined, initialParticipants?: Array | undefined, conversationProperties?: Array | undefined, subjectOverride?: string | undefined}; export type Eventcontent = {textType?: ('plain' | 'html' | 'markdown') | undefined, message: string}; export type FileInfo = {filename: string, fileKey: string, fileHref?: string | undefined, isInlined?: boolean | undefined, transcribable?: boolean | undefined}; export type ConversationEvent = {eventTime?: number | undefined, eventType?: ('File_transfer' | 'Invitation' | 'Message' | 'Admin_event' | 'User_joined' | 'User_left' | 'Message_edited' | 'Message_deleted' | 'User_removed' | 'User_event' | 'Reaction' | 'Meeting_event' | 'Context_reply') | undefined, childEvents?: Array | undefined, participants?: Array | undefined, content?: Eventcontent | undefined, systemText?: string | undefined, files?: Array | undefined}; export type CreateConversationPayload = {requestInfo: RequestInfo, conversationOverview: ConversationOverview, conversationEvents?: Array | undefined}; export type CreateApiV2ConversationsArgs = {'X-SubType': string, body: CreateConversationPayload}; export type CreateApiV2ConversationsOptions = {baseUrl?: string | undefined, oAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const createConversationResponse = z.object({reconciliationId: z.string()}); 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 }; /** * POST a single conversation message * * POST /v2/conversations */ export const createApiV2Conversations = async (args: CreateApiV2ConversationsArgs, options?: CreateApiV2ConversationsOptions) => { const {baseUrl = "https://conversations.api.globalrelay.com/v2/conversations", fetch: fetchFn = fetch, oAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-SubType'] != null) headers.set("X-SubType", String(args['X-SubType'])) if (oAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${oAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/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(createConversationResponse, await res.json()) };