UNPKG

@trifrost/core

Version:

Blazingly fast, runtime-agnostic server framework for modern edge and node environments

137 lines (136 loc) 4.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Route = void 0; const constants_1 = require("../types/constants"); const _RateLimit_1 = require("../modules/RateLimit/_RateLimit"); const util_1 = require("./util"); class Route { /* Array of middleware for this route */ #middleware = []; /* Route Methods */ #routes = Object.create(null); /* Configured Rate limit instance from the app */ #rateLimit = null; /* Configured body parser from the parent router */ #bodyParser = null; constructor(options) { this.#rateLimit = options.rateLimit; this.#bodyParser = options.bodyParser; } /** * Returns the built routes */ get stack() { const acc = []; for (const method in this.#routes) { const el = this.#routes[method]; acc.push({ methods: [method], handler: el.handler, bodyParser: el.bodyParser, middleware: el.middleware, }); } return acc; } /** * Attach middleware to this route */ use(val) { if (!(0, util_1.isValidMiddleware)(val)) throw new Error('TriFrostRoute@use: Handler is expected'); this.#middleware.push(val); return this; } /** * Attach a limit middleware to this route */ limit(limit) { if (!this.#rateLimit) throw new Error('TriFrostRoute@limit: RateLimit is not configured on App'); if (!(0, util_1.isValidLimit)(limit)) throw new Error('TriFrostRoute@limit: Invalid limit'); this.use((0, _RateLimit_1.limitMiddleware)(this.#rateLimit, limit)); return this; } /** * Configure body parser options for this route */ bodyParser(options) { if (!(0, util_1.isValidBodyParser)(options)) throw new Error('TriFrostRoute@bodyParser: Invalid bodyparser'); this.#bodyParser = options; return this; } /** * Configure a HTTP Get route */ get(handler) { if (!(0, util_1.isValidHandler)(handler)) throw new Error('TriFrostRoute@get: Invalid handler'); this.#routes[constants_1.HttpMethods.GET] = { handler, bodyParser: this.#bodyParser, middleware: [...this.#middleware], }; this.#routes[constants_1.HttpMethods.HEAD] = { handler, bodyParser: this.#bodyParser, middleware: [...this.#middleware], }; return this; } /** * Configure a HTTP Post route */ post(handler) { if (!(0, util_1.isValidHandler)(handler)) throw new Error('TriFrostRoute@post: Invalid handler'); this.#routes[constants_1.HttpMethods.POST] = { handler, bodyParser: this.#bodyParser, middleware: [...this.#middleware], }; return this; } /** * Configure a HTTP Put route */ put(handler) { if (!(0, util_1.isValidHandler)(handler)) throw new Error('TriFrostRoute@put: Invalid handler'); this.#routes[constants_1.HttpMethods.PUT] = { handler, bodyParser: this.#bodyParser, middleware: [...this.#middleware], }; return this; } /** * Configure a HTTP Patch route */ patch(handler) { if (!(0, util_1.isValidHandler)(handler)) throw new Error('TriFrostRoute@patch: Invalid handler'); this.#routes[constants_1.HttpMethods.PATCH] = { handler, bodyParser: this.#bodyParser, middleware: [...this.#middleware], }; return this; } /** * Configure a HTTP Delete route */ del(handler) { if (!(0, util_1.isValidHandler)(handler)) throw new Error('TriFrostRoute@del: Invalid handler'); this.#routes[constants_1.HttpMethods.DELETE] = { handler, bodyParser: this.#bodyParser, middleware: [...this.#middleware], }; return this; } } exports.Route = Route;