import {z} from 'zod' export type CleanupPolicy = 'delete' | 'compact' | 'compact,delete'; export type CleanupPolicyFilter = {oneOf?: Array | undefined, type: 'CleanupPolicyFilter'}; export type HideInternal = {type: 'HideInternal'}; export type HideStream = {type: 'HideStream'}; export type LabelsFilter = {key: string, values?: Array | undefined, type: 'LabelsFilter'}; export type TagsFilter = {atLeast?: Array | undefined, type: 'TagsFilter'}; export type TopicNameFilter = {contains: string, type: 'TopicNameFilter'}; export type TopicListFilter = CleanupPolicyFilter | HideInternal | HideStream | LabelsFilter | TagsFilter | TopicNameFilter; export type TopicListSort = 'ByName' | 'ByReplicationFactor' | 'ByNumPartition' | 'ByMessageCount' | 'ByTopicSizeByte' | 'ByCleanupPolicy' | 'ByRetentionTimes' | 'ByMinIsr' | 'ByLastActivityAt' | 'ByProduceRate'; export type TopicListRequest = {page: number, itemsPerPage: number, filter?: Array | undefined, sort: TopicListSort, descSort?: boolean | undefined}; export type CreateApiPublicV1ClustersClusterNameTopicsArgs = {clusterName: string, body: TopicListRequest}; export type CreateApiPublicV1ClustersClusterNameTopicsOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cleanupPolicy = z.enum(["delete", "compact", "compact,delete"]); export const tagInfo = z.object({id: z.string(), name: z.string(), color: z.string()}); export const mapLabelKeyString = z.record(z.string(), z.string()); export const indexedTopic = z.object({name: z.string(), replicationFactor: z.number().int(), partitionCount: z.number().int(), recordCount: z.number().int(), topicSize: z.number().int().optional(), cleanupPolicy: cleanupPolicy.optional(), retentionInSize: z.number().int().optional(), retentionInMs: z.number().int().optional(), minIsr: z.number().int().optional(), tags: z.array(tagInfo).optional(), labels: mapLabelKeyString, lastActivityAt: z.string().optional(), produceRate: z.number(), type: z.literal("IndexedTopic")}); export const notIndexedTopic = z.object({name: z.string(), type: z.literal("NotIndexedTopic")}); export const topicResponse = z.discriminatedUnion("type", [indexedTopic, notIndexedTopic]); export const pageTopicResponse = z.object({data: z.array(topicResponse).optional(), page: z.number().int(), totalPages: z.number().int(), perPage: z.number().int(), totalElements: z.number().int()}); 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 }; /** * List topic on a kafka cluster * * POST /public/v1/clusters/{clusterName}/topics */ export const createApiPublicV1ClustersClusterNameTopics = async (args: CreateApiPublicV1ClustersClusterNameTopicsArgs, options: CreateApiPublicV1ClustersClusterNameTopicsOptions) => { 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 (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/clusters/${encodeURIComponent(String(args.clusterName))}/topics${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(pageTopicResponse, await res.json()) };