UNPKG

1.23 kBJavaScriptView Raw
1/**
2 * @license
3 * MOST Web Framework 2.0 Codename Blueshift
4 * Copyright (c) 2017, THEMOST LP All rights reserved
5 *
6 * Use of this source code is governed by an BSD-3-Clause license that can be
7 * found in the LICENSE file at https://themost.io/license
8 */
9var _ = require('lodash');
10/**
11 * Extends context parameters by adding the default context params that are defined on the current route, if any
12 * @class
13 * @constructor
14 * @implements MapRequestHandler
15 */
16function RouteParamsHandler() {
17 //
18}
19
20RouteParamsHandler.prototype.mapRequest = function(context, callback) {
21 if (_.isNil(context.request)) {
22 return callback();
23 }
24 else if (_.isNil(context.request.route)) {
25 return callback();
26 }
27 else {
28 var route=context.request.route;
29 //extend params
30 context.params = context.params || {};
31 if (typeof route.params === 'object' && route.params!==null) {
32 var keys = Object.keys(route.params);
33 keys.forEach(function(key) { context.params[key] = route.params[key] });
34 }
35 return callback();
36 }
37};
38
39if (typeof exports !== 'undefined') {
40 module.exports.createInstance = function() { return new RouteParamsHandler(); };
41}