import {z} from 'zod' export type GetApiApiV1AccountAuthIdNumbersE164HealthArgs = {auth_id: string, e164: string, granularity?: ('daily' | 'hourly') | undefined, days?: number | undefined}; export type GetApiApiV1AccountAuthIdNumbersE164HealthOptions = {baseUrl?: string | undefined, AuthID?: string | undefined, AuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1AccountAuthIdNumbersE164HealthResponse = z.object({e164: z.string().optional(), status: z.string().optional(), usage_status: z.string().optional(), is_spam: z.boolean().optional(), granularity: z.string().optional(), summary: z.object({period_days: z.number().int().optional(), total_calls: z.number().int().optional(), answered_calls: z.number().int().optional(), answer_rate: z.number().optional(), total_minutes: z.number().optional(), avg_duration: z.number().optional()}).optional(), snapshots: z.array(z.object({id: z.string().optional(), ts: z.string().optional(), total_calls: z.number().int().optional(), answered_calls: z.number().int().optional(), failed_calls: z.number().int().optional(), answer_rate: z.number().optional(), total_duration: z.number().optional(), avg_duration: z.number().optional(), total_minutes: z.number().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 }; /** * Get number health dashboard * * GET /api/v1/account/{auth_id}/numbers/{e164}/health */ export const getApiApiV1AccountAuthIdNumbersE164Health = async (args: GetApiApiV1AccountAuthIdNumbersE164HealthArgs, options?: GetApiApiV1AccountAuthIdNumbersE164HealthOptions) => { const {baseUrl = "https://api.vobiz.ai", fetch: fetchFn = fetch, AuthID, AuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.days != null) query.append("days", String(args.days)) if (AuthID !== undefined) headers.set("X-Auth-ID", AuthID) if (AuthToken !== undefined) headers.set("X-Auth-Token", AuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/account/${encodeURIComponent(String(args.auth_id))}/numbers/${encodeURIComponent(String(args.e164))}/health${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(getApiApiV1AccountAuthIdNumbersE164HealthResponse, await res.json()) };