import {z} from 'zod' export type GetApiTopicsTopicIdRawMetricsArgs = {topic_id: string, since?: string | undefined, metric_names?: (string | null) | undefined, include_destinations?: boolean | undefined, include_qa?: boolean | undefined}; export type GetApiTopicsTopicIdRawMetricsOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const topicRawMetricRow = z.object({metric_name: z.string(), partition: z.string(), task: z.string(), context: z.string(), value: z.number(), updated_at: z.string()}); export const topicRawConnectorMetrics = z.object({connector: z.string(), ch_topic: z.string(), metrics: z.array(topicRawMetricRow)}); export const topicRawDestinationMetrics = z.object({destination_id: z.string(), connector: z.string(), ch_topic: z.string(), pipeline_id: z.string().nullable().optional(), transform_id: z.string().nullable().optional(), metrics: z.array(topicRawMetricRow)}); export const topicRawQaRow = z.object({metric_name: z.string(), connector_ctx: z.string(), connector_id: z.string(), value: z.number(), updated_at: z.string()}); export const topicRawMetricsRes = z.object({topic_id: z.string(), since: z.string(), source: topicRawConnectorMetrics.nullable(), destinations: z.array(topicRawDestinationMetrics).optional(), qa: z.array(topicRawQaRow).optional()}); 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 latest raw metrics for a topic * * GET /topics/{topic_id}/raw-metrics */ export const getApiTopicsTopicIdRawMetrics = async (args: GetApiTopicsTopicIdRawMetricsArgs, options?: GetApiTopicsTopicIdRawMetricsOptions) => { 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 (args.since != null) query.append("since", String(args.since)) if (args.metric_names != null) query.append("metric_names", String(args.metric_names)) if (args.include_destinations != null) query.append("include_destinations", String(args.include_destinations)) if (args.include_qa != null) query.append("include_qa", String(args.include_qa)) if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/topics/${encodeURIComponent(String(args.topic_id))}/raw-metrics${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(topicRawMetricsRes, await res.json()) };