import {z} from 'zod' export type GetApiApiV1ProvidersAwsDcSubnetsArgs = {AccessKeyID?: string | undefined, SecretAccessKey?: string | undefined, Credential?: string | undefined, dc: string, vpc?: string | undefined}; export type GetApiApiV1ProvidersAwsDcSubnetsOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const aWSTag = z.object({key: z.string().optional(), value: z.string().optional()}); export const aWSSubnet = z.object({availability_zone: z.string().optional(), availability_zone_id: z.string().optional(), available_ip_address_count: z.number().int().optional(), default: z.boolean().optional(), id: z.string().optional(), ipv4cidr: z.string().optional(), ipv6cidr: z.string().optional(), isDefaultSubnet: z.boolean().optional(), name: z.string().optional(), state: z.string().optional(), tags: z.array(aWSTag).optional()}); export const aWSSubnetList = z.array(aWSSubnet); 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 }; /** * Lists available AWS subnets * * GET /api/v1/providers/aws/{dc}/subnets */ export const getApiApiV1ProvidersAwsDcSubnets = async (args: GetApiApiV1ProvidersAwsDcSubnetsArgs, options: GetApiApiV1ProvidersAwsDcSubnetsOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.AccessKeyID != null) headers.set("AccessKeyID", String(args.AccessKeyID)) if (args.SecretAccessKey != null) headers.set("SecretAccessKey", String(args.SecretAccessKey)) if (args.Credential != null) headers.set("Credential", String(args.Credential)) if (args.vpc != null) headers.set("vpc", String(args.vpc)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/providers/aws/${encodeURIComponent(String(args.dc))}/subnets${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(aWSSubnetList, await res.json()) };