import {z} from 'zod' export type GetApiGatewayV2TopicViewArgs = {vcluster?: string | undefined, name?: string | undefined, showDefaults?: boolean | undefined}; export type GetApiGatewayV2TopicViewOptions = {baseUrl: string, httpAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const topicViewMetadata = z.object({name: z.string(), vCluster: z.string().optional()}); export const transformation = z.object({type: z.string(), statement: z.string()}); export const onErrorStrategyType = z.enum(["DROP", "FAIL_FETCH"]); export const onErrorStrategy = z.object({type: onErrorStrategyType}); export const topicViewSpec = z.object({transformation: transformation, onError: onErrorStrategy, physicalCluster: z.string().optional(), schemaRegistryConnectionName: z.string().optional()}); export const topicView = z.object({kind: z.string(), apiVersion: z.string(), metadata: topicViewMetadata, spec: topicViewSpec}); export const getApiGatewayV2TopicViewResponse = z.array(topicView); 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/topic-view */ export const getApiGatewayV2TopicView = async (args: GetApiGatewayV2TopicViewArgs, options: GetApiGatewayV2TopicViewOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.vcluster != null) query.append("vcluster", String(args.vcluster)) if (args.name != null) query.append("name", String(args.name)) if (args.showDefaults != null) query.append("showDefaults", String(args.showDefaults)) if (httpAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${httpAuth.username}:${httpAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/gateway/v2/topic-view${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(getApiGatewayV2TopicViewResponse, await res.json()) };