import {z} from 'zod' export type TopicDetailsEntityTypeEnum = 'sources' | 'destinations' | 'transforms' | 'pipelines' | 'manual' | 'agents'; export type SingleTopicTableMetricReq = {id: string, entity_type: TopicDetailsEntityTypeEnum, connector: string, topic_ids: Array, topic_db_ids: Array} | {[key: string]: unknown}; export type TopicTableMetricsReq = {entities: Array} | {[key: string]: unknown}; export type CreateApiTopicsTableMetricsArgs = {body: TopicTableMetricsReq}; export type CreateApiTopicsTableMetricsOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const topicListKafkaMetadata = z.object({partition_count: z.number().int().nullable().optional(), replication_factor: z.number().int().nullable().optional(), retention_ms: z.number().int().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const topicTableMetricsRowRes = z.object({id: z.string(), kafka: topicListKafkaMetadata, lastMessageTimestamp: z.number().int().nullable().optional(), snapshotStatus: z.array(z.record(z.string(), z.unknown())).optional(), recordErrorTotal: z.number().int().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const createApiTopicsTableMetricsResponse = z.record(z.string(), topicTableMetricsRowRes); 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 }; /** * Per-topic Kafka metadata for the topics list * * POST /topics/table_metrics */ export const createApiTopicsTableMetrics = async (args: CreateApiTopicsTableMetricsArgs, options?: CreateApiTopicsTableMetricsOptions) => { const {baseUrl = "https://api.streamkap.com", fetch: fetchFn = fetch, FronteggHTTPAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/topics/table_metrics${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(createApiTopicsTableMetricsResponse, await res.json()) };