import {z} from 'zod' export type GetApiV1AcceleratorArgs = {before?: unknown | undefined, limit?: number | undefined, name?: string | undefined}; export type GetApiV1AcceleratorOptions = {baseUrl?: string | undefined, accessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const v1Accelerator = z.object({destination_ip: z.string().optional(), destination_port: z.number().int().optional(), id: z.string().optional(), name: z.string().optional(), subspace_ipv4: z.string().optional(), subspace_port: z.number().int().optional()}); export const v1NextPage = z.object({before: z.string().optional(), limit: z.number().int().optional()}); export const v1ListAcceleratorResponse = z.object({data: z.array(v1Accelerator).optional(), next_page: v1NextPage.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 /v1/accelerator */ export const getApiV1Accelerator = async (args: GetApiV1AcceleratorArgs, options?: GetApiV1AcceleratorOptions) => { const {baseUrl = "https://api.subspace.com", fetch: fetchFn = fetch, accessCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.before != null) query.append("before", String(args.before)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.name != null) query.append("name", String(args.name)) if (accessCode !== undefined) headers.set('authorization', `Bearer ${accessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accelerator${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(v1ListAcceleratorResponse, await res.json()) };