Style improvements (#2475)
* HooksRouter is enabled by default * Adds middleswares on PromiseRouter, fixes #2410 * Move testing line to helper * Modernize middlewares.js * Moves DB uniqueness initialization to DBController, modernize * Moves testing related code to spec folder * remove unused _removeHook function * Adds tests, docs for Analytics and improvements * nit * moves back TestUtils
This commit is contained in:
@@ -23,6 +23,7 @@ export default class PromiseRouter {
|
||||
// location: optional. a location header
|
||||
constructor(routes = [], appId) {
|
||||
this.routes = routes;
|
||||
this.middlewares = [];
|
||||
this.appId = appId;
|
||||
this.mountRoutes();
|
||||
}
|
||||
@@ -38,6 +39,10 @@ export default class PromiseRouter {
|
||||
}
|
||||
};
|
||||
|
||||
use(middleware) {
|
||||
this.middlewares.push(middleware);
|
||||
}
|
||||
|
||||
route(method, path, ...handlers) {
|
||||
switch(method) {
|
||||
case 'POST':
|
||||
@@ -107,47 +112,17 @@ export default class PromiseRouter {
|
||||
|
||||
// Mount the routes on this router onto an express app (or express router)
|
||||
mountOnto(expressApp) {
|
||||
for (var route of this.routes) {
|
||||
switch(route.method) {
|
||||
case 'POST':
|
||||
expressApp.post(route.path, makeExpressHandler(this.appId, route.handler));
|
||||
break;
|
||||
case 'GET':
|
||||
expressApp.get(route.path, makeExpressHandler(this.appId, route.handler));
|
||||
break;
|
||||
case 'PUT':
|
||||
expressApp.put(route.path, makeExpressHandler(this.appId, route.handler));
|
||||
break;
|
||||
case 'DELETE':
|
||||
expressApp.delete(route.path, makeExpressHandler(this.appId, route.handler));
|
||||
break;
|
||||
default:
|
||||
throw 'unexpected code branch';
|
||||
}
|
||||
}
|
||||
this.routes.forEach((route) => {
|
||||
let method = route.method.toLowerCase();
|
||||
let handler = makeExpressHandler(this.appId, route.handler);
|
||||
let args = [].concat(route.path, this.middlewares, handler);
|
||||
expressApp[method].apply(expressApp, args);
|
||||
});
|
||||
return expressApp;
|
||||
};
|
||||
|
||||
expressApp() {
|
||||
var expressApp = express();
|
||||
for (var route of this.routes) {
|
||||
switch(route.method) {
|
||||
case 'POST':
|
||||
expressApp.post(route.path, makeExpressHandler(this.appId, route.handler));
|
||||
break;
|
||||
case 'GET':
|
||||
expressApp.get(route.path, makeExpressHandler(this.appId, route.handler));
|
||||
break;
|
||||
case 'PUT':
|
||||
expressApp.put(route.path, makeExpressHandler(this.appId, route.handler));
|
||||
break;
|
||||
case 'DELETE':
|
||||
expressApp.delete(route.path, makeExpressHandler(this.appId, route.handler));
|
||||
break;
|
||||
default:
|
||||
throw 'unexpected code branch';
|
||||
}
|
||||
}
|
||||
return expressApp;
|
||||
expressRouter() {
|
||||
return this.mountOnto(express.Router());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user