@sigiljs/sigil
Version:
TypeScript-first Node.js HTTP framework offering schema-driven routing, modifier-based middleware, plugin extensibility, and flexible response templating
114 lines (113 loc) • 3.72 kB
JavaScript
import { Pathfinder as i } from "@sigiljs/pathfinder";
import { seal as s } from "@sigiljs/seal";
import n from "./route-requests.mjs";
class p extends n {
/**
* Creates a new Route instance with optional configuration.
* Instantiates the underlying Pathfinder router and applies modifiers.
*
* @param options configuration options for the router.
*/
constructor(e) {
const t = new i(), a = e?.modifiers ?? [];
super(a, t, e);
}
/**
* Registers a validation schema for the request body of the next handler.
* After calling this, GET routes will be disabled until a new router clone is created.
*
* @param schema object schema defining the expected request body.
* @param meta optional metadata (e.g., name, description) for documentation.
* @returns temporary cloned router with the body schema applied (without `get` method if appropriate).
*/
body(e, t) {
let a = Array.isArray(e) ? e[0] : e, r = Array.isArray(e) && e[1] ? e[1] : t;
return this.$cloneWithSchema("body", a, r);
}
/**
* Registers a validation schema for the request headers of the next handler.
*
* @param schema object schema defining the expected headers.
* @returns temporary cloned router with the headers schema applied.
*/
headers(e) {
let t = Array.isArray(e) ? e[0] : e;
return this.$cloneWithSchema("headers", t);
}
/**
* Registers a validation schema for the query parameters of the next handler.
*
* @param schema object schema defining the expected query parameters.
* @returns temporary cloned router with the query schema applied.
*/
query(e) {
let t = Array.isArray(e) ? e[0] : e;
return this.$cloneWithSchema("query", t);
}
/**
* Registers a validation schema for the path parameters of the next handler.
*
* @param schema object schema defining the expected path parameters.
* @returns temporary cloned router with the params schema applied.
*/
params(e) {
return this.$cloneWithSchema("params", Array.isArray(e) ? e[0] : e);
}
/**
* Internal method that clones the current router instance and applies a new schema.
* Used to isolate schema settings per handler registration.
*
* @param key schema type key ("body", "headers", "query", or "params").
* @param schema validation schema object.
* @param meta optional metadata to attach to the schema.
* @returns cloned Route instance with the updated schema.
* @private
*/
$cloneWithSchema(e, t, a) {
const r = this.$applyMetadata(s.object(t), a), o = Object.assign(Object.create(Object.getPrototypeOf(this)), this);
return o.__initialParent = this.__initialParent ?? this, o.__$schemas = {
...this.__$schemas,
[e]: r
}, o;
}
/**
* Internal helper to attach metadata (name, description, example, etc.)
* to a given schema.
*
* @param schema ObjectSchema to which metadata will be applied.
* @param meta partial metadata properties to assign.
* @returns schema instance with metadata applied.
* @private
*/
$applyMetadata(e, t) {
if (!t) return e;
for (const [a, r] of Object.entries(t))
switch (a) {
case "name":
e.name(r);
break;
case "description":
e.description(r);
break;
case "example":
e.example(r);
break;
case "default":
e.default(r);
break;
case "deprecated":
e.deprecated;
break;
case "externalDocs":
e.externalDocs(r);
break;
case "allowUnknown":
r === !0 && e.loose;
break;
}
return e;
}
}
export {
p as default
};