import {z} from 'zod' export type RouteMapAction = 'permit' | 'deny'; export type PfxListItem = {ip: string, le?: (number | null) | undefined, ge?: (number | null) | undefined, seq?: (number | null) | undefined, match?: boolean | undefined}; export type NBBGPRouteMapRuleCreateRequest = {pfx_list_name: string, name?: (string | null) | undefined, action: RouteMapAction, priority: number, filters?: (Array | null) | undefined, description?: (string | null) | undefined}; export type CreateApiServiceTenantNameBgpRouteMapRuleArgs = {tenant_name: unknown, body: NBBGPRouteMapRuleCreateRequest}; export type CreateApiServiceTenantNameBgpRouteMapRuleOptions = {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 Tenant Bgp Route Map Rule * * POST /service/{tenant_name}/bgp_route_map_rule */ export const createApiServiceTenantNameBgpRouteMapRule = async (args: CreateApiServiceTenantNameBgpRouteMapRuleArgs, options: CreateApiServiceTenantNameBgpRouteMapRuleOptions) => { 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))}/bgp_route_map_rule${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()) };