import {z} from 'zod' export type GetApiAccountsAccountSidAlertsArgs = {AccountSid: string, page: number, count: number, days?: number | undefined, start?: string | undefined, end?: string | undefined, alert_type?: ('webhook-failure' | 'webhook-connection-failure' | 'webhook-auth-failure' | 'no-tts' | 'no-stt' | 'tts-failure' | 'stt-failure' | 'no-carrier' | 'call-limit' | 'device-limit' | 'api-limit') | undefined}; export type GetApiAccountsAccountSidAlertsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAccountsAccountSidAlertsResponse = z.object({total: z.number().int().optional(), batch: z.number().int().optional(), page: z.number().int().optional(), data: z.array(z.object({time: z.string(), account_sid: z.string(), alert_type: z.string(), message: z.string(), detail: 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 alerts for an account * * GET /Accounts/{AccountSid}/Alerts */ export const getApiAccountsAccountSidAlerts = async (args: GetApiAccountsAccountSidAlertsArgs, options?: GetApiAccountsAccountSidAlertsOptions) => { 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.alert_type != null) query.append("alert_type", String(args.alert_type)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/Accounts/${encodeURIComponent(String(args.AccountSid))}/Alerts${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(getApiAccountsAccountSidAlertsResponse, await res.json()) };