import {z} from 'zod' export type CustomSipHeader = {name: string, value: string}; export type SipHeader = {name: 'User-to-User', value: string}; export type SoundModifications = {pitch?: number | undefined, semitone?: number | undefined, octaves?: number | undefined, track?: string | undefined}; export type TransferCallRequest = {to: string, from?: string | undefined, diversion?: string | undefined, from_display_name?: string | undefined, privacy?: ('id' | 'none') | undefined, audio_url?: string | undefined, send_digits_on_answer?: string | undefined, early_media?: boolean | undefined, media_name?: string | undefined, timeout_secs?: number | undefined, time_limit_secs?: number | undefined, park_after_unbridge?: string | undefined, answering_machine_detection?: ('premium' | 'premium_ios_call_screening_detection' | 'detect' | 'detect_beep' | 'detect_words' | 'greeting_end' | 'disabled') | undefined, answering_machine_detection_config?: ({total_analysis_time_millis?: number | undefined, beep_detection_profile?: ('both' | 'freq_only') | undefined, beep_min_frequency_hz?: number | undefined, beep_max_frequency_hz?: number | undefined, beep_min_tone_duration_millis?: number | undefined, beep_spectral_confirmation?: boolean | undefined, beep_spectral_window_millis?: number | undefined, beep_spectral_min_purity?: number | undefined, beep_spectral_reject_fax_cng?: boolean | undefined, after_greeting_silence_millis?: number | undefined, between_words_silence_millis?: number | undefined, greeting_duration_millis?: number | undefined, initial_silence_millis?: number | undefined, maximum_number_of_words?: number | undefined, maximum_word_length_millis?: number | undefined, silence_threshold?: number | undefined, greeting_total_analysis_time_millis?: number | undefined, greeting_silence_duration_millis?: number | undefined, prompt_end_timeout_millis?: number | undefined}) | undefined, custom_headers?: Array | undefined, client_state?: string | undefined, target_leg_client_state?: string | undefined, command_id?: string | undefined, media_encryption?: ('disabled' | 'SRTP' | 'DTLS') | undefined, sip_auth_username?: string | undefined, sip_auth_password?: string | undefined, sip_headers?: Array | undefined, sip_transport_protocol?: ('UDP' | 'TCP' | 'TLS') | undefined, sound_modifications?: SoundModifications | undefined, webhook_url?: string | undefined, webhook_url_method?: ('POST' | 'GET') | undefined, mute_dtmf?: ('none' | 'both' | 'self' | 'opposite') | undefined, record?: 'record-from-answer' | undefined, record_channels?: ('single' | 'dual') | undefined, record_format?: ('wav' | 'mp3') | undefined, record_max_length?: number | undefined, record_timeout_secs?: number | undefined, record_track?: ('both' | 'inbound' | 'outbound') | undefined, record_trim?: 'trim-silence' | undefined, record_custom_file_name?: string | undefined, sip_region?: ('US' | 'Europe' | 'Canada' | 'Australia' | 'Middle East') | undefined, preferred_codecs?: string | undefined, webhook_urls?: {[key: string]: Array} | undefined, webhook_urls_method?: ('POST' | 'GET') | undefined, webhook_retries_policies?: ({[key: string]: {retries_ms?: Array | undefined}}) | undefined, route_to_mobile?: boolean | undefined}; export type CreateApiCallsCallControlIdActionsTransferArgs = {call_control_id: string, body: TransferCallRequest}; export type CreateApiCallsCallControlIdActionsTransferOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const callControlCommandResult = z.object({result: z.string().optional()}); export const createApiCallsCallControlIdActionsTransferResponse = z.object({data: callControlCommandResult.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 }; /** * Transfer call * * POST /calls/{call_control_id}/actions/transfer */ export const createApiCallsCallControlIdActionsTransfer = async (args: CreateApiCallsCallControlIdActionsTransferArgs, options?: CreateApiCallsCallControlIdActionsTransferOptions) => { const {baseUrl = "https://api.telnyx.com/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/calls/${encodeURIComponent(String(args.call_control_id))}/actions/transfer${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(createApiCallsCallControlIdActionsTransferResponse, await res.json()) };