import {z} from 'zod' export type UpdateCallRequest = {Status?: string | undefined, Url?: string | undefined, Method?: ('GET' | 'POST') | undefined, FallbackUrl?: string | undefined, FallbackMethod?: ('GET' | 'POST') | undefined, StatusCallback?: string | undefined, StatusCallbackMethod?: ('GET' | 'POST') | undefined, Texml?: string | undefined}; export type CreateApiTexmlAccountsAccountSidCallsCallSidArgs = {call_sid: string, account_sid: string, body: UpdateCallRequest}; export type CreateApiTexmlAccountsAccountSidCallsCallSidOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const callResource = z.object({account_sid: z.string().optional(), answered_by: z.enum(["human", "machine", "not_sure"]).optional(), caller_name: z.string().optional(), date_created: z.string().optional(), date_updated: z.string().optional(), direction: z.enum(["inbound", "outbound"]).optional(), duration: z.string().optional(), end_time: z.string().optional(), from: z.string().optional(), from_formatted: z.string().optional(), price: z.string().optional(), price_unit: z.string().optional(), sid: z.string().optional(), start_time: z.string().optional(), status: z.enum(["ringing", "in-progress", "canceled", "completed", "failed", "busy", "no-answer"]).optional(), to: z.string().optional(), to_formatted: z.string().optional(), uri: 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 }; /** * Update call * * POST /texml/Accounts/{account_sid}/Calls/{call_sid} */ export const createApiTexmlAccountsAccountSidCallsCallSid = async (args: CreateApiTexmlAccountsAccountSidCallsCallSidArgs, options?: CreateApiTexmlAccountsAccountSidCallsCallSidOptions) => { 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/x-www-form-urlencoded") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/texml/Accounts/${encodeURIComponent(String(args.account_sid))}/Calls/${encodeURIComponent(String(args.call_sid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(callResource, await res.json()) };