import {z} from 'zod' export type TransferManagerTransferType = 'attended' | 'blind'; export type CallManagerParticipantAddress = {type: 'extension', target: string}; export type CreateApiServiceAgentsTransfersArgs = {body: {transfer_type: TransferManagerTransferType, transferer_call_id: string, transferee_addresses: Array}}; export type CreateApiServiceAgentsTransfersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const transferManagerTransferType = z.enum(["attended", "blind"]); export const commonAddress = z.object({type: z.enum(["", "agent", "conference", "email", "extension", "line", "sip", "tel", "web_session"]).optional(), target: z.string().optional(), target_name: z.string().optional(), name: z.string().optional(), detail: z.string().optional()}); export const transferManagerTransfer = z.object({id: z.string().optional(), customer_id: z.string().optional(), type: transferManagerTransferType.optional(), transferer_call_id: z.string().optional(), transferee_addresses: z.array(commonAddress).optional(), transferee_call_id: z.string().optional(), groupcall_id: z.string().optional(), confbridge_id: z.string().optional(), tm_create: z.string().optional(), tm_update: z.string().optional(), tm_delete: z.string().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 }; /** * Start a transfer * * POST /service_agents/transfers */ export const createApiServiceAgentsTransfers = async (args: CreateApiServiceAgentsTransfersArgs, options?: CreateApiServiceAgentsTransfersOptions) => { const {baseUrl = "https://api.voipbin.net/v1.0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/service_agents/transfers${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(transferManagerTransfer, await res.json()) };