import {z} from 'zod' export type GetApiV1VolumesArgs = {limit?: number | undefined, offset?: number | undefined, sort?: string | undefined, name?: string | undefined, native_storage_pool_id?: string | undefined, native_volume_id?: string | undefined, storage_id?: string | undefined, status?: ('noraml' | 'offline' | 'abnormal') | undefined}; export type GetApiV1VolumesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const volumeRespSpec = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), name: z.string().optional(), description: z.string().optional(), status: z.enum(["available", "error"]).optional(), storage_id: z.string().optional(), total_capacity: z.number().int().optional(), used_capacity: z.number().int().optional(), free_capacity: z.number().int().optional(), compressed: z.boolean().optional(), deduplicated: z.boolean().optional(), type: z.enum(["thick", "thin"]).optional(), native_volume_id: z.string().optional(), wwn: z.string().optional(), native_storage_pool_id: z.string().optional()}); export const getApiV1VolumesResponse = z.object({volumes: z.array(volumeRespSpec)}).and(z.record(z.string(), z.unknown())); 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 }; /** * List all storage volumes. * * GET /v1/volumes */ export const getApiV1Volumes = async (args: GetApiV1VolumesArgs, options: GetApiV1VolumesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.name != null) query.append("name", String(args.name)) if (args.native_storage_pool_id != null) query.append("native_storage_pool_id", String(args.native_storage_pool_id)) if (args.native_volume_id != null) query.append("native_volume_id", String(args.native_volume_id)) if (args.storage_id != null) query.append("storage_id", String(args.storage_id)) if (args.status != null) query.append("status", String(args.status)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/volumes${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(getApiV1VolumesResponse, await res.json()) };