UNPKG

@report-toolkit/core

Version:

See docs at [https://ibm.github.io/report-toolkit](https://ibm.github.io/report-toolkit)

355 lines (354 loc) 12.4 kB
/** * [RxJS](https://rxjs.dev) `Observable`-based API. Used interally, but can also be used by consumers. * @module @report-toolkit/core.observable * */ /** */ /** * * @param {object} [config] - Raw rule configuration * @hidden * @returns {import('rxjs').OperatorFunction<import('@report-toolkit/inspector/src/rule').RuleDefinition,import('@report-toolkit/inspector/src/rule-config').RuleConfig>} */ export function toRuleConfig(config?: object): import('rxjs').OperatorFunction<import('@report-toolkit/inspector/src/rule').RuleDefinition, import('@report-toolkit/inspector/src/rule-config').RuleConfig>; /** * Returns the difference between two reports. * * Example: * * ```js * const {diff} = require('@report-toolkit/core').observable; * * const report1 = process.report.getReport(); * const report2 = process.report.getReport(); * * diff(report1, report2, { * filterProperties: ['header', 'javascriptStack', 'nativeStack'], * showSecretsUnsafe: false * }).subscribe(({op, path, newValue, oldValue}) => { * console.log(`[${op}] <${path}> ${oldValue} => ${newValue}`); * }) * ``` * @param {import('@report-toolkit/common/src/report').ReportLike|import('rxjs').Observable<import('@report-toolkit/common/src/report').ReportLike>} report1 - First report to diff * @param {import('@report-toolkit/common/src/report').ReportLike|import('rxjs').Observable<import('@report-toolkit/common/src/report').ReportLike>} report2 - Second report to diff * @param {Partial<DiffOptions>} [opts] Options * @returns {import('rxjs').Observable<DiffResult>} Results, one per difference * @todo support JSON reports */ export function diff(report1: import('@report-toolkit/common/src/report').ReportLike | import('rxjs').Observable<import('@report-toolkit/common/src/report').ReportLike>, report2: import('@report-toolkit/common/src/report').ReportLike | import('rxjs').Observable<import('@report-toolkit/common/src/report').ReportLike>, opts?: Partial<DiffOptions>): import('rxjs').Observable<DiffResult>; /** * Inspect one or more reports, running rules against each. Resolves with an array of zero or more {@link @report-toolkit/inspector.message.Message|Messages}. * * Example: * * ```js * const {inspect} = require('@report-toolkit/core').observable * * const report = process.report.getReport(); * inspect(report, { * severity: 'info', * sort: true, * sortDirection: 'asc', * sortField: 'header.dumpEventTimestamp', * showSecretsUnsafe: false, * ruleConfig: { * 'long-timeout': { * timeout: '2s' * } * } * }).subscribe({message, filename} => { * console.log(`${filename}: ${message}`); * }); * ``` * @param {import('@report-toolkit/common/src/report').ReportLike|import('rxjs').Observable<import('@report-toolkit/common/src/report').ReportLike>} reports - One or more Reports * @param {Partial<InspectOptions>} [opts] - Options * @returns {import('rxjs').Observable<import('@report-toolkit/inspector/src/message').Message>} */ export function inspect(reports: import('@report-toolkit/common/src/report').ReportLike | import('rxjs').Observable<import('@report-toolkit/common/src/report').ReportLike>, opts?: Partial<InspectOptions>): import('rxjs').Observable<import('@report-toolkit/inspector/src/message').Message>; /** * Emits normalized config objects from raw config objects. Only a single input config should be necessary. * * Example: * * ```js * const {loadConfig} = require('@report-toolkit/core').observable; * * // or require('./path/to/.rtkrc.js') * const rawConfig = [ * 'report-toolkit:recommended', * { * rules: { * 'long-timeout': { * timeout: '2s' * } * } * } * ]; * * loadConfig(rawConfig).subscribe(normalizedConfig => { * // `normalizedConfig` contains contents of "recommended" settings, * // with our override of custom rule config * }); * ``` * @param {object} config - Raw config object * @todo ALWAYS load builtin plugin(s) * @todo Document config shape * @returns {import('rxjs').Observable<Config>} Normalized config object(s) */ export function loadConfig(config: object): import('rxjs').Observable<Config>; /** * Given a list of transformer IDs, create an `Observable` which emits {@link TransformerBlueprint} objects. Output should be piped to {@link transform}. * * Example: * * ```js * const {fromTransformerChain} = require('@report-toolkit/core').observable; * * fromTransformerChain(['filter', 'csv'], { * transformers: { * filter: {include: 'header'}, * csv: {flatten: true} * } * }); // pipe to transform() * ``` * @param {string[]|string} transformerIds - List of Transformer IDs * @param {Partial<Config>} [config] - Normalized config object * @returns {import('rxjs').Observable<TransformerBlueprint>} */ export function fromTransformerChain(transformerIds: string[] | string, config?: Partial<Config>): import('rxjs').Observable<TransformerBlueprint>; /** * Run `source` through chain of one or more transformers. Pipe {@link fromTransformerChain} into this. * Performs validation before piping. * While most other functions here will automatically convert a raw report into a `Report` instance for further processing, this one does not (since transformers don't necessarily accept them). You'll need to do this manually, as seen in the below example. * If the final transformer does not output the desired `endType`, the `defaultTransformer` will be appended to the chain; otherwise it is ignored. * * Example: * * ```js * const {fromTransformerChain, transform, toReportFromObject} = require('@report-toolkit/core').observable; * * const report$ = toReportFromObject(process.report.getReport()); * fromTransformerChain(['filter', 'csv'], { * transformers: { * filter: {include: 'header'}, * csv: {flatten: true} * } * }).pipe(transform(report$)).subscribe(line => { * console.log(line); * }); * ``` * @param {import('rxjs').Observable<any>} source - Source data to transform. Objects, {@link @report-toolkit/common.report.Report|Reports}, etc. * @param {Partial<TransformOptions>} [opts] - Options for the transformation * @returns {import('rxjs').OperatorFunction<TransformerBlueprint,any>} Result of running `source` through the transformer chains. */ export function transform(source: import('rxjs').Observable<any>, opts?: Partial<TransformOptions>): import('rxjs').OperatorFunction<TransformerBlueprint, any>; /** * Creates a target `Observable` of {@link @report-toolkit/common.report.Report|Report} objects from a source `Observable` of plain objects (usually parsed from a JSON report). * * Example: * * ```js * const {toReportFromObject} = require('@report-toolkit/core').observable; * * const json = fs.readFileSync('./report-xxxxx.json'); * toReportFromObject(json, { * showSecretsUnsafe: false * }).subscribe(report => { * // `Report` instance with secrets redacted * }); * ``` * @param {Partial<ToReportFromObjectOptions>} [opts] - Options */ export function toReportFromObject(opts?: Partial<ToReportFromObjectOptions>): import("rxjs").OperatorFunction<any, Readonly<import("@report-toolkit/common/src/report").Report>>; /** * Get a list of rule definitions contained within registered plugins. * * Example: * * ```js * const {registeredRuleDefinitions} = require('@report-toolkit/core').observable; * * registeredRuleDefinitions().forEach(ruleDef => { * console.log(ruleDef.meta.id); * }) * ``` * @returns {import('@report-toolkit/inspector/src/rule').RuleDefinition[]} */ export function registeredRuleDefinitions(): import('@report-toolkit/inspector/src/rule').RuleDefinition[]; /** * @hidden * @todo XXX this is not the right place to load the builtins, and it's essentially redundant with {@link registeredRuleDefinitions}. * @returns {import('rxjs').Observable<import('@report-toolkit/inspector/src/rule').RuleDefinition>} */ export function fromRegisteredRuleDefinitions(): import('rxjs').Observable<import('@report-toolkit/inspector/src/rule').RuleDefinition>; /** * Register & enable a plugin. * * Example: * * ```js * const {use} = require('@report-toolkit/core').observable; * * use('some-plugin-in-node_modules').subscribe(); * * ``` * @param {string} pluginId - ID of plugin to register; a resolvable path to a module * @returns {import('rxjs').Observable<RTKPlugin>} A plugin instance, but YAGNI. */ export function use(pluginId: string): import('rxjs').Observable<RTKPlugin>; /** * De-register ("unload") all plugins. * * Example: * * ```js * const {deregisterPlugins} = require('@report-toolkit/core').observable; * * console.log(deregisterPlugins()); // `true` or `false`, depending. * ``` * @returns {boolean} `true` if plugins were cleared; `false` if none registered */ export function deregisterPlugins(): boolean; /** * Returns `true` if plugin with id `pluginId` has already been registered. * * ```js * const {isPluginRegistered} = require('@report-toolkit/core').observable; * * console.log(isPluginRegistered('my-plugin')); // `true` or `false`, depending. * ``` * @param {string} pluginId - A unique [module ID](https://nodejs.org/api/modules.html#modules_module_id) */ export function isPluginRegistered(pluginId: string): boolean; /** * Options for {@link transform}. */ export type TransformOptions = { /** * - Begin transformer chain with this type */ beginWith: string; /** * - End transformer chain with this type */ endWith: string; /** * - Default transformer */ defaultTransformer: string; /** * - Default transformer config */ defaultTransformerConfig: object; }; /** * Represents a "plugin". As of this writing, plugins may only contain rule definitions for {@link inspect}; it would make sense to add support for transformers, as well. */ export type RTKPlugin = { /** * - An array of rule definitions. */ rules: import('@report-toolkit/inspector/src/rule').RuleDefinition[] | null; }; /** * Options for {@link inspect}. */ export type InspectOptions = { /** * - Whether or not to sort output when multiple reports are provided */ sort: boolean; /** * - Filter by message severity */ severity: string; /** * - Ascending or descending */ sortDirection: "asc" | "desc"; /** * - Field to sort by; keypaths are allowed */ sortField: string; /** * - Rule configuration object */ ruleConfig: object; /** * - If `true`, do not redact secrets */ showSecretsUnsafe: boolean; }; /** * A single difference between two reports. Emitted from {@link diff}. */ export type DiffResult = { /** * - Operation */ op: "add" | "remove" | "replace"; /** * - [RFC6902](https://tools.ietf.org/html/rfc6902)-style keypath */ path: string; /** * - Value from second report (where applicable) */ value: string | boolean | number | (null | null); /** * - Value from first report (where applicable) */ oldValue: string | boolean | number | (null | null); }; /** * Options for {@link diff}. */ export type DiffOptions = { /** * - Include only these keypaths in the diff */ includeProperties: string[]; /** * - Exclude these keypaths from the diff */ excludeProperties: string[]; /** * - Just show the whole diff if `true` */ includeAll: boolean; /** * - If `true`, do not redact secrets */ showSecretsUnsafe: boolean; }; /** * A "normalized" configuration object. */ export type Config = any; /** * A pairing of a transformer ID and a configuration of that transformer, to be * ingested by {@link transform}. */ export type TransformerBlueprint = { /** * - Transformer ID */ id: string; /** * - Configuration for transformer */ config: object | null; }; /** * Options for {@link toReportFromObject}. */ export type ToReportFromObjectOptions = { /** * - If `true`, do not redact secrets */ showSecretsUnsafe: boolean; }; import { compatibleTransformers } from "../../transformers/src"; import { builtinTransformerIds } from "../../transformers/src"; export { compatibleTransformers, builtinTransformerIds };