import {z} from 'zod' export type OnConsumerGroupName = {contains: string, type: 'OnConsumerGroupName'}; export type GroupType = 'Unknown' | 'Classic' | 'Consumer' | 'Share'; export type OnGroupType = {oneOf?: Array | undefined, type: 'OnGroupType'}; export type OnLabels = {key: string, values?: Array | undefined, type: 'OnLabels'}; export type ConsumerGroupState = 'Unknown' | 'PreparingRebalance' | 'CompletingRebalance' | 'Stable' | 'Dead' | 'Empty' | 'Assigning' | 'Reconciling'; export type OnState = {oneOf?: Array | undefined, type: 'OnState'}; export type ConsumerGroupFilter = OnConsumerGroupName | OnGroupType | OnLabels | OnState; export type ConsumerGroupSort = 'ByName' | 'ByState' | 'ByOverallLag' | 'ByMaxLagTimeInSeconds' | 'ByMembers' | 'ByGroupType' | 'ByTopicCount'; export type ConsumerGroupRequest = {page: number, itemsPerPage: number, filter?: Array | undefined, sort: ConsumerGroupSort, descSort?: boolean | undefined}; export type CreateApiPublicV1ClustersClusterNameConsumergroupsArgs = {clusterName: string, body: ConsumerGroupRequest}; export type CreateApiPublicV1ClustersClusterNameConsumergroupsOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const consumerGroupState = z.enum(["Unknown", "PreparingRebalance", "CompletingRebalance", "Stable", "Dead", "Empty", "Assigning", "Reconciling"]); export const mapLabelKeyString = z.record(z.string(), z.string()); export const groupType = z.enum(["Unknown", "Classic", "Consumer", "Share"]); export const indexedConsumerGroup = z.object({name: z.string(), state: consumerGroupState, members: z.number().int(), overallLag: z.number().int().optional(), topics: z.array(z.string()).optional(), maxLagTimeInSeconds: z.number().int().optional(), labels: mapLabelKeyString, groupType: groupType, type: z.literal("IndexedConsumerGroup")}); export const notIndexedConsumerGroup = z.object({name: z.string(), type: z.literal("NotIndexedConsumerGroup")}); export const consumerGroupResponse = z.discriminatedUnion("type", [indexedConsumerGroup, notIndexedConsumerGroup]); export const pageConsumerGroupResponse = z.object({data: z.array(consumerGroupResponse).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 consumer group * * POST /public/v1/clusters/{clusterName}/consumergroups */ export const createApiPublicV1ClustersClusterNameConsumergroups = async (args: CreateApiPublicV1ClustersClusterNameConsumergroupsArgs, options: CreateApiPublicV1ClustersClusterNameConsumergroupsOptions) => { 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))}/consumergroups${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(pageConsumerGroupResponse, await res.json()) };