UNPKG

@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.66 kB
import { seal as p, ValidationError as h } from "@sigiljs/seal"; import b from "../sigil/misc/sigil-responses-list.mjs"; import _ from "./route-core.mjs"; class y extends _ { /** * @param modifiers array of modifier constructors to apply. * @param pathfinder the underlying pathfinder router instance. * @param $options optional router configuration options. */ constructor(e, t, o) { super(e, t, o); } /** * Registers a GET route. * Not available if a body schema was previously applied. * * @param path URL path for the route. * @param handler request handler function. * @returns chainable methods for adding metadata (meta, description). */ get(e, t) { return this.$request("GET", e, t); } /** Registers a POST route. */ post(e, t) { return this.$request("POST", e, t); } /** Registers a PUT route. */ put(e, t) { return this.$request("PUT", e, t); } /** Registers a PATCH route. */ patch(e, t) { return this.$request("PATCH", e, t); } /** Registers a DELETE route. */ delete(e, t) { return this.$request("DELETE", e, t); } /** Registers an OPTIONS route. */ options(e, t) { return this.$request("OPTIONS", e, t); } /** Registers a TRACE route. */ trace(e, t) { return this.$request("TRACE", e, t); } /** Registers a CONNECT route. */ connect(e, t) { return this.$request("CONNECT", e, t); } /** Registers a HEAD route. */ head(e, t) { return this.$request("HEAD", e, t); } /** * Internal method to register a route with pathfinder. * Validates incoming requests against schemas if provided. * * @param method HTTP method for the route. * @param path URL path for the route. * @param handler request handler function. * @returns chainable methods for adding metadata (meta, description). * @private */ $request(e, t, o) { const r = this.__initialParent ?? this, c = { ...this.__$schemas }, $ = Object.entries(c), d = Math.random().toString(), l = (s) => `Provided request cannot be processed due to invalid ${s}`, g = Object.fromEntries( Object.entries(c).map(([s, i]) => [s, p.exportMetadataOf(i)]) ), a = { method: e, path: t, flatSchema: g, metadata: {} }; return r.$registeredRequests.set(d, a), r.$updateCallback?.(), this.logger({ message: (s) => `Registering new path @ ${e} ${s("<...>/")}${t.slice(1)}`, level: "info", module: "route", json: { milestone: "path", ok: !0, method: e, path: t } }), r.$pathfinder.register(e, t, async (s) => { const i = s, u = { params: i.params, body: i.body?.json(), query: i.query.getObject(), headers: i.headers.link }; if (!r.__$options?.debug?.validation?.skip) for (const [n, f] of $) { if (!u[n] || typeof u[n] != "object") return new h(l(n)); const m = p.validate(f, u[n]); if (m.length) { const q = r.__$options?.debug?.validation?.messages ? m.join(", ") : l(n); throw new h(q); } } return o(await r.$injectModifier(i), new b(), this.__$sigil || null); }), { /** * Adds metadata to the registered route. * @param payload partial metadata object to merge. */ meta(s) { a.metadata = { ...a.metadata, ...s }, r.$registeredRequests.set(d, a), r.$updateCallback?.(); }, /** * Shortcut to set the route description metadata. * @param payload description text. */ description(s) { this.meta({ description: s }); } }; } } export { y as default };