import {z} from 'zod' export type HistogramRequestBody = {source_ids?: Array | undefined, destination_ids?: Array | undefined, transform_ids?: Array | undefined} | {[key: string]: unknown}; export type CreateApiLogsHistogramArgs = {timestamp_from: string, timestamp_to?: (string | null) | undefined, bucket_count?: number | undefined, log_level?: (string | null) | undefined, query_string?: (string | null) | undefined, exclude_string?: (string | null) | undefined, use_regex?: boolean | undefined, exclude_regex?: boolean | undefined, include_noise?: boolean | undefined, body: HistogramRequestBody | null}; export type CreateApiLogsHistogramOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const histogramBucket = z.object({timestamp_start: z.string(), timestamp_end: z.string(), error: z.number().int().optional(), warn: z.number().int().optional(), info: z.number().int().optional(), debug: z.number().int().optional(), total: z.number().int().optional()}).and(z.record(z.string(), z.unknown())); export const totalsByLevel = z.object({error: z.number().int().optional(), warn: z.number().int().optional(), info: z.number().int().optional(), debug: z.number().int().optional()}).and(z.record(z.string(), z.unknown())); export const histogramTimeRange = z.object({from: z.string(), to: z.string()}).and(z.record(z.string(), z.unknown())); export const histogramResponse = z.object({buckets: z.array(histogramBucket).optional(), total_logs: z.number().int().optional(), totals_by_level: totalsByLevel.nullable().optional(), time_range: histogramTimeRange}).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 Logs Histogram * * POST /logs/histogram */ export const createApiLogsHistogram = async (args: CreateApiLogsHistogramArgs, options?: CreateApiLogsHistogramOptions) => { 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 (args.timestamp_from != null) query.append("timestamp_from", String(args.timestamp_from)) if (args.timestamp_to != null) query.append("timestamp_to", String(args.timestamp_to)) if (args.bucket_count != null) query.append("bucket_count", String(args.bucket_count)) if (args.log_level != null) query.append("log_level", String(args.log_level)) if (args.query_string != null) query.append("query_string", String(args.query_string)) if (args.exclude_string != null) query.append("exclude_string", String(args.exclude_string)) if (args.use_regex != null) query.append("use_regex", String(args.use_regex)) if (args.exclude_regex != null) query.append("exclude_regex", String(args.exclude_regex)) if (args.include_noise != null) query.append("include_noise", String(args.include_noise)) if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/logs/histogram${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(histogramResponse, await res.json()) };