import {z} from 'zod' export type KubernetesLabelValue = string; export type Tag = {name: string, value: string}; export type TagList = Array; export type ResourceMetadata = {name: KubernetesLabelValue, description?: string | undefined, tags?: TagList | undefined}; export type Ipv4Address = string; export type Ipv4AddressList = Array; export type Route = {prefix: string, nexthop: string}; export type Routes = Array; export type RegionId = string; export type NetworkReservations = {prefixLength: number, providerReservedPrefixLength?: number | undefined}; export type NetworkV2CreateSpec = {dnsNameservers: Ipv4AddressList, routes?: Routes | undefined, organizationId: string, projectId: string, regionId: RegionId, prefix: string, reservations?: NetworkReservations | undefined}; export type NetworkV2Create = {metadata: ResourceMetadata, spec: NetworkV2CreateSpec}; export type CreateApiApiV2NetworksArgs = {body: NetworkV2Create}; export type CreateApiApiV2NetworksOptions = {baseUrl: string, oauth2Authentication?: string | undefined, fetch?: typeof fetch | undefined}; export const kubernetesLabelValue = z.string(); export const tag = z.object({name: z.string(), value: z.string()}); export const tagList = z.array(tag); export const resourceProvisioningStatus = z.enum(["unknown", "provisioning", "provisioned", "deprovisioning", "error"]); export const resourceHealthStatus = z.enum(["unknown", "healthy", "degraded", "error"]); export const projectScopedResourceReadMetadata = z.object({name: kubernetesLabelValue, description: z.string().optional(), tags: tagList.optional(), id: z.string(), creationTime: z.string(), createdBy: z.string().optional(), modifiedTime: z.string().optional(), modifiedBy: z.string().optional(), deletionTime: z.string().optional(), provisioningStatus: resourceProvisioningStatus, healthStatus: resourceHealthStatus, organizationId: z.string(), projectId: z.string()}); export const ipv4Address = z.string(); export const ipv4AddressList = z.array(ipv4Address); export const route = z.object({prefix: z.string(), nexthop: z.string()}); export const routes = z.array(route); export const networkV2Spec = z.object({dnsNameservers: ipv4AddressList, routes: routes.optional()}); export const networkReservations = z.object({prefixLength: z.number().int().gte(0).lte(30), providerReservedPrefixLength: z.number().int().gte(0).lte(30).optional()}); export const networkV2Status = z.object({regionId: z.string(), prefix: z.string(), reservations: networkReservations.optional()}); export const networkV2Read = z.object({metadata: projectScopedResourceReadMetadata, spec: networkV2Spec, status: networkV2Status}); 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 }; /** * Create network * * POST /api/v2/networks */ export const createApiApiV2Networks = async (args: CreateApiApiV2NetworksArgs, options: CreateApiApiV2NetworksOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2Authentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2Authentication !== undefined) headers.set('authorization', `Bearer ${oauth2Authentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/networks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(networkV2Read, await res.json()) };