tspace-spear
Version:
tspace-spear is a lightweight API framework for Node.js that is fast and highly focused on providing the best developer experience. It utilizes the native HTTP server
128 lines • 4.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Router = void 0;
class Router {
_routes = [];
get routes() {
return this._routes;
}
/**
* The 'groups' method is used to add the request handler to the router for 'GET' 'POST' 'PUT' 'PATCH' 'DELETE' methods.
*
* @param {string} prefix
* @param {Router} router
* @returns {void}
*/
groups(prefix, router) {
const routes = router(new Router());
for (const route of routes._routes) {
route.path = `${prefix}${route.path}`.replace(/^\/+/, '/');
this._routes.push(route);
}
}
/**
* The 'get' method is used to add the request handler to the router for the 'GET' method.
*
* @param {string} path
* @callback {...Function[]} handlers of the middlewares
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
* @property {Function} next - go to next function
* @returns {this}
*/
get(path, ...handlers) {
this._routes.push({
path,
method: 'get',
handlers
});
return this;
}
/**
* The 'post' method is used to add the request handler to the router for the 'POST' method.
*
* @param {string} path
* @callback {...Function[]} handlers of the middlewares
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
* @property {Function} next - go to next function
* @returns {this}
*/
post(path, ...handlers) {
this._routes.push({
path,
method: 'post',
handlers
});
return this;
}
/**
* The 'put' method is used to add the request handler to the router for the 'PUT' method.
*
* @param {string} path
* @callback {...Function[]} handlers of the middlewares
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
* @property {Function} next - go to next function
* @returns {this}
*/
put(path, ...handlers) {
this._routes.push({
path,
method: 'put',
handlers
});
return this;
}
/**
* The 'patch' method is used to add the request handler to the router for the 'PATCH' method.
*
* @param {string} path
* @callback {...Function[]} handlers of the middlewares
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
* @property {Function} next - go to next function
* @returns {this}
*/
patch(path, ...handlers) {
this._routes.push({
path,
method: 'patch',
handlers
});
return this;
}
/**
* The 'delete' method is used to add the request handler to the router for the 'DELETE' method.
*
* @param {string} path
* @callback {...Function[]} handlers of the middlewares
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
* @property {Function} next - go to next function
* @returns {this}
*/
delete(path, ...handlers) {
this._routes.push({
path,
method: 'delete',
handlers
});
return this;
}
/**
* The 'all' method is used to add the request handler to the router for 'GET' 'POST' 'PUT' 'PATCH' 'DELETE' methods.
*
* @param {string} path
* @callback {...Function[]} handlers of the middlewares
* @property {object} ctx - context { req , res , query , params , cookies , files , body}
* @property {function} next - go to next function
* @returns {this}
*/
all(path, ...handlers) {
this._routes.push({
path,
method: 'all',
handlers
});
return this;
}
}
exports.Router = Router;
exports.default = Router;
//# sourceMappingURL=router.js.map