import {z} from 'zod' export type AWSBandwidth = '50000' | '100000' | '200000' | '300000' | '400000' | '500000' | '1000000' | '2000000' | '5000000' | '10000000' | '25000000'; export type NBTenantAWSDirectConnectCreateRequest = {vi_name: string, site_name?: (string | null) | undefined, account_id?: (string | null) | undefined, max_bandwidth?: (AWSBandwidth | null) | undefined, aws_customer_port_name?: (string | null) | undefined}; export type CreateApiServiceTenantNameL2AwsDirectConnectArgs = {tenant_name: unknown, body: NBTenantAWSDirectConnectCreateRequest}; export type CreateApiServiceTenantNameL2AwsDirectConnectOptions = {baseUrl: string, HTTPBearerFromCookie?: string | undefined, fetch?: typeof fetch | undefined}; export const configurationRequestStatus = z.enum(["received", "in_progress", "success", "failed", "cancelled"]); export const nBRequestResponse = z.object({id: z.string(), created_at: z.string(), updated_at: z.string(), status: configurationRequestStatus, extra_details: z.record(z.string(), z.unknown()).nullable().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 }; /** * Create Aws Tenant L2 * * POST /service/{tenant_name}/l2/aws_direct_connect */ export const createApiServiceTenantNameL2AwsDirectConnect = async (args: CreateApiServiceTenantNameL2AwsDirectConnectArgs, options: CreateApiServiceTenantNameL2AwsDirectConnectOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearerFromCookie} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearerFromCookie !== undefined) headers.set('authorization', `Bearer ${HTTPBearerFromCookie}`) const url = `${baseUrl.replace(/\/+$/, '')}/service/${encodeURIComponent(String(args.tenant_name))}/l2/aws_direct_connect${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(nBRequestResponse, await res.json()) };