import {z} from 'zod' export type SubjectKind = 'Subject'; export type MapLabelKeyString = {[key: string]: string}; export type SubjectMetadata = {name: string, cluster: string, labels?: MapLabelKeyString | undefined}; export type SchemaFormat = 'AVRO' | 'JSON' | 'PROTOBUF'; export type SchemaCompatibility = 'BACKWARD' | 'BACKWARD_TRANSITIVE' | 'FORWARD' | 'FORWARD_TRANSITIVE' | 'FULL' | 'FULL_TRANSITIVE' | 'NONE'; export type SchemaReference = {name: string, subject: string, version: number}; export type SubjectSpec = {format: SchemaFormat, compatibility?: SchemaCompatibility | undefined, schema: string, id?: number | undefined, version?: number | undefined, references?: Array | undefined, guid?: string | undefined}; export type SubjectResourceV2 = {apiVersion: string, kind: SubjectKind, metadata: SubjectMetadata, spec: SubjectSpec}; export type UpdateApiPublicKafkaV2ClusterClusterSubjectArgs = {cluster: string, dryMode?: boolean | undefined, body: SubjectResourceV2}; export type UpdateApiPublicKafkaV2ClusterClusterSubjectOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const subjectKind = z.literal("Subject"); export const mapLabelKeyString = z.record(z.string(), z.string()); export const subjectMetadata = z.object({name: z.string(), cluster: z.string(), labels: mapLabelKeyString.optional()}); export const schemaFormat = z.enum(["AVRO", "JSON", "PROTOBUF"]); export const schemaCompatibility = z.enum(["BACKWARD", "BACKWARD_TRANSITIVE", "FORWARD", "FORWARD_TRANSITIVE", "FULL", "FULL_TRANSITIVE", "NONE"]); export const schemaReference = z.object({name: z.string(), subject: z.string(), version: z.number().int()}); export const subjectSpec = z.object({format: schemaFormat, compatibility: schemaCompatibility.optional(), schema: z.string(), id: z.number().int().optional(), version: z.number().int().optional(), references: z.array(schemaReference).optional(), guid: z.string().optional()}); export const subjectResourceV2 = z.object({apiVersion: z.string(), kind: subjectKind, metadata: subjectMetadata, spec: subjectSpec}); export const upsertResult = z.enum(["Created", "Updated", "NotChanged"]); export const subjectApplyResultV2 = z.object({resource: subjectResourceV2, 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 }; /** * Create or update a Subject * * PUT /public/kafka/v2/cluster/{cluster}/subject */ export const updateApiPublicKafkaV2ClusterClusterSubject = async (args: UpdateApiPublicKafkaV2ClusterClusterSubjectArgs, options: UpdateApiPublicKafkaV2ClusterClusterSubjectOptions) => { 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', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/kafka/v2/cluster/${encodeURIComponent(String(args.cluster))}/subject${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(subjectApplyResultV2, await res.json()) };