import {z} from 'zod' export type GetApiTexmlAccountsAccountSidCallsArgs = {account_sid: string, Page?: number | undefined, PageSize?: number | undefined, PageToken?: string | undefined, To?: string | undefined, From?: string | undefined, Status?: ('canceled' | 'completed' | 'failed' | 'busy' | 'no-answer') | undefined, StartTime?: string | undefined, 'StartTime>'?: string | undefined, 'StartTime<'?: string | undefined, EndTime?: string | undefined, 'EndTime>'?: string | undefined, 'EndTime<'?: string | undefined}; export type GetApiTexmlAccountsAccountSidCallsOptions = {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 callResourceIndex = z.object({calls: z.array(callResource).optional(), end: z.number().int().optional(), first_page_uri: z.string().optional(), next_page_uri: z.string().optional(), page: z.number().int().optional(), page_size: z.number().int().optional(), start: z.number().int().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 }; /** * Fetch multiple call resources * * GET /texml/Accounts/{account_sid}/Calls */ export const getApiTexmlAccountsAccountSidCalls = async (args: GetApiTexmlAccountsAccountSidCallsArgs, options?: GetApiTexmlAccountsAccountSidCallsOptions) => { const {baseUrl = "https://api.telnyx.com/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Page != null) query.append("Page", String(args.Page)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (args.PageToken != null) query.append("PageToken", String(args.PageToken)) if (args.To != null) query.append("To", String(args.To)) if (args.From != null) query.append("From", String(args.From)) if (args.Status != null) query.append("Status", String(args.Status)) if (args.StartTime != null) query.append("StartTime", String(args.StartTime)) if (args['StartTime>'] != null) query.append("StartTime>", String(args['StartTime>'])) if (args['StartTime<'] != null) query.append("StartTime<", String(args['StartTime<'])) if (args.EndTime != null) query.append("EndTime", String(args.EndTime)) if (args['EndTime>'] != null) query.append("EndTime>", String(args['EndTime>'])) if (args['EndTime<'] != null) query.append("EndTime<", String(args['EndTime<'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/texml/Accounts/${encodeURIComponent(String(args.account_sid))}/Calls${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(callResourceIndex, await res.json()) };