import {z} from 'zod' export type GetApiApiV1PerformanceTunerStateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const power = z.object({watt: z.number().int().gte(0)}); export const timestamp = z.object({nanos: z.number().int(), seconds: z.number().int()}); export const gigaHashrate = z.object({gigahash_per_second: z.number()}); export const powerTargetProfile = z.object({created: timestamp.nullable().optional(), estimated_power_consumption: power.nullable().optional(), measured_hashrate: gigaHashrate.nullable().optional(), target: power.nullable().optional()}); export const powerTargetModeState = z.object({current_target: power.nullable().optional(), profile: powerTargetProfile.nullable().optional()}); export const teraHashrate = z.object({terahash_per_second: z.number()}); export const hashrateTargetProfile = z.object({created: timestamp.nullable().optional(), estimated_power_consumption: power.nullable().optional(), measured_hashrate: gigaHashrate.nullable().optional(), target: teraHashrate.nullable().optional()}); export const hashrateTargetModeState = z.object({current_target: teraHashrate.nullable().optional(), profile: hashrateTargetProfile.nullable().optional()}); export const modeState = z.union([z.object({powertargetmodestate: powerTargetModeState}), z.object({hashratetargetmodestate: hashrateTargetModeState})]); export const getTunerStateResponse = z.object({mode_state: modeState.nullable().optional(), overall_tuner_state: z.number().int()}); 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 tuner state * * GET /api/v1/performance/tuner-state */ export const getApiApiV1PerformanceTunerState = async (options?: GetApiApiV1PerformanceTunerStateOptions) => { const {baseUrl = "http://miner", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/performance/tuner-state${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(getTunerStateResponse, await res.json()) };