import {z} from 'zod' export type GetApiPublicV1ClustersOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const basicAuth = z.object({username: z.string(), password: z.string(), type: z.literal("BasicAuth")}); export const bearerToken = z.object({token: z.string(), type: z.literal("BearerToken")}); export const noSecurity = z.object({type: z.literal("NoSecurity")}); export const sSLAuth = z.object({key: z.string(), certificateChain: z.string(), type: z.literal("SSLAuth")}); export const confluentLikeSchemaRegistrySecurity = z.discriminatedUnion("type", [basicAuth, bearerToken, noSecurity, sSLAuth]); export const confluentLikeSchemaRegistryResponse = z.object({id: z.string(), url: z.string(), security: confluentLikeSchemaRegistrySecurity, properties: z.string().optional(), ignoreUntrustedCertificate: z.boolean(), schemaRegistryProxyClusterName: z.string().optional(), schemaRegistryProxyId: z.string().optional(), type: z.literal("ConfluentLikeSchemaRegistryResponse")}); export const credentials = z.object({accessKeyId: z.string(), secretKey: z.string(), type: z.literal("Credentials")}); export const fromContext = z.object({profile: z.string().optional(), type: z.literal("FromContext")}); export const fromRole = z.object({role: z.string(), type: z.literal("FromRole")}); export const iAMAnywhere = z.object({trustAnchorArn: z.string(), profileArn: z.string(), roleArn: z.string(), certificate: z.string(), privateKey: z.string(), type: z.literal("IAMAnywhere")}); export const amazonSecurity = z.discriminatedUnion("type", [credentials, fromContext, fromRole, iAMAnywhere]); export const glueSchemaRegistryResponse = z.object({id: z.string(), registryName: z.string().optional(), region: z.string(), security: amazonSecurity, type: z.literal("GlueSchemaRegistryResponse")}); export const sharedSchemaRegistryResponseV2 = z.discriminatedUnion("type", [confluentLikeSchemaRegistryResponse, glueSchemaRegistryResponse]); export const kafkaConnectBasicAuth = z.object({username: z.string(), password: z.string(), type: z.literal("KafkaConnectBasicAuth")}); export const kafkaConnectBearerToken = z.object({token: z.string(), type: z.literal("KafkaConnectBearerToken")}); export const kafkaConnectNoSecurity = z.object({type: z.literal("KafkaConnectNoSecurity")}); export const kafkaConnectSSLAuth = z.object({key: z.string(), certificateChain: z.string(), type: z.literal("KafkaConnectSSLAuth")}); export const kafkaConnectSecurity = z.discriminatedUnion("type", [kafkaConnectBasicAuth, kafkaConnectBearerToken, kafkaConnectNoSecurity, kafkaConnectSSLAuth]); export const mapLabelKeyString = z.record(z.string(), z.string()); export const kafkaConnectWithId = z.object({id: z.string(), slug: z.string(), url: z.string(), name: z.string(), security: kafkaConnectSecurity, headers: z.string().optional(), ignoreUntrustedCertificate: z.boolean(), labels: mapLabelKeyString, policiesRef: z.array(z.string()).optional()}); export const aivenResponse = z.object({project: z.string(), serviceName: z.string(), type: z.literal("Aiven")}); export const confluentPlatformAuthenticationResponse = z.object({username: z.string(), type: z.literal("LDAP")}); export const confluentPlatformMTLSResponse = z.object({clientCertificateId: z.string(), clientKeyId: z.string(), caCertificateIds: z.array(z.string()).optional()}); export const confluentPlatformResponse = z.object({kafkaClusterId: z.string(), mdsUrl: z.string().optional(), authentication: confluentPlatformAuthenticationResponse.optional(), mtls: confluentPlatformMTLSResponse.optional(), schemaRegistryClusterId: z.string().optional(), enableRbacRoleBindings: z.boolean().optional(), ignoreUntrustedCertificate: z.boolean().optional(), type: z.literal("ConfluentPlatform")}); export const confluentResponse = z.object({key: z.string(), confluentEnvironmentId: z.string(), confluentClusterId: z.string(), organizationId: z.string().optional(), schemaRegistryId: z.string().optional(), enableRbacRoleBindings: z.boolean().optional(), type: z.literal("Confluent")}); export const gatewayResponse = z.object({url: z.string(), user: z.string(), virtualCluster: z.string(), ignoreUntrustedCertificate: z.boolean().optional(), type: z.literal("Gateway")}); export const kafkaFlavorResponse = z.discriminatedUnion("type", [aivenResponse, confluentPlatformResponse, confluentResponse, gatewayResponse]); export const ksqlDBBasicAuth = z.object({username: z.string(), password: z.string(), type: z.literal("KsqlDBBasicAuth")}); export const ksqlDBBearerToken = z.object({token: z.string(), type: z.literal("KsqlDBBearerToken")}); export const ksqlDBNoSecurity = z.object({type: z.literal("KsqlDBNoSecurity")}); export const ksqlDBSSLAuth = z.object({key: z.string(), certificateChain: z.string(), type: z.literal("KsqlDBSSLAuth")}); export const ksqlDBSecurity = z.discriminatedUnion("type", [ksqlDBBasicAuth, ksqlDBBearerToken, ksqlDBNoSecurity, ksqlDBSSLAuth]); export const ksqlDBWithId = z.object({id: z.string(), slug: z.string(), url: z.string(), name: z.string(), security: ksqlDBSecurity, headers: z.string().optional(), ignoreUntrustedCertificate: z.boolean()}); export const publicKafkaClusterResponse = z.object({clusterSlug: z.string(), name: z.string(), bootstrapServers: z.string(), properties: z.string().optional(), color: z.string().optional(), icon: z.string().optional(), schemaRegistry: sharedSchemaRegistryResponseV2.optional(), kafkaConnects: z.array(kafkaConnectWithId).optional(), ignoreUntrustedCertificate: z.boolean(), accessCert: z.string().optional(), accessKey: z.string().optional(), createdAt: z.string(), updatedAt: z.string(), amazonSecurity: amazonSecurity.optional(), kafkaFlavor: kafkaFlavorResponse.optional(), ksqlDBs: z.array(ksqlDBWithId).optional()}); export const getApiPublicV1ClustersResponse = z.array(publicKafkaClusterResponse); 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 }; /** * Get all clusters of the organization * * GET /public/v1/clusters */ export const getApiPublicV1Clusters = async (options: GetApiPublicV1ClustersOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/clusters${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiPublicV1ClustersResponse, await res.json()) };