source: node_modules/@swagger-api/apidom-reference/cjs/util/plugins.cjs@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4exports.__esModule = true;
5exports.run = exports.filter = void 0;
6var _ramdaAdjunct = require("ramda-adjunct");
7var _PluginError = _interopRequireDefault(require("../errors/PluginError.cjs"));
8/**
9 * Filters the given plugins, returning only the ones return `true` for the given method.
10 */
11const filter = async (method, file, plugins) => {
12 const pluginResults = await Promise.all(plugins.map((0, _ramdaAdjunct.invokeArgs)([method], [file])));
13 return plugins.filter((plugin, index) => pluginResults[index]);
14};
15
16/**
17 * Runs the specified method of the given plugins, in order,
18 * until one of them returns a successful result.
19 * Each method can return a synchronous value or a Promise.
20 * If the promise resolves successfully then the result
21 * is immediately returned and no further plugins are called.
22 * If the promise rejects then the next plugin is called.
23 * If ALL plugins fail, then the last error is thrown.
24 */
25exports.filter = filter;
26const run = async (method, parameters, plugins) => {
27 let lastError;
28 for (const plugin of plugins) {
29 try {
30 // @ts-ignore
31 const result = await plugin[method].call(plugin, ...parameters); // eslint-disable-line no-await-in-loop
32 return {
33 plugin,
34 result
35 };
36 } catch (error) {
37 lastError = new _PluginError.default('Error while running plugin', {
38 cause: error,
39 plugin
40 });
41 }
42 }
43 return Promise.reject(lastError);
44};
45exports.run = run;
Note: See TracBrowser for help on using the repository browser.