import {z} from 'zod' export type GetApiApiV1UpgradeAutoUpgradeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const timestamp = z.object({nanos: z.number().int(), seconds: z.number().int()}); export const upgradeTime = z.object({hours: z.number().int().gte(0), minutes: z.number().int().gte(0), seconds: z.number().int().gte(0).nullable().optional()}); export const dailySchedule = z.object({time: upgradeTime.nullable().optional()}); export const weeklySchedule = z.object({day_of_week: z.number().int().nullable().optional(), time: upgradeTime.nullable().optional()}); export const monthlySchedule = z.object({day_of_month: z.number().int().gte(0).nullable().optional(), time: upgradeTime.nullable().optional()}); export const scheduleType = z.union([z.object({daily: dailySchedule}), z.object({weekly: weeklySchedule}), z.object({monthly: monthlySchedule})]); export const autoUpgradeSchedule = z.object({schedule_type: scheduleType.nullable().optional()}); export const getAutoUpgradeStatusResponse = z.object({enabled: z.boolean(), last_execution: timestamp.nullable().optional(), next_execution: timestamp.nullable().optional(), schedule: autoUpgradeSchedule.nullable().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 auto-upgrade settings * * GET /api/v1/upgrade/auto-upgrade */ export const getApiApiV1UpgradeAutoUpgrade = async (options?: GetApiApiV1UpgradeAutoUpgradeOptions) => { 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/upgrade/auto-upgrade${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(getAutoUpgradeStatusResponse, await res.json()) };