import {z} from 'zod' export type OrganizationSshKeysSelectiveConfig = {mode: 'selective', ssh_key_ids: Array}; export type OrganizationSshKeysNonSelectiveConfig = {mode: 'all' | 'none'}; export type CloudInitWriteFile = {encoding?: (string | null) | undefined, content: string, owner?: (string | null) | undefined, path: string, permissions?: (string | null) | undefined}; export type InstantCloudInit = {packages?: Array | undefined, write_files?: Array | undefined, runcmd?: Array | undefined}; export type InstantVMConfig = {config_id: string, name: string, password?: (string | null) | undefined, organization_ssh_keys?: (OrganizationSshKeysSelectiveConfig | OrganizationSshKeysNonSelectiveConfig) | undefined, ssh_keys?: Array | undefined, cloud_init?: (InstantCloudInit | null) | undefined, tags?: Array | undefined}; export type CreateApiVirtualMachinesInstantArgs = {body: InstantVMConfig}; export type CreateApiVirtualMachinesInstantOptions = {baseUrl: string, apiTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const instantVMResult = z.object({vm_id: z.string()}); 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 }; /** * Deploy an instant virtual machine * * POST /virtual-machines/instant * * Also accepts auth: APIKeyHeader */ export const createApiVirtualMachinesInstant = async (args: CreateApiVirtualMachinesInstantArgs, options: CreateApiVirtualMachinesInstantOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiTokenAuth !== undefined) headers.set('authorization', `Bearer ${apiTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/virtual-machines/instant${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(instantVMResult, await res.json()) };