import {z} from 'zod' export type CreateApiCoreStatsArgs = {Prefer?: 'respond-async' | undefined, body: {_config?: {[key: string]: unknown} | undefined, _filter?: {[key: string]: unknown} | undefined, _group?: string | undefined, group?: string | undefined, short?: boolean | undefined}}; export type CreateApiCoreStatsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const coreStatsTransfer = z.object({bytes: z.number().int().optional(), eta: z.number().nullable().optional(), group: z.string().optional(), name: z.string().optional(), percentage: z.number().int().optional(), size: z.number().int().optional(), speed: z.number().optional(), speedAvg: z.number().optional(), srcFs: z.string().optional(), dstFs: z.string().optional(), srcRemote: z.string().optional(), dstRemote: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const coreStatsChecking = z.object({group: z.string().optional(), name: z.string().optional(), size: z.number().int().optional()}).and(z.record(z.string(), z.unknown())); export const createApiCoreStatsResponse = z.object({bytes: z.number().int(), checks: z.number().int(), deletedDirs: z.number().int(), deletes: z.number().int(), elapsedTime: z.number(), errors: z.number().int(), eta: z.number().nullable().optional(), fatalError: z.boolean(), lastError: z.string().optional(), retryError: z.boolean(), renames: z.number().int(), listed: z.number().int().optional(), serverSideCopies: z.number().int(), serverSideCopyBytes: z.number().int(), serverSideMoveBytes: z.number().int(), serverSideMoves: z.number().int(), speed: z.number(), totalBytes: z.number().int(), totalChecks: z.number().int(), totalTransfers: z.number().int(), transferTime: z.number(), transfers: z.number().int(), updatedDirs: z.number().int(), transferring: z.array(coreStatsTransfer).optional(), checking: z.array(coreStatsChecking).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 }; /** * Current stats snapshot * * POST /core/stats */ export const createApiCoreStats = async (args: CreateApiCoreStatsArgs, options?: CreateApiCoreStatsOptions) => { const {baseUrl = "http://localhost:5572", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Prefer != null) headers.set("Prefer", String(args.Prefer)) const url = `${baseUrl.replace(/\/+$/, '')}/core/stats${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiCoreStatsResponse, await res.json()) };