import {z} from 'zod' export type GlueSchemaKind = 'GlueSchema'; export type MapLabelKeyString = {[key: string]: string}; export type GlueSchemaMetadata = {name: string, cluster: string, labels?: MapLabelKeyString | undefined}; export type SchemaFormat = 'AVRO' | 'JSON' | 'PROTOBUF'; export type GlueCompatibility = 'BACKWARD' | 'BACKWARD_ALL' | 'DISABLED' | 'FORWARD' | 'FORWARD_ALL' | 'FULL' | 'FULL_ALL' | 'NONE'; export type GlueSchemaSpec = {format: SchemaFormat, compatibility: GlueCompatibility, schema: string, id?: string | undefined, version?: number | undefined}; export type GlueSchemaResourceV2 = {apiVersion: string, kind: GlueSchemaKind, metadata: GlueSchemaMetadata, spec: GlueSchemaSpec}; export type UpdateApiPublicKafkaV2ClusterClusterGlueSchemasArgs = {cluster: string, dryMode?: boolean | undefined, body: GlueSchemaResourceV2}; export type UpdateApiPublicKafkaV2ClusterClusterGlueSchemasOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const glueSchemaKind = z.literal("GlueSchema"); export const mapLabelKeyString = z.record(z.string(), z.string()); export const glueSchemaMetadata = z.object({name: z.string(), cluster: z.string(), labels: mapLabelKeyString.optional()}); export const schemaFormat = z.enum(["AVRO", "JSON", "PROTOBUF"]); export const glueCompatibility = z.enum(["BACKWARD", "BACKWARD_ALL", "DISABLED", "FORWARD", "FORWARD_ALL", "FULL", "FULL_ALL", "NONE"]); export const glueSchemaSpec = z.object({format: schemaFormat, compatibility: glueCompatibility, schema: z.string(), id: z.string().optional(), version: z.number().int().optional()}); export const glueSchemaResourceV2 = z.object({apiVersion: z.string(), kind: glueSchemaKind, metadata: glueSchemaMetadata, spec: glueSchemaSpec}); export const upsertResult = z.enum(["Created", "Updated", "NotChanged"]); export const glueSchemaApplyResultV2 = z.object({resource: glueSchemaResourceV2, 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 GlueSchema * * PUT /public/kafka/v2/cluster/{cluster}/glue-schemas */ export const updateApiPublicKafkaV2ClusterClusterGlueSchemas = async (args: UpdateApiPublicKafkaV2ClusterClusterGlueSchemasArgs, options: UpdateApiPublicKafkaV2ClusterClusterGlueSchemasOptions) => { 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))}/glue-schemas${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(glueSchemaApplyResultV2, await res.json()) };