import {z} from 'zod' export type SpawnNodeRequest = {tenant_id: string, image?: (string | null) | undefined, node_name: string, memory?: number | undefined, lifetime?: number | undefined}; export type CreateApiAdminSpawnNodeArgs = {body: SpawnNodeRequest}; export type CreateApiAdminSpawnNodeOptions = {baseUrl: string, AdminKey?: string | undefined, fetch?: typeof fetch | undefined}; export const node = z.object({name: z.string(), connection_id: z.string().nullable().optional(), key: z.string(), id: z.string(), create_date_time: z.string(), last_known_version: z.string().nullable().optional(), last_known_features: z.string().nullable().optional(), destroy_date_time: z.string().nullable().optional(), infra_id: z.string().nullable().optional(), demo: z.boolean(), ephemeral: z.boolean().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 }; /** * Spawn Node Endpoint * * POST /admin/spawn-node */ export const createApiAdminSpawnNode = async (args: CreateApiAdminSpawnNodeArgs, options: CreateApiAdminSpawnNodeOptions) => { const {baseUrl, fetch: fetchFn = fetch, AdminKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AdminKey !== undefined) headers.set("X-Tenzir-AdminKey", AdminKey) const url = `${baseUrl.replace(/\/+$/, '')}/admin/spawn-node${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(node, await res.json()) };