import {z} from 'zod' export type NodeInfo = {address: string, availableCpus: number, availableDiskGb: number, availableMemoryMb: number, bootId?: (string | null) | undefined, csrPem?: (string | null) | undefined, diskCordonedAt?: (string | null) | undefined, id: string, labels: {[key: string]: string}, lastHeartbeatAt?: (string | null) | undefined, registeredAt: string, reservedDiskGb?: number | undefined, runtime?: string | undefined, smolvmAddress: string, smolvmVersion?: (string | null) | undefined, status: string, totalCpus: number, totalDiskGb: number, totalMemoryMb: number, usedCpus?: number | undefined, usedDiskGb?: number | undefined, usedMemoryMb?: number | undefined}; export type CreateApiV1NodesRegisterArgs = {body: NodeInfo}; export type CreateApiV1NodesRegisterOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nodeInfo = z.object({address: z.string(), availableCpus: z.number().int().gte(0), availableDiskGb: z.number().int().gte(0), availableMemoryMb: z.number().int().gte(0), bootId: z.string().nullable().optional(), csrPem: z.string().nullable().optional(), diskCordonedAt: z.string().nullable().optional(), id: z.string(), labels: z.record(z.string(), z.string()), lastHeartbeatAt: z.string().nullable().optional(), registeredAt: z.string(), reservedDiskGb: z.number().int().gte(0).optional(), runtime: z.string().optional(), smolvmAddress: z.string(), smolvmVersion: z.string().nullable().optional(), status: z.string(), totalCpus: z.number().int().gte(0), totalDiskGb: z.number().int().gte(0), totalMemoryMb: z.number().int().gte(0), usedCpus: z.number().optional(), usedDiskGb: z.number().int().gte(0).optional(), usedMemoryMb: z.number().int().gte(0).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 }; /** * Register a node. * * POST /v1/nodes/register */ export const createApiV1NodesRegister = async (args: CreateApiV1NodesRegisterArgs, options?: CreateApiV1NodesRegisterOptions) => { const {baseUrl = "https://api.smolmachines.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/nodes/register${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(nodeInfo, await res.json()) };