1 | 'use strict';
|
2 |
|
3 | const isArray = require('lodash/isArray');
|
4 | const isPlainObject = require('lodash/isPlainObject');
|
5 | const isObject = require('lodash/isObject');
|
6 | const extend = require('lodash/extend');
|
7 | const keys = require('lodash/keys');
|
8 | const reduce = require('lodash/reduce');
|
9 | const pick = require('lodash/pick');
|
10 | const toArray = require('lodash/toArray');
|
11 | const toPlainObject = require('lodash/toPlainObject');
|
12 | const utils = require('./utils');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | const Method = function(handler, options) {
|
24 |
|
25 | if(!(this instanceof Method)) {
|
26 | return new Method(handler, options);
|
27 | }
|
28 |
|
29 |
|
30 | if(isPlainObject(handler)) {
|
31 | options = handler;
|
32 | handler = null;
|
33 | }
|
34 |
|
35 | const defaults = {
|
36 | useContext: false,
|
37 | };
|
38 |
|
39 | options = options || {};
|
40 |
|
41 | this.options = utils.merge(defaults, options);
|
42 | this.handler = handler || options.handler;
|
43 | };
|
44 |
|
45 | module.exports = Method;
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | Method.prototype.getHandler = function() {
|
52 | return this.handler;
|
53 | };
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | Method.prototype.setHandler = function(handler) {
|
60 | this.handler = handler;
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | Method.prototype._getHandlerParams = function(params) {
|
68 | const options = this.options;
|
69 |
|
70 | const isObjectParams = !isArray(params) && isObject(params) && params;
|
71 | const isArrayParams = isArray(params);
|
72 |
|
73 | switch(true) {
|
74 |
|
75 |
|
76 | case options.params === Array:
|
77 | return isArrayParams ? params : toArray(params);
|
78 |
|
79 |
|
80 | case options.params === Object:
|
81 | return isObjectParams ? params : toPlainObject(params);
|
82 |
|
83 |
|
84 | case isArray(options.params): {
|
85 | const undefinedParams = reduce(options.params, function(undefinedParams, key) {
|
86 | undefinedParams[key] = undefined;
|
87 | return undefinedParams;
|
88 | }, {});
|
89 | return extend(undefinedParams, pick(params, keys(params)));
|
90 | }
|
91 |
|
92 |
|
93 | case isPlainObject(options.params):
|
94 | return extend({}, options.params, pick(params, keys(params)));
|
95 |
|
96 |
|
97 | default:
|
98 | return params;
|
99 |
|
100 | }
|
101 |
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | Method.prototype.execute = function(server, requestParams, context, callback) {
|
112 | if(typeof(context) === 'function') {
|
113 | callback = context;
|
114 | context = {};
|
115 | }
|
116 |
|
117 | if(!context) {
|
118 | context = {};
|
119 | }
|
120 |
|
121 |
|
122 | const useContext = Boolean(this.options.useContext);
|
123 | const handler = this.getHandler();
|
124 | const params = this._getHandlerParams(requestParams);
|
125 |
|
126 | const args = useContext ? [params, context, callback] : [params, callback];
|
127 | return handler.call(server, ...args);
|
128 | };
|