import {z} from 'zod' export type GetApiTopicsMetricsArgs = {timestamp_to?: (string | null) | undefined, timestamp_from?: (string | null) | undefined, time_type?: (string | null) | undefined, time_interval?: (number | null) | undefined, time_unit?: (string | null) | undefined, entity_type?: (string | null) | undefined, entity_id?: (string | null) | undefined, destination_ids?: (string | null) | undefined, value_format?: (string | null) | undefined, has_errors?: (boolean | null) | undefined, partial_name?: (string | null) | undefined}; export type GetApiTopicsMetricsOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const timeseriesEntry = z.object({value: z.union([z.number().int(), z.number()]), timestamp: z.string()}).and(z.record(z.string(), z.unknown())); export const topicTimeseriesMetrics = z.object({byteTotal: z.array(timeseriesEntry).optional(), SnapshotTotalNumberOfEventsSeen: z.array(timeseriesEntry).optional(), StreamingTotalNumberOfCreateEventsSeen: z.array(timeseriesEntry).optional(), StreamingTotalNumberOfDeleteEventsSeen: z.array(timeseriesEntry).optional(), StreamingTotalNumberOfUpdateEventsSeen: z.array(timeseriesEntry).optional(), recordRetryTotal: z.array(timeseriesEntry).optional(), recordSendTotal: z.array(timeseriesEntry).optional()}).and(z.record(z.string(), z.unknown())); export const topicLatestMetrics = z.object({byteTotal: z.union([z.number().int(), z.number()]).nullable().optional(), SnapshotTotalNumberOfEventsSeen: z.union([z.number().int(), z.number()]).nullable().optional(), StreamingTotalNumberOfCreateEventsSeen: z.union([z.number().int(), z.number()]).nullable().optional(), StreamingTotalNumberOfDeleteEventsSeen: z.union([z.number().int(), z.number()]).nullable().optional(), StreamingTotalNumberOfUpdateEventsSeen: z.union([z.number().int(), z.number()]).nullable().optional(), recordRetryTotal: z.union([z.number().int(), z.number()]).nullable().optional(), recordSendTotal: z.union([z.number().int(), z.number()]).nullable().optional()}).and(z.record(z.string(), z.unknown())); export const topicMetricsRes = z.object({timeseries: topicTimeseriesMetrics.nullable().optional(), latest: topicLatestMetrics.nullable().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 topic metrics * * GET /topics/metrics */ export const getApiTopicsMetrics = async (args: GetApiTopicsMetricsArgs, options?: GetApiTopicsMetricsOptions) => { 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.timestamp_to != null) query.append("timestamp_to", String(args.timestamp_to)) if (args.timestamp_from != null) query.append("timestamp_from", String(args.timestamp_from)) if (args.time_type != null) query.append("time_type", String(args.time_type)) if (args.time_interval != null) query.append("time_interval", String(args.time_interval)) if (args.time_unit != null) query.append("time_unit", String(args.time_unit)) if (args.entity_type != null) query.append("entity_type", String(args.entity_type)) if (args.entity_id != null) query.append("entity_id", String(args.entity_id)) if (args.destination_ids != null) query.append("destination_ids", String(args.destination_ids)) if (args.value_format != null) query.append("value_format", String(args.value_format)) if (args.has_errors != null) query.append("has_errors", String(args.has_errors)) if (args.partial_name != null) query.append("partial_name", String(args.partial_name)) if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/topics/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(topicMetricsRes, await res.json()) };