@sigiljs/sigil
Version:
TypeScript-first Node.js HTTP framework offering schema-driven routing, modifier-based middleware, plugin extensibility, and flexible response templating
69 lines (68 loc) • 1.98 kB
JavaScript
import { SigilMiddleware as r } from "./misc/sigil-middleware.mjs";
import s from "./sigil-core.mjs";
class d extends s {
/**
* 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 t = this.$plugins.get(e.name);
return t || 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 }
}), t;
}
/**
* 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, t) {
const i = this.$plugins.get(e.name);
return i ? t(i) : null;
}
/**
* Adds a 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 t = crypto.randomUUID();
this.logger({
message: `Registering middleware with id #${t}`,
level: "info",
module: "registry",
json: { milestone: "middleware", ok: !0, id: t }
});
const i = new r(
e,
() => {
this.$middlewares.delete(t);
},
t
);
return this.$middlewares.set(t, e), i;
}
}
export {
d as default
};