import {z} from 'zod' export type GetApiV1PortsIdArgs = {id: string}; export type GetApiV1PortsIdOptions = {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 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 }; /** * Get port detail by port ID. * * GET /v1/ports/{id} */ export const getApiV1PortsId = async (args: GetApiV1PortsIdArgs, options: GetApiV1PortsIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/ports/${encodeURIComponent(String(args.id))}${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(portSpec, await res.json()) };