import {z} from 'zod' export type PropertyUcCallTelephone = {telephone: string, type?: ('WORK' | 'HOME' | 'OTHER' | 'FAX' | 'MOBILE') | undefined}; export type UcEmail = {email: string, type?: ('WORK' | 'HOME' | 'OTHER') | undefined}; export type PropertyUcContactEmails = Array; export type UcTelephone = {telephone: string, type?: ('WORK' | 'HOME' | 'OTHER' | 'FAX' | 'MOBILE') | undefined}; export type PropertyUcContactTelephones = Array; export type UcMetadata = {id?: string | undefined, slug?: string | undefined, value?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined, namespace?: string | undefined, format?: ('TEXT' | 'NUMBER' | 'DATE' | 'BOOLEAN' | 'FILE' | 'TEXTAREA' | 'SINGLE_SELECT' | 'MULTIPLE_SELECT' | 'MEASUREMENT' | 'PRICE' | 'YES_NO' | 'CURRENCY' | 'URL') | undefined, extra_data?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined}; export type PropertyUcContactMetadata = Array; export type UcContact = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, name?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, title?: string | undefined, company?: string | undefined, emails?: PropertyUcContactEmails | undefined, telephones?: PropertyUcContactTelephones | undefined, metadata?: PropertyUcContactMetadata | undefined, raw?: {[key: string]: unknown} | undefined}; export type PropertyUcCallContacts = Array; export type PropertyUcCallMetadata = Array; export type PropertyUcCallInstructionTelephone = {telephone: string, type?: ('WORK' | 'HOME' | 'OTHER' | 'FAX' | 'MOBILE') | undefined}; export type UcCallInstruction = {type: 'SAY' | 'PLAY' | 'PAUSE' | 'DIAL' | 'HANGUP', text?: string | undefined, language?: string | undefined, audio_url?: string | undefined, seconds?: number | undefined, telephone?: PropertyUcCallInstructionTelephone | undefined, sip_uri?: string | undefined, user_phone?: string | undefined, timeout_seconds?: number | undefined}; export type PropertyUcCallInstructions = Array; export type UcCall = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, contact_id?: string | undefined, telephone?: PropertyUcCallTelephone | undefined, start_at?: string | undefined, end_at?: string | undefined, user_id?: string | undefined, contacts?: PropertyUcCallContacts | undefined, is_private?: boolean | undefined, user_name?: string | undefined, user_phone?: string | undefined, type?: ('INBOUND' | 'OUTBOUND') | undefined, metadata?: PropertyUcCallMetadata | undefined, raw?: {[key: string]: unknown} | undefined, conversation_identifier?: string | undefined, status?: ('QUEUED' | 'RINGING' | 'IN_PROGRESS' | 'COMPLETED' | 'BUSY' | 'NO_ANSWER' | 'FAILED' | 'CANCELED' | 'OTHER') | undefined, answer_at?: string | undefined, duration_seconds?: number | undefined, cost_amount?: number | undefined, currency?: string | undefined, parent_id?: string | undefined, instructions?: PropertyUcCallInstructions | undefined}; export type UpdateApiUcConnectionIdCallIdArgs = {fields?: (Array<'id' | 'created_at' | 'updated_at' | 'contact_id' | 'telephone' | 'start_at' | 'end_at' | 'user_id' | 'contacts' | 'is_private' | 'user_name' | 'user_phone' | 'type' | 'metadata' | 'raw' | 'conversation_identifier' | 'status' | 'answer_at' | 'duration_seconds' | 'cost_amount' | 'currency' | 'parent_id' | 'instructions'>) | undefined, raw?: string | undefined, connection_id: string, id: string, body: UcCall}; export type UpdateApiUcConnectionIdCallIdOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyUcCallTelephone = z.object({telephone: z.string(), type: z.enum(["WORK", "HOME", "OTHER", "FAX", "MOBILE"]).optional()}); export const ucEmail = z.object({email: z.string(), type: z.enum(["WORK", "HOME", "OTHER"]).optional()}); export const propertyUcContactEmails = z.array(ucEmail); export const ucTelephone = z.object({telephone: z.string(), type: z.enum(["WORK", "HOME", "OTHER", "FAX", "MOBILE"]).optional()}); export const propertyUcContactTelephones = z.array(ucTelephone); export const ucMetadata = z.object({id: z.string().optional(), slug: z.string().optional(), value: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional(), namespace: z.string().optional(), format: z.enum(["TEXT", "NUMBER", "DATE", "BOOLEAN", "FILE", "TEXTAREA", "SINGLE_SELECT", "MULTIPLE_SELECT", "MEASUREMENT", "PRICE", "YES_NO", "CURRENCY", "URL"]).optional(), extra_data: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional()}); export const propertyUcContactMetadata = z.array(ucMetadata); export const ucContact = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), name: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), title: z.string().optional(), company: z.string().optional(), emails: propertyUcContactEmails.optional(), telephones: propertyUcContactTelephones.optional(), metadata: propertyUcContactMetadata.optional(), raw: z.record(z.string(), z.unknown()).optional()}); export const propertyUcCallContacts = z.array(ucContact); export const propertyUcCallMetadata = z.array(ucMetadata); export const propertyUcCallInstructionTelephone = z.object({telephone: z.string(), type: z.enum(["WORK", "HOME", "OTHER", "FAX", "MOBILE"]).optional()}); export const ucCallInstruction = z.object({type: z.enum(["SAY", "PLAY", "PAUSE", "DIAL", "HANGUP"]), text: z.string().optional(), language: z.string().optional(), audio_url: z.string().optional(), seconds: z.number().optional(), telephone: propertyUcCallInstructionTelephone.optional(), sip_uri: z.string().optional(), user_phone: z.string().optional(), timeout_seconds: z.number().optional()}); export const propertyUcCallInstructions = z.array(ucCallInstruction); export const ucCall = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), contact_id: z.string().optional(), telephone: propertyUcCallTelephone.optional(), start_at: z.string().optional(), end_at: z.string().optional(), user_id: z.string().optional(), contacts: propertyUcCallContacts.optional(), is_private: z.boolean().optional(), user_name: z.string().optional(), user_phone: z.string().optional(), type: z.enum(["INBOUND", "OUTBOUND"]).optional(), metadata: propertyUcCallMetadata.optional(), raw: z.record(z.string(), z.unknown()).optional(), conversation_identifier: z.string().optional(), status: z.enum(["QUEUED", "RINGING", "IN_PROGRESS", "COMPLETED", "BUSY", "NO_ANSWER", "FAILED", "CANCELED", "OTHER"]).optional(), answer_at: z.string().optional(), duration_seconds: z.number().optional(), cost_amount: z.number().optional(), currency: z.string().optional(), parent_id: z.string().optional(), instructions: propertyUcCallInstructions.optional()}); 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 }; /** * Update a call * * PUT /uc/{connection_id}/call/{id} */ export const updateApiUcConnectionIdCallId = async (args: UpdateApiUcConnectionIdCallIdArgs, options?: UpdateApiUcConnectionIdCallIdOptions) => { const {baseUrl = "https://api.unified.to", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") for (const item of args.fields ?? []) query.append("fields", String(item)) if (args.raw != null) query.append("raw", String(args.raw)) if (jwt !== undefined) headers.set("authorization", jwt) const url = `${baseUrl.replace(/\/+$/, '')}/uc/${encodeURIComponent(String(args.connection_id))}/call/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(ucCall, await res.json()) };