import {z} from 'zod' export type GetApiV1DisksArgs = {limit?: number | undefined, offset?: number | undefined, sort?: string | undefined, name?: string | undefined, id?: string | undefined, native_disk_id?: string | undefined, storage_id?: string | undefined, native_disk_group_id?: string | undefined, status?: ('normal' | 'offline' | 'abnormal') | undefined}; export type GetApiV1DisksOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const diskSpec = z.object({id: z.string(), created_at: z.string().optional(), updated_at: z.string().optional(), name: z.string(), storage_id: z.string().optional(), native_disk_id: z.string().optional(), serial_number: z.string().optional(), manufacturer: z.string().optional(), model: z.string().optional(), firmware: z.string().optional(), speed: z.number().int().optional(), capacity: z.number().int().optional(), location: z.string().optional(), status: z.enum(["normal", "offline", "abnormal"]).optional(), physical_type: z.enum(["sata", "sas", "ssd", "nl-sas", "unknown"]).optional(), logical_type: z.enum(["free", "member", "hotspare", "cache"]).optional(), health_score: z.number().int().optional(), native_disk_group_id: z.string().optional()}); export const getApiV1DisksResponse = z.object({disks: z.array(diskSpec)}).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 disks. * * GET /v1/disks */ export const getApiV1Disks = async (args: GetApiV1DisksArgs, options: GetApiV1DisksOptions) => { 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.id != null) query.append("id", String(args.id)) if (args.native_disk_id != null) query.append("native_disk_id", String(args.native_disk_id)) if (args.storage_id != null) query.append("storage_id", String(args.storage_id)) if (args.native_disk_group_id != null) query.append("native_disk_group_id", String(args.native_disk_group_id)) if (args.status != null) query.append("status", String(args.status)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/disks${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(getApiV1DisksResponse, await res.json()) };