@sigiljs/sigil
Version:
TypeScript-first Node.js HTTP framework offering schema-driven routing, modifier-based middleware, plugin extensibility, and flexible response templating
78 lines (77 loc) • 2.27 kB
JavaScript
import { SigilMiddleware as s } from "./misc/sigil-middleware.mjs";
import r from "./sigil-core.mjs";
class a extends r {
/**
* Constructs the SigilPluginSystem with optional configuration.
*
* @param options partial SigilOptions for core initialization.
*/
constructor(e) {
super(e);
}
/**
* Retrieves a registered plugin instance by its constructor.
* Logs an error if the plugin has not been loaded.
*
* @param plugin plugin constructor to retrieve.
* @returns plugin instance, or undefined if not loaded.
*/
plugin(e) {
const i = this.$plugins.get(e.name);
return i || this.logger({
message: `Found call to the plugin that was not loaded: ${e.name}`,
level: "error",
module: "registry",
json: { milestone: "call", ok: !1, name: e.name }
}), i;
}
/**
* Checks if a specific plugin is registered within the system.
*
* @param {SigilPluginConstructor} plugin constructor of the plugin to check.
* @return {boolean} true if the plugin is registered, otherwise false.
*/
hasPlugin(e) {
return this.$plugins.has(e.name);
}
/**
* Executes a callback with the plugin instance if it exists.
* Returns null if the plugin is not registered.
*
* @param plugin plugin constructor to use.
* @param callback function to execute with the plugin.
* @returns result of the callback or null.
*/
withPlugin(e, i) {
const t = this.$plugins.get(e.name);
return t ? i(t) : null;
}
/**
* Adds global middleware to the Sigil framework.
* Generates a unique ID for the middleware and logs its registration.
* Returns a SigilMiddleware instance that can be used to unregister.
*
* @param callback the middleware function to register.
* @returns SigilMiddleware object for managing the middleware lifecycle.
*/
addMiddleware(e) {
const i = crypto.randomUUID();
this.logger({
message: `Registering middleware with id #${i}`,
level: "info",
module: "registry",
json: { milestone: "middleware", ok: !0, id: i }
});
const t = new s(
e,
() => {
this.$middlewares.delete(i);
},
i
);
return this.$middlewares.set(i, e), t;
}
}
export {
a as default
};