import {z} from 'zod' export type GetApiV1PortsArgs = {limit?: number | undefined, offset?: number | undefined, sort?: string | undefined, name?: string | undefined, id?: string | undefined, native_port_id?: string | undefined, storage_id?: string | undefined, connection_status?: ('connected' | 'disconnected' | 'unknown') | undefined, health_status?: ('normal' | 'abnormal' | 'unknown') | undefined}; export type GetApiV1PortsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const portSpec = z.object({id: z.string(), created_at: z.string().optional(), updated_at: z.string().optional(), name: z.string(), storage_id: z.string().optional(), native_port_id: z.string().optional(), native_parent_id: z.string().optional(), speed: z.number().int().optional(), max_speed: z.number().int().optional(), location: z.string().optional(), connection_status: z.enum(["connected", "disconnected", "unknown"]).optional(), health_status: z.enum(["normal", "abnormal", "unknown"]).optional(), type: z.enum(["fc", "iscsi", "ficon", "fcoe", "eth", "sas", "ib", "other"]).optional(), logical_type: z.enum(["frontend", "backend", "service", "management", "internal", "maintenance", "interconnect", "other"]).optional(), wwn: z.string().optional(), mac_address: z.string().optional(), ipv4: z.string().optional(), ipv4_mask: z.string().optional(), ipv6: z.string().optional(), ipv6_mask: z.string().optional()}); export const getApiV1PortsResponse = z.object({ports: z.array(portSpec)}).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 ports. * * GET /v1/ports */ export const getApiV1Ports = async (args: GetApiV1PortsArgs, options: GetApiV1PortsOptions) => { 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_port_id != null) query.append("native_port_id", String(args.native_port_id)) if (args.storage_id != null) query.append("storage_id", String(args.storage_id)) if (args.connection_status != null) query.append("connection_status", String(args.connection_status)) if (args.health_status != null) query.append("health_status", String(args.health_status)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ports${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(getApiV1PortsResponse, await res.json()) };