UNPKG

@report-toolkit/inspector

Version:

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

130 lines (129 loc) 5.63 kB
/** * Describes a Rule * @module @report-toolkit/inspector.rule * */ /** */ /** * A Rule which can be matched against a Context */ export class Rule { /** * Applies defaults to a rule definition during `Rule` construction. * @param {Partial<RuleDefinition>} ruleDef - Raw rule definition * @returns {RuleDefinition} */ static applyDefaults(ruleDef: Partial<RuleDefinition>): RuleDefinition; /** * Operator. Given a "handler" (returned by the rule definition's `inspect` * function), normalize it into an object (since it may be just a function) * @returns {import('rxjs').OperatorFunction<RuleHandler,RuleHandlerObject>} */ static normalizeHandler(): import('rxjs').OperatorFunction<RuleHandler, RuleHandlerObject>; /** * Creates a `Rule` from a user-defined (or builtin) `RuleDefinition`, which * is the exports of a rule definition file. * @param {RuleDefinition} ruleDefinition - Rule definition * @returns {Rule} New rule */ static create(ruleDefinition: RuleDefinition): Rule; /** * Applies defaults, assigns some metadata. * @param {RuleDefinition} ruleDef */ constructor(ruleDef: RuleDefinition); /** * @type {string} */ get id(): string; get description(): any; get url(): any; get schema(): any; /** * @type {object} * @todo update with schema for 'meta' prop */ get meta(): any; /** * Lazily-created function which validates the schema itself when first * referenced, creates a config-validation function, caches it, then asserts * any user-supplied config is valid using said function. * @throws If user-supplied config is invalid. */ get validate(): Function; /** * Calls the `inspect()` function of a Rule impl, which will return one or * more "handler" functions. Note `inspect()` might return a `Promise` which * resolves to the "handler" functions. * @param {Object} [config] Optional rule-specific config * @returns {Promise<Object|Function>} */ handlers(config?: any): Promise<any | Function>; /** * Given a stream of Report objects and an optional configuration, execute the * `inspect()` function of the rule definition, which should return a "next" * handler function, or an object having handler function props `next` and * `complete`. * 1. Normalize the result of the `inspect()` so we can make assumptions about * the shape of the returned value. * 2. For each `Report` (`report`), run the `next` handler as if it returned * a `Promise`. This handler is passed the `report`, and any `Error`s * thrown are trapped. The handler may return a string ("message"), a * partial `Message` object, `Array` thereof, or a `Promise` resolving to * any of that stuff, or just `undefined` in the case of "nothing to * mention" * 3. Return values are correlated with the filepath of the report. Note that * `Report` objects may not *have* a filepath if they were not loaded from * file. * 4. Once all `Report`s have passed through the `next` handler, call the * `complete` handler. It receives no `report`, and can be used in tandem * with `next` to perform aggregation. Supports the same return values as * `next` * 5. Finally, filter out empty/falsy partial `Message`s (e.g., those without * actual `string` `message` props), and normalize the `Message` by adding * relevant metadata (`Rule` ID, user-supplied config used, default * severity, etc.) * @param {{reports: import('rxjs').Observable<import('@report-toolkit/common/src/report').Report>, config?: object}} opts * @returns {import('rxjs').Observable<import('./message').Message>} */ inspect({ reports, config }: { reports: import('rxjs').Observable<import('@report-toolkit/common/src/report').Report>; config?: object; }): import('rxjs').Observable<import('./message').Message>; /** * Given a {@link Config}, get associated rule config and create a `RuleConfig`. */ toRuleConfig(config: any): import("./rule-config.js").RuleConfig; } /** * Creates a `Rule` from a user-defined (or builtin) `RuleDefinition`, which * is the exports of a rule definition file. * @param {RuleDefinition} ruleDefinition - Rule definition * @returns {Rule} New rule */ export function createRule(ruleDefinition: RuleDefinition): Rule; export type RuleDefinition = { /** * - (schema for `meta` prop) */ meta: object; /** * - Async function which receives `Context` object * and optional configuration */ inspect: RuleDefinitionInspectFunction; /** * - Unique rule ID */ id: string; }; export type RuleDefinitionMeta = any; export type RuleDefinitionId = string; export type RuleDefinitionInspectFunction = (config?: object) => Promise<RuleHandler> | RuleHandler; export type RuleHandler = ((report: import('@report-toolkit/common').Report) => Promise<import('./message').RawMessage> | import('./message').RawMessage | void | import('./message').RawMessage[]) | RuleHandlerObject; export type RuleHandlerObject = { next: RuleHandlerFunction; complete: (() => Promise<import('./message').RawMessage> | import('./message').RawMessage | void) | null; }; export type RuleHandlerFunction = (report: import('@report-toolkit/common').Report) => Promise<import('./message').RawMessage> | import('./message').RawMessage | void | import('./message').RawMessage[];