@trifrost/core
Version:
Blazingly fast, runtime-agnostic server framework for modern edge and node environments
133 lines (132 loc) • 3.96 kB
JavaScript
import { HttpMethods } from '../types/constants';
import { limitMiddleware } from '../modules/RateLimit/_RateLimit';
import { isValidHandler, isValidLimit, isValidMiddleware, isValidBodyParser } from './util';
export 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 (!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 (!isValidLimit(limit))
throw new Error('TriFrostRoute@limit: Invalid limit');
this.use(limitMiddleware(this.#rateLimit, limit));
return this;
}
/**
* Configure body parser options for this route
*/
bodyParser(options) {
if (!isValidBodyParser(options))
throw new Error('TriFrostRoute@bodyParser: Invalid bodyparser');
this.#bodyParser = options;
return this;
}
/**
* Configure a HTTP Get route
*/
get(handler) {
if (!isValidHandler(handler))
throw new Error('TriFrostRoute@get: Invalid handler');
this.#routes[HttpMethods.GET] = {
handler,
bodyParser: this.#bodyParser,
middleware: [...this.#middleware],
};
this.#routes[HttpMethods.HEAD] = {
handler,
bodyParser: this.#bodyParser,
middleware: [...this.#middleware],
};
return this;
}
/**
* Configure a HTTP Post route
*/
post(handler) {
if (!isValidHandler(handler))
throw new Error('TriFrostRoute@post: Invalid handler');
this.#routes[HttpMethods.POST] = {
handler,
bodyParser: this.#bodyParser,
middleware: [...this.#middleware],
};
return this;
}
/**
* Configure a HTTP Put route
*/
put(handler) {
if (!isValidHandler(handler))
throw new Error('TriFrostRoute@put: Invalid handler');
this.#routes[HttpMethods.PUT] = {
handler,
bodyParser: this.#bodyParser,
middleware: [...this.#middleware],
};
return this;
}
/**
* Configure a HTTP Patch route
*/
patch(handler) {
if (!isValidHandler(handler))
throw new Error('TriFrostRoute@patch: Invalid handler');
this.#routes[HttpMethods.PATCH] = {
handler,
bodyParser: this.#bodyParser,
middleware: [...this.#middleware],
};
return this;
}
/**
* Configure a HTTP Delete route
*/
del(handler) {
if (!isValidHandler(handler))
throw new Error('TriFrostRoute@del: Invalid handler');
this.#routes[HttpMethods.DELETE] = {
handler,
bodyParser: this.#bodyParser,
middleware: [...this.#middleware],
};
return this;
}
}