Skip to content

Commit

Permalink
feat: Create the Router module. Module that offers route api methods …
Browse files Browse the repository at this point in the history
…and middleware use interface

The Router modules provides the needed api methods, for working with request routing
  • Loading branch information
teclone committed Jul 25, 2018
1 parent c75db10 commit 48988c4
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/modules/Router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import Util from './Util.js';
import RouteWrapper from './RouteWrapper.js';

export default class Router {

/**
* creates router
*@param {boolean} [inheritMiddlewares=true] - boolean value indicating if parent
* middlewares should be inherited. defaults to true
*/
constructor(inheritMiddlewares) {
this.routes = {
options: [],
head: [],
get: [],
post: [],
put:[],
delete: [],
all: []
};
this.middlewares = [];
this.inheritMiddlewares = inheritMiddlewares === false? false : true;
}

/**
* return object identity
*/
get [Symbol.toStringTag]() {
return 'Router';
}

/**
* performs route rules for http OPTIONS method verb
*@param {string} url - the route url
*@param {Function} callback - callback function
*@param {Object} [options] - optional configuration options
*/
options(url, callback, options) {
this.routes.options.push([url, callback, options]);
}

/**
* performs route rules for http HEAD method verb
*@param {string} url - the route url
*@param {Function} callback - callback function
*@param {Object} [options] - optional configuration options
*/
head(url, callback, options) {
this.routes.head.push([url, callback, options]);
}

/**
* performs route rules for http GET method verb
*@param {string} url - the route url
*@param {Function} callback - callback function
*@param {Object} [options] - optional configuration options
*/
get(url, callback, options) {
this.routes.get.push([url, callback, options]);
}

/**
* performs route rules for http POST method verb
*@param {string} url - the route url
*@param {Function} callback - callback function
*@param {Object} [options] - optional configuration options
*/
post(url, callback, options) {
this.routes.post.push([url, callback, options]);
}

/**
* performs route rules for http PUT method verb
*@param {string} url - the route url
*@param {Function} callback - callback function
*@param {Object} [options] - optional configuration options
*/
put(url, callback, options) {
this.routes.put.push([url, callback, options]);
}

/**
* performs route rules for http DELETE method verb
*@param {string} url - the route url
*@param {Function} callback - callback function
*@param {Object} [options] - optional configuration options
*/
delete(url, callback, options) {
this.routes.delete.push([url, callback, options]);
}

/**
* performs route rules for all http method verbs
*@param {string} url - the route url
*@param {Function} callback - callback function
*@param {Object} [options] - optional configuration options
*/
all(url, callback, options) {
this.routes.all.push([url, callback, options]);
}

/**
* returns a route wrapper for the given url
*@returns {RouteWrapper}
*/
route(url) {
return new RouteWrapper(this, url);
}

/**
* use a middleware
*@param {Function} middleware - the middleware function
*/
use(middleware) {
if (Util.isCallable(middleware))
this.middlewares.push(middleware);
}
}

0 comments on commit 48988c4

Please sign in to comment.