import {z} from 'zod' export type TopicTemplateKind = 'TopicTemplate'; export type MapLabelKeyString = {[key: string]: string}; export type TopicTemplateMetadata = {name: string, labels?: MapLabelKeyString | undefined}; export type TopicTemplateDefaultsMetadata = {name?: string | undefined, labels?: MapLabelKeyString | undefined}; export type MapString = {[key: string]: string}; export type TopicTemplateDefaultsSpec = {partitions?: number | undefined, replicationFactor?: number | undefined, configs?: MapString | undefined}; export type TopicTemplateDefaults = {metadata: TopicTemplateDefaultsMetadata, spec: TopicTemplateDefaultsSpec}; export type TopicTemplateSpec = {displayName: string, description?: string | undefined, defaults: TopicTemplateDefaults}; export type TopicTemplateResourceV2 = {apiVersion: string, kind: TopicTemplateKind, metadata: TopicTemplateMetadata, spec: TopicTemplateSpec}; export type UpdateApiPublicConsoleV2TopicTemplateArgs = {dryMode?: boolean | undefined, body: TopicTemplateResourceV2}; export type UpdateApiPublicConsoleV2TopicTemplateOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const topicTemplateKind = z.literal("TopicTemplate"); export const mapLabelKeyString = z.record(z.string(), z.string()); export const topicTemplateMetadata = z.object({name: z.string(), labels: mapLabelKeyString.optional()}); export const topicTemplateDefaultsMetadata = z.object({name: z.string().optional(), labels: mapLabelKeyString.optional()}); export const mapString = z.record(z.string(), z.string()); export const topicTemplateDefaultsSpec = z.object({partitions: z.number().int().gt(0).optional(), replicationFactor: z.number().int().gt(0).optional(), configs: mapString.optional()}); export const topicTemplateDefaults = z.object({metadata: topicTemplateDefaultsMetadata, spec: topicTemplateDefaultsSpec}); export const topicTemplateSpec = z.object({displayName: z.string(), description: z.string().optional(), defaults: topicTemplateDefaults}); export const topicTemplateResourceV2 = z.object({apiVersion: z.string(), kind: topicTemplateKind, metadata: topicTemplateMetadata, spec: topicTemplateSpec}); export const upsertResult = z.enum(["Created", "Updated", "NotChanged"]); export const topicTemplateApplyResultV2 = z.object({resource: topicTemplateResourceV2, 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 TopicTemplate * * PUT /public/console/v2/topic-template */ export const updateApiPublicConsoleV2TopicTemplate = async (args: UpdateApiPublicConsoleV2TopicTemplateArgs, options: UpdateApiPublicConsoleV2TopicTemplateOptions) => { 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/console/v2/topic-template${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(topicTemplateApplyResultV2, await res.json()) };