import {z} from 'zod' export type LogCountRequestBody = {source_ids?: Array | undefined, destination_ids?: Array | undefined, transform_ids?: Array | undefined} | {[key: string]: unknown}; export type CreateApiLogsCountArgs = {since: string, 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: LogCountRequestBody | null}; export type CreateApiLogsCountOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const logCountFiltersApplied = z.object({source_ids: z.array(z.string()).optional(), destination_ids: z.array(z.string()).optional(), transform_ids: z.array(z.string()).optional(), log_level: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const logCountResponse = z.object({count: z.number().int(), since: z.string(), filters_applied: logCountFiltersApplied}).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 Count * * POST /logs/count */ export const createApiLogsCount = async (args: CreateApiLogsCountArgs, options?: CreateApiLogsCountOptions) => { 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.since != null) query.append("since", String(args.since)) 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/count${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(logCountResponse, await res.json()) };