@sigiljs/sigil
Version:
TypeScript-first Node.js HTTP framework offering schema-driven routing, modifier-based middleware, plugin extensibility, and flexible response templating
150 lines (149 loc) • 4.24 kB
JavaScript
import n from "./modifier/attach-modifier-context.mjs";
import r from "../utils/make-log.mjs";
class u {
/**
* Initializes the RouteCore.
*
* @param modifiers array of modifier constructors to apply, or undefined.
* @param $pathfinder underlying pathfinder router instance.
* @param $options optional router settings (excluding modifiers).
* @protected
*/
constructor(t, i, s) {
this.$pathfinder = i, this.__$options = s, this.logger = r.bind({}, s?.debug), t && (this.$modifierConstructors = t, this.initializeModifiers());
}
/**
* Internal container for route validation schemas.
* Populated by route definitions.
* @internal
*/
__$schemas = {};
/**
* Bound logger function configured with route-level debug options.
*/
logger;
/**
* Router-level configuration options (e.g., debug settings).
*/
__$options;
__$sigil;
/**
* Reference to the root Route when chaining definitions.
* Used to aggregate child request metadata.
* @protected
*/
__initialParent;
/**
* Map of registered request descriptors for this router.
* Keyed by a unique identifier.
* @protected
*/
$registeredRequests = /* @__PURE__ */ new Map();
/**
* Instantiated middleware modifiers for this router.
* @protected
*/
$modifierInstances = [];
/**
* @protected
*/
$modifierConstructors = [];
/**
* Callback invoked whenever the router's structure is updated.
* @protected
*/
$updateCallback;
/**
* Map of mounted child routers by mount path.
* @private
*/
$mounted = /* @__PURE__ */ new Map();
/**
* Retrieve route options
*/
get routeOptions() {
return this.__$options;
}
/**
* Internal accessor for the underlying pathfinder router.
* @internal
*/
get __$pathfinder() {
return this.$pathfinder;
}
/**
* Accessor for all registered request descriptors,
* including those from mounted child routers with full paths.
*/
get exportRequests() {
const t = Array.from(this.$mounted.entries()).flatMap(
([i, s]) => s.exportRequests.map((e) => ({
...e,
path: i + e.path
}))
);
return [
...this.$registeredRequests.values(),
...t
];
}
/**
* Mounts a child router at the specified sub-path.
* Propagates update callbacks to maintain global request metadata.
*
* @param path sub-path at which to mount the child router.
* @param route child Route instance.
*/
mount(t, i) {
this.$mounted.set(t, i), this.__$pathfinder.mount(t, i.__$pathfinder), this.$updateCallback && i.__$connectToSigil(
this.__$sigil,
() => this.$updateCallback?.(),
this.__$options
), this.$updateCallback?.();
}
/**
* Connects the router to the Sigil framework internals.
* Merges new router options and registers the structure update callback.
*
* @param sigil sigil instance
* @param updateCallback callback to invoke on structural changes.
* @param optionsRequest partial router options to merge (excluding modifiers).
* @internal
*/
__$connectToSigil(t, i, s) {
if (this.$updateCallback = i, this.__$sigil = t, this.initializeModifiers(), !!s) {
this.__$options || (this.__$options = {});
for (const [e, o] of Object.entries(s))
e !== "middleware" && this.__$options[e] === void 0 && (this.__$options[e] = o);
}
}
/**
* Applies all middleware modifiers to the incoming client request.
* Merges each modifier's output into the request object.
*
* @param req - The parsed client request to modify.
* @returns The modified request with merged modifier payloads.
* @protected
*/
async $injectModifier(t) {
let i = Object.assign({}, t);
for (const s of this.$modifierInstances) {
const e = s.onRequest(t), o = e instanceof Promise ? await e : e;
i = { ...i, ...o };
}
return i;
}
/**
* Initialize or re-initialize modifier instances
* @private
*/
initializeModifiers() {
this.$modifierInstances = this.$modifierConstructors.map((t) => (n(t, {
sigilApi: this.__$sigil,
debugOpts: this.routeOptions?.debug || {}
}), new t()));
}
}
export {
u as default
};