import {z} from 'zod' export type RequestInfo = {requestId: string}; export type User = {name?: string | undefined, company?: string | undefined, detail?: string | undefined, phone?: string | undefined, email?: string | undefined, channel?: number | undefined, joinTimeMS?: number | undefined, leaveTimeMS?: number | undefined, identityType?: ('internal' | 'external' | 'bot') | undefined}; export type CallProperty = {name: string, value: string, xheader?: boolean | undefined, hidden?: boolean | undefined}; export type CallOverview = {callId: string, owner: User, callType?: string | undefined, direction?: ('inbound' | 'outbound') | undefined, stopReason?: ('hangup' | 'owner_hangup' | 'remote_hangup' | 'forward' | 'unsuccessful' | 'unknown') | undefined, deviceName?: string | undefined, isExternal?: boolean | undefined, subjectOverride?: string | undefined, callProperties?: Array | undefined}; export type RecordingFile = {filename?: string | undefined, description?: string | undefined, fileKey?: string | undefined}; export type VoiceRecord = {startTime: number, stopTime?: number | undefined, timeZone?: string | undefined, participants?: Array | undefined, recordingFile?: RecordingFile | undefined}; export type FileInfo = {filename?: string | undefined, fileKey?: string | undefined, fileHref?: string | undefined, description?: string | undefined, isTranscript?: boolean | undefined}; export type VoiceData = {requestInfo: RequestInfo, callOverview: CallOverview, voiceRecords: Array, extraFiles?: Array | undefined}; export type CreateApiV2VoicedataArgs = {'X-SubType': string, body: VoiceData}; export type CreateApiV2VoicedataOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const apiResponse = 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 }; /** * Adds multiple voice records. * * POST /v2/voicedata */ export const createApiV2Voicedata = async (args: CreateApiV2VoicedataArgs, options: CreateApiV2VoicedataOptions) => { const {baseUrl, fetch: fetchFn = fetch} = 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'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/voicedata${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(apiResponse, await res.json()) };