import {z} from 'zod' export type KsqlDBClusterKind = 'KsqlDBCluster'; export type KsqlDBMetadata = {name: string, cluster: string}; export type MapString = {[key: string]: string}; export type BasicAuth2 = {username: string, password: string, type: 'BasicAuth'}; export type BearerToken2 = {token: string, type: 'BearerToken'}; export type NoSecurity1 = {type: 'NoSecurity'}; export type SSLAuth2 = {key: string, certificateChain: string, type: 'SSLAuth'}; export type KsqlDBSecurityDto = BasicAuth2 | BearerToken2 | NoSecurity1 | SSLAuth2; export type KsqlDBSpec = {displayName: string, url: string, headers?: MapString | undefined, ignoreUntrustedCertificate?: boolean | undefined, security?: KsqlDBSecurityDto | undefined}; export type KsqlDBClusterResourceV2 = {apiVersion: string, kind: KsqlDBClusterKind, metadata: KsqlDBMetadata, spec: KsqlDBSpec}; export type UpdateApiPublicConsoleV2ClusterClusterKsqldbArgs = {cluster: string, dryMode?: boolean | undefined, body: KsqlDBClusterResourceV2}; export type UpdateApiPublicConsoleV2ClusterClusterKsqldbOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ksqlDBClusterKind = z.literal("KsqlDBCluster"); export const ksqlDBMetadata = z.object({name: z.string(), cluster: z.string()}); export const mapString = z.record(z.string(), z.string()); export const basicAuth2 = z.object({username: z.string(), password: z.string(), type: z.literal("BasicAuth")}); export const bearerToken2 = z.object({token: z.string(), type: z.literal("BearerToken")}); export const noSecurity1 = z.object({type: z.literal("NoSecurity")}); export const sSLAuth2 = z.object({key: z.string(), certificateChain: z.string(), type: z.literal("SSLAuth")}); export const ksqlDBSecurityDto = z.discriminatedUnion("type", [basicAuth2, bearerToken2, noSecurity1, sSLAuth2]); export const ksqlDBSpec = z.object({displayName: z.string(), url: z.string(), headers: mapString.optional(), ignoreUntrustedCertificate: z.boolean().optional(), security: ksqlDBSecurityDto.optional()}); export const ksqlDBClusterResourceV2 = z.object({apiVersion: z.string(), kind: ksqlDBClusterKind, metadata: ksqlDBMetadata, spec: ksqlDBSpec}); export const upsertResult = z.enum(["Created", "Updated", "NotChanged"]); export const ksqlDBClusterApplyResultV2 = z.object({resource: ksqlDBClusterResourceV2, 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 KsqlDBCluster * * PUT /public/console/v2/cluster/{cluster}/ksqldb */ export const updateApiPublicConsoleV2ClusterClusterKsqldb = async (args: UpdateApiPublicConsoleV2ClusterClusterKsqldbArgs, options: UpdateApiPublicConsoleV2ClusterClusterKsqldbOptions) => { 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/cluster/${encodeURIComponent(String(args.cluster))}/ksqldb${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(ksqlDBClusterApplyResultV2, await res.json()) };