UNPKG

nyro

Version:

A simple and effective promise-based HTTP & HTTP/2 request library that supports all HTTP methods.

58 lines (57 loc) 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var PluginManager = /** @class */ (function () { function PluginManager() { this.plugins = []; } PluginManager.prototype.use = function (plugin, notification) { if (!plugin.name) throw new Error('Plugin must have a name.'); if (this.plugins.find(function (p) { return p.name === plugin.name; })) throw new Error("Plugin with name \"".concat(plugin.name, "\" already exists.")); if (!plugin.onRequest && !plugin.onResponse && !plugin.onError) throw new Error('Plugin must have at least one method.'); if (plugin.onRequest && typeof plugin.onRequest !== 'function') throw new Error('onRequest must be a function.'); if (plugin.onResponse && typeof plugin.onResponse !== 'function') throw new Error('onResponse must be a function.'); if (plugin.onError && typeof plugin.onError !== 'function') throw new Error('onError must be a function.'); if (notification === undefined) notification = true; if (notification) console.log("\u001B[95;5;5mPlugin\u001B[0m \u001B[38;5;119m'".concat(plugin.name, "'\u001B[0m \u001B[95;5;5mhas been successfully loaded and activated.\u001B[0m")); this.plugins.push(plugin); }; ; PluginManager.prototype.applyOnRequest = function (options) { for (var _i = 0, _a = this.plugins; _i < _a.length; _i++) { var plugin = _a[_i]; if (plugin.onRequest) { options = plugin.onRequest(options) || options; } } return options; }; PluginManager.prototype.applyOnResponse = function (response) { for (var _i = 0, _a = this.plugins; _i < _a.length; _i++) { var plugin = _a[_i]; if (plugin.onResponse) { response = plugin.onResponse(response) || response; } } return response; }; PluginManager.prototype.applyOnError = function (error) { for (var _i = 0, _a = this.plugins; _i < _a.length; _i++) { var plugin = _a[_i]; if (plugin.onError) { error = plugin.onError(error) || error; } } return error; }; return PluginManager; }()); exports.default = PluginManager; ;