import {z} from 'zod' export type ApplicationInstanceKind = 'ApplicationInstance'; export type MapLabelKeyString = {[key: string]: string}; export type ApplicationInstanceMetadata = {name: string, application: string, labels?: MapLabelKeyString | undefined}; export type ResourceType = 'TOPIC' | 'CONSUMER_GROUP' | 'SUBJECT' | 'CONNECTOR' | 'TRANSACTIONAL_ID'; export type ResourcePatternType = 'LITERAL' | 'PREFIXED'; export type OwnershipMode = 'ALL' | 'LIMITED'; export type ResourceWithOwnership = {type: ResourceType, name: string, patternType: ResourcePatternType, connectCluster?: string | undefined, ownershipMode?: OwnershipMode | undefined}; export type CatalogVisibility = 'PRIVATE' | 'PUBLIC'; export type ApplicationInstanceSpec = {cluster: string, topicPolicyRef?: Array | undefined, policyRef?: Array | undefined, resources?: Array | undefined, applicationManagedServiceAccount?: boolean | undefined, serviceAccount?: string | undefined, defaultCatalogVisibility?: CatalogVisibility | undefined}; export type ApplicationInstanceResourceV1 = {apiVersion: string, kind: ApplicationInstanceKind, metadata: ApplicationInstanceMetadata, spec: ApplicationInstanceSpec}; export type UpdateApiPublicSelfServeV1ApplicationInstanceArgs = {dryMode?: boolean | undefined, body: ApplicationInstanceResourceV1}; export type UpdateApiPublicSelfServeV1ApplicationInstanceOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const applicationInstanceKind = z.literal("ApplicationInstance"); export const mapLabelKeyString = z.record(z.string(), z.string()); export const applicationInstanceMetadata = z.object({name: z.string(), application: z.string(), labels: mapLabelKeyString.optional()}); export const resourceType = z.enum(["TOPIC", "CONSUMER_GROUP", "SUBJECT", "CONNECTOR", "TRANSACTIONAL_ID"]); export const resourcePatternType = z.enum(["LITERAL", "PREFIXED"]); export const ownershipMode = z.enum(["ALL", "LIMITED"]); export const resourceWithOwnership = z.object({type: resourceType, name: z.string(), patternType: resourcePatternType, connectCluster: z.string().optional(), ownershipMode: ownershipMode.optional()}); export const catalogVisibility = z.enum(["PRIVATE", "PUBLIC"]); export const applicationInstanceSpec = z.object({cluster: z.string(), topicPolicyRef: z.array(z.string()).optional(), policyRef: z.array(z.string()).optional(), resources: z.array(resourceWithOwnership).optional(), applicationManagedServiceAccount: z.boolean().optional(), serviceAccount: z.string().optional(), defaultCatalogVisibility: catalogVisibility.optional()}); export const applicationInstanceResourceV1 = z.object({apiVersion: z.string(), kind: applicationInstanceKind, metadata: applicationInstanceMetadata, spec: applicationInstanceSpec}); export const upsertResult = z.enum(["Created", "Updated", "NotChanged"]); export const applicationInstanceApplyResultV1 = z.object({resource: applicationInstanceResourceV1, 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 ApplicationInstance * * PUT /public/self-serve/v1/application-instance */ export const updateApiPublicSelfServeV1ApplicationInstance = async (args: UpdateApiPublicSelfServeV1ApplicationInstanceArgs, options: UpdateApiPublicSelfServeV1ApplicationInstanceOptions) => { 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/self-serve/v1/application-instance${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(applicationInstanceApplyResultV1, await res.json()) };