import {z} from 'zod' export type LogBody = {source_ids?: Array | undefined, destination_ids?: Array | undefined, transform_ids?: Array | undefined} | {[key: string]: unknown}; export type CreateApiLogsArgs = {size?: (number | null) | undefined, from?: (number | null) | undefined, log_level?: (string | null) | undefined, query_string?: (string | null) | undefined, use_regex?: boolean | undefined, exclude_string?: (string | null) | undefined, exclude_regex?: boolean | undefined, include_noise?: boolean | undefined, timestamp_to?: (string | null) | undefined, timestamp_from?: (string | null) | undefined, body: LogBody}; export type CreateApiLogsOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiLogsResponse = 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 * * POST /logs */ export const createApiLogs = async (args: CreateApiLogsArgs, options?: CreateApiLogsOptions) => { 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.size != null) query.append("size", String(args.size)) if (args.from != null) query.append("from", String(args.from)) 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.use_regex != null) query.append("use_regex", String(args.use_regex)) if (args.exclude_string != null) query.append("exclude_string", String(args.exclude_string)) 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 (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 (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/logs${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(createApiLogsResponse, await res.json()) };