import {z} from 'zod' export type TexmlStatusCallback = string; export type TexmlStatusCallbackMethod = 'GET' | 'POST'; export type StreamTrack = 'inbound_track' | 'outbound_track' | 'both_tracks'; export type StreamName = string; export type TexmlBidirectionalStreamMode = 'mp3' | 'rtp'; export type TexmlBidirectionalStreamCodec = 'PCMU' | 'PCMA' | 'G722'; export type TexmlStreamUrl = string; export type TexmlCreateCallStreamingRequestBody = {StatusCallback?: TexmlStatusCallback | undefined, StatusCallbackMethod?: TexmlStatusCallbackMethod | undefined, Track?: StreamTrack | undefined, Name?: StreamName | undefined, BidirectionalMode?: TexmlBidirectionalStreamMode | undefined, BidirectionalCodec?: TexmlBidirectionalStreamCodec | undefined, Url?: TexmlStreamUrl | undefined}; export type CreateApiTexmlAccountsAccountSidCallsCallSidStreamsJsonArgs = {account_sid: string, call_sid: string, body: TexmlCreateCallStreamingRequestBody}; export type CreateApiTexmlAccountsAccountSidCallsCallSidStreamsJsonOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const accountSid = z.string(); export const callControlId = z.string(); export const texmlSid = z.string(); export const dateTimeRFC2822 = z.string(); export const texmlCreateCallStreamingResponseBody = z.object({account_sid: accountSid.optional(), call_sid: callControlId.optional(), sid: texmlSid.optional(), name: z.string().optional(), status: z.literal("in-progress").optional(), date_updated: dateTimeRFC2822.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 }; /** * Start streaming media from a call. * * POST /texml/Accounts/{account_sid}/Calls/{call_sid}/Streams.json */ export const createApiTexmlAccountsAccountSidCallsCallSidStreamsJson = async (args: CreateApiTexmlAccountsAccountSidCallsCallSidStreamsJsonArgs, options?: CreateApiTexmlAccountsAccountSidCallsCallSidStreamsJsonOptions) => { 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))}/Streams.json${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(texmlCreateCallStreamingResponseBody, await res.json()) };