import {z} from 'zod' export type TopicViewMetadata = {name: unknown, vCluster?: unknown | undefined}; export type Transformation = {type: string, statement: string}; export type OnErrorStrategyType = 'DROP' | 'FAIL_FETCH'; export type OnErrorStrategy = {type: OnErrorStrategyType}; export type TopicViewSpec = {transformation: Transformation, onError: OnErrorStrategy, physicalCluster?: string | undefined, schemaRegistryConnectionName?: string | undefined}; export type TopicView = {kind: string, apiVersion: string, metadata: TopicViewMetadata, spec: TopicViewSpec}; export type UpdateApiGatewayV2TopicViewArgs = {dryMode?: boolean | undefined, body: TopicView}; export type UpdateApiGatewayV2TopicViewOptions = {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 upsertResult = z.enum(["Created", "NotChanged", "Updated"]); export const applyResultTopicView = z.object({resource: topicView, upsertResult: upsertResult}); 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 }; /** * PUT /gateway/v2/topic-view */ export const updateApiGatewayV2TopicView = async (args: UpdateApiGatewayV2TopicViewArgs, options: UpdateApiGatewayV2TopicViewOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryMode != null) query.append("dryMode", String(args.dryMode)) 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: "PUT", 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(applyResultTopicView, await res.json()) };