import {z} from 'zod' export type GetApiCabinetAdminTrafficArgs = {period?: number | undefined, limit?: number | undefined, offset?: number | undefined, search?: string | undefined, sort_by?: string | undefined, sort_desc?: boolean | undefined, tariffs?: string | undefined, statuses?: string | undefined, nodes?: string | undefined, start_date?: string | undefined, end_date?: string | undefined}; export type GetApiCabinetAdminTrafficOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const userTrafficItem = z.object({user_id: z.number().int(), telegram_id: z.number().int().nullable(), username: z.string().nullable(), email: z.string().nullable(), full_name: z.string(), tariff_name: z.string().nullable(), subscription_status: z.string().nullable(), traffic_limit_gb: z.number(), device_limit: z.number().int(), node_traffic: z.record(z.string(), z.number().int()), total_bytes: z.number().int()}); export const trafficNodeInfo = z.object({node_uuid: z.string(), node_name: z.string(), country_code: z.string()}); export const trafficUsageResponse = z.object({items: z.array(userTrafficItem), nodes: z.array(trafficNodeInfo), total: z.number().int(), offset: z.number().int(), limit: z.number().int(), period_days: z.number().int(), available_tariffs: z.array(z.string()), available_statuses: z.array(z.string())}); 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 Traffic Usage * * GET /cabinet/admin/traffic */ export const getApiCabinetAdminTraffic = async (args: GetApiCabinetAdminTrafficArgs, options: GetApiCabinetAdminTrafficOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period != null) query.append("period", String(args.period)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.search != null) query.append("search", String(args.search)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_desc != null) query.append("sort_desc", String(args.sort_desc)) if (args.tariffs != null) query.append("tariffs", String(args.tariffs)) if (args.statuses != null) query.append("statuses", String(args.statuses)) if (args.nodes != null) query.append("nodes", String(args.nodes)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/cabinet/admin/traffic${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(trafficUsageResponse, await res.json()) };