import {z} from 'zod' export type GetApiGatewayV2InterceptorArgs = {name?: string | undefined, global?: boolean | undefined, vcluster?: string | undefined, group?: string | undefined, username?: string | undefined}; export type GetApiGatewayV2InterceptorOptions = {baseUrl: string, httpAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const interceptorScope = z.object({vCluster: z.string().optional(), group: z.string().optional(), username: z.string().optional()}); export const interceptorMetadata = z.object({name: z.string(), scope: interceptorScope.optional()}); export const mapJson = z.record(z.string(), z.unknown()); export const interceptorSpec = z.object({comment: z.string().optional(), pluginClass: z.string(), priority: z.number().int(), config: mapJson}); export const interceptor = z.object({kind: z.string(), apiVersion: z.string(), metadata: interceptorMetadata, spec: interceptorSpec}); export const getApiGatewayV2InterceptorResponse = z.array(interceptor); 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 /gateway/v2/interceptor */ export const getApiGatewayV2Interceptor = async (args: GetApiGatewayV2InterceptorArgs, options: GetApiGatewayV2InterceptorOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.global != null) query.append("global", String(args.global)) if (args.vcluster != null) query.append("vcluster", String(args.vcluster)) if (args.group != null) query.append("group", String(args.group)) if (args.username != null) query.append("username", String(args.username)) if (httpAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${httpAuth.username}:${httpAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/gateway/v2/interceptor${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(getApiGatewayV2InterceptorResponse, await res.json()) };