import {z} from 'zod' export type GetApiAccountsAccountSidRecentCallsArgs = {AccountSid: string, page: number, count: number, days?: number | undefined, start?: string | undefined, end?: string | undefined, answered?: ('true' | 'false') | undefined, direction?: ('inbound' | 'outbound') | undefined, filter?: string | undefined}; export type GetApiAccountsAccountSidRecentCallsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAccountsAccountSidRecentCallsResponse = z.object({total: z.number().int().optional(), batch: z.number().int().optional(), page: z.number().int().optional(), data: z.array(z.object({account_sid: z.string(), call_sid: z.string(), from: z.string(), to: z.string(), answered: z.boolean(), sip_call_id: z.string().optional(), sip_status: z.number().int(), duration: z.number().int(), attempted_at: z.number().int(), answered_at: z.number().int().optional(), terminated_at: z.number().int(), termination_reason: z.string().optional(), host: z.string().optional(), remote_host: z.string().optional(), direction: z.enum(["inbound", "outbound"]), trunk: z.string().optional()})).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 }; /** * retrieve recent calls for an account * * GET /Accounts/{AccountSid}/RecentCalls */ export const getApiAccountsAccountSidRecentCalls = async (args: GetApiAccountsAccountSidRecentCallsArgs, options?: GetApiAccountsAccountSidRecentCallsOptions) => { const {baseUrl = "https://api.jambonz.cloud/v1", 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.count != null) query.append("count", String(args.count)) if (args.days != null) query.append("days", String(args.days)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.answered != null) query.append("answered", String(args.answered)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.filter != null) query.append("filter", String(args.filter)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/Accounts/${encodeURIComponent(String(args.AccountSid))}/RecentCalls${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(getApiAccountsAccountSidRecentCallsResponse, await res.json()) };