import {z} from 'zod' export type Topology = {[key: string]: string}; export type ClusterVolumeSpec = {Group?: string | undefined, AccessMode?: ({Scope?: ('single' | 'multi') | undefined, Sharing?: ('none' | 'readonly' | 'onewriter' | 'all') | undefined, MountVolume?: Record | undefined, Secrets?: (Array<{Key?: string | undefined, Secret?: string | undefined}>) | undefined, AccessibilityRequirements?: ({Requisite?: Array | undefined, Preferred?: Array | undefined}) | undefined, CapacityRange?: ({RequiredBytes?: number | undefined, LimitBytes?: number | undefined}) | undefined, Availability?: ('active' | 'pause' | 'drain') | undefined}) | undefined}; export type VolumeCreateOptions = {Name?: string | undefined, Driver?: string | undefined, DriverOpts?: {[key: string]: string} | undefined, Labels?: {[key: string]: string} | undefined, ClusterVolumeSpec?: ClusterVolumeSpec | undefined}; export type CreateApiVolumesCreateArgs = {body: VolumeCreateOptions}; export type CreateApiVolumesCreateOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const objectVersion = z.object({Index: z.number().int().optional()}); export const topology = z.record(z.string(), z.string()); export const clusterVolumeSpec = z.object({Group: z.string().optional(), AccessMode: z.object({Scope: z.enum(["single", "multi"]).optional(), Sharing: z.enum(["none", "readonly", "onewriter", "all"]).optional(), MountVolume: z.object({}).optional(), Secrets: z.array(z.object({Key: z.string().optional(), Secret: z.string().optional()})).optional(), AccessibilityRequirements: z.object({Requisite: z.array(topology).optional(), Preferred: z.array(topology).optional()}).optional(), CapacityRange: z.object({RequiredBytes: z.number().int().optional(), LimitBytes: z.number().int().optional()}).optional(), Availability: z.enum(["active", "pause", "drain"]).optional()}).optional()}); export const clusterVolume = z.object({ID: z.string().optional(), Version: objectVersion.optional(), CreatedAt: z.string().optional(), UpdatedAt: z.string().optional(), Spec: clusterVolumeSpec.optional(), Info: z.object({CapacityBytes: z.number().int().optional(), VolumeContext: z.record(z.string(), z.string()).optional(), VolumeID: z.string().optional(), AccessibleTopology: z.array(topology).optional()}).optional(), PublishStatus: z.array(z.object({NodeID: z.string().optional(), State: z.enum(["pending-publish", "published", "pending-node-unpublish", "pending-controller-unpublish"]).optional(), PublishContext: z.record(z.string(), z.string()).optional()})).optional()}); export const volume = z.object({Name: z.string(), Driver: z.string(), Mountpoint: z.string(), CreatedAt: z.string().optional(), Status: z.record(z.string(), z.object({})).optional(), Labels: z.record(z.string(), z.string()), Scope: z.enum(["local", "global"]), ClusterVolume: clusterVolume.optional(), Options: z.record(z.string(), z.string()), UsageData: z.object({Size: z.number().int(), RefCount: z.number().int()}).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 }; /** * Create a volume * * POST /volumes/create */ export const createApiVolumesCreate = async (args: CreateApiVolumesCreateArgs, options: CreateApiVolumesCreateOptions) => { const {baseUrl, 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(/\/+$/, '')}/volumes/create${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(volume, await res.json()) };