frontexpress/lib/router.js

135 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-06-26 10:10:37 +00:00
import {HTTP_METHODS} from './requester';
import Middleware from './middleware';
class Route {
constructor(router, uriPart, method, middleware) {
this.router = router;
this.uriPart = uriPart;
this.method = method;
this.middleware = middleware;
this.visited = false;
2016-06-26 10:10:37 +00:00
}
get uri() {
if (this.uriPart instanceof RegExp) {
return this.uriPart;
}
if (this.router.baseUri instanceof RegExp) {
return this.router.baseUri;
}
if (this.router.baseUri && this.uriPart) {
return (this.router.baseUri.trim() + this.uriPart.trim()).replace(/\/{2,}/, '/');
}
if (this.router.baseUri) {
return this.router.baseUri.trim();
}
return this.uriPart;
}
}
export default class Router {
constructor(baseUri) {
if (baseUri) {
this.baseUri = baseUri;
}
this._routes = [];
2016-06-26 10:10:37 +00:00
}
_add(route) {
this._routes.push(route);
2016-06-26 10:10:37 +00:00
return this;
}
routes(uri, method) {
return this._routes.filter((route) => {
2016-06-26 10:10:37 +00:00
if (route.method !== method) {
return false;
}
if (!route.uri) {
return true;
}
let uriToCheck = uri;
//remove query string from uri to test
const questionMarkIndex = uriToCheck.indexOf('?');
if (questionMarkIndex >= 0) {
uriToCheck = uriToCheck.slice(0, questionMarkIndex);
}
//remove anchor from uri to test
const hashIndex = uriToCheck.indexOf('#');
if (hashIndex >= 0) {
uriToCheck = uriToCheck.slice(0, hashIndex);
}
if (route.uri instanceof RegExp) {
return uriToCheck.match(route.uri);
}
return route.uri === uriToCheck;
2016-06-26 10:10:37 +00:00
});
}
visited() {
return this._routes.filter((route) => {
return route.visited;
});
}
2016-06-26 10:10:37 +00:00
all(...args) {
if (args.length === 0) {
throw new TypeError(`use all method takes at least a middleware`);
2016-06-26 10:10:37 +00:00
}
let middleware;
if (args.length === 1) {
[middleware,] = args;
} else {
[, middleware,] = args;
}
if (!(middleware instanceof Middleware) && (typeof middleware !== 'function') ) {
throw new TypeError(`use all method takes at least a middleware`);
2016-06-26 10:10:37 +00:00
}
for (const method of Object.keys(HTTP_METHODS)) {
this[method.toLowerCase()](...args);
}
return this;
}
}
for (const method of Object.keys(HTTP_METHODS)) {
const methodName = method.toLowerCase();
Router.prototype[methodName] = function(...args) {
if (args.length === 0) {
throw new TypeError(`use ${methodName} method takes at least a middleware`);
2016-06-26 10:10:37 +00:00
}
let uri, middleware;
if (args.length === 1) {
[middleware,] = args;
} else {
[uri, middleware,] = args;
}
if (!(middleware instanceof Middleware) && (typeof middleware !== 'function') ) {
throw new TypeError(`use ${methodName} method takes at least a middleware`);
}
if (uri && this.baseUri && this.baseUri instanceof RegExp) {
throw new TypeError(`router contains a regexp cannot mix with route uri/regexp`);
2016-06-26 10:10:37 +00:00
}
this._add(new Route(this, uri, method, middleware));
return this;
}
}