import {z} from 'zod' export type ConfluentOrganizationKind = 'ConfluentOrganization'; export type MapLabelKeyString = {[key: string]: string}; export type ConfluentOrganizationMetadata = {name: string, labels?: MapLabelKeyString | undefined}; export type ConfluentOrganizationSpec = {displayName: string, apiKey: string, apiSecret?: string | undefined}; export type ConfluentOrganizationResourceV1 = {apiVersion: string, kind: ConfluentOrganizationKind, metadata: ConfluentOrganizationMetadata, spec: ConfluentOrganizationSpec}; export type UpdateApiPublicConfluentV1ConfluentOrganizationArgs = {dryMode?: boolean | undefined, body: ConfluentOrganizationResourceV1}; export type UpdateApiPublicConfluentV1ConfluentOrganizationOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const confluentOrganizationKind = z.literal("ConfluentOrganization"); export const mapLabelKeyString = z.record(z.string(), z.string()); export const confluentOrganizationMetadata = z.object({name: z.string(), labels: mapLabelKeyString.optional()}); export const confluentOrganizationSpec = z.object({displayName: z.string(), apiKey: z.string(), apiSecret: z.string().optional()}); export const confluentOrganizationResourceV1 = z.object({apiVersion: z.string(), kind: confluentOrganizationKind, metadata: confluentOrganizationMetadata, spec: confluentOrganizationSpec}); export const upsertResult = z.enum(["Created", "Updated", "NotChanged"]); export const confluentOrganizationApplyResultV1 = z.object({resource: confluentOrganizationResourceV1, 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 ConfluentOrganization * * PUT /public/confluent/v1/confluent-organization */ export const updateApiPublicConfluentV1ConfluentOrganization = async (args: UpdateApiPublicConfluentV1ConfluentOrganizationArgs, options: UpdateApiPublicConfluentV1ConfluentOrganizationOptions) => { 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/confluent/v1/confluent-organization${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(confluentOrganizationApplyResultV1, await res.json()) };