import {z} from 'zod' export type GetApiDashboardHealthOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const brokenPipelineRes = z.object({id: z.string(), name: z.string()}).and(z.record(z.string(), z.unknown())); export const brokenConnectorRes = z.object({id: z.string(), name: z.string(), type: z.string()}).and(z.record(z.string(), z.unknown())); export const activeSnapshotRes = z.object({source_id: z.string(), source_name: z.string(), topic_count: z.number().int().optional(), completed_count: z.number().int().optional()}).and(z.record(z.string(), z.unknown())); export const topicHealthRes = z.object({topics_high_latency: z.number().int().optional(), topics_with_lag: z.number().int().optional(), max_latency_ms: z.number().int().optional(), max_lag: z.number().int().optional()}).and(z.record(z.string(), z.unknown())); export const dashboardHealthRes = z.object({sources_broken: z.number().int().optional(), destinations_broken: z.number().int().optional(), transforms_failed: z.number().int().optional(), failed_transform_ids: z.array(z.string()).optional(), pipelines_broken: z.number().int().optional(), broken_pipelines: z.array(brokenPipelineRes).optional(), dlq_topics_with_messages: z.number().int().optional(), orphaned_topic_count: z.number().int().optional(), stale_source_count: z.number().int().optional(), partition_skew_count: z.number().int().optional(), throughput_total_rps: z.number().optional(), broken_connectors: z.array(brokenConnectorRes).optional(), sources_throttled: z.number().int().optional(), throttled_connectors: z.array(brokenConnectorRes).optional(), active_snapshots: z.array(activeSnapshotRes).optional(), topic_health: topicHealthRes.optional()}).and(z.record(z.string(), z.unknown())); 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 dashboard health summary * * GET /dashboard/health */ export const getApiDashboardHealth = async (options?: GetApiDashboardHealthOptions) => { const {baseUrl = "https://api.streamkap.com", fetch: fetchFn = fetch, FronteggHTTPAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/dashboard/health${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(dashboardHealthRes, await res.json()) };