forked from tedeh/jayson
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmethod.js
128 lines (104 loc) · 3.47 KB
/
method.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
const isArray = require('lodash/isArray');
const isPlainObject = require('lodash/isPlainObject');
const isObject = require('lodash/isObject');
const extend = require('lodash/extend');
const keys = require('lodash/keys');
const reduce = require('lodash/reduce');
const pick = require('lodash/pick');
const toArray = require('lodash/toArray');
const toPlainObject = require('lodash/toPlainObject');
const utils = require('./utils');
/**
* @summary Constructor for a Jayson Method
* @class Method
* @param {Function} [handler] Function to set as handler
* @param {Object} [options]
* @param {Function} [options.handler] Same as separate handler
* @param {Boolean} [options.useContext=false] When true, the handler expects a context object
* @param {Array|Object} [options.params] Defines params that the handler accepts
*/
const Method = function(handler, options) {
if(!(this instanceof Method)) {
return new Method(handler, options);
}
// only got passed options
if(isPlainObject(handler)) {
options = handler;
handler = null;
}
const defaults = {
useContext: false,
};
options = options || {};
this.options = utils.merge(defaults, options);
this.handler = handler || options.handler;
};
module.exports = Method;
/**
* @summary Returns the handler function associated with this method
* @return {Function}
*/
Method.prototype.getHandler = function() {
return this.handler;
};
/**
* @summary Sets the handler function associated with this method
* @param {Function} handler
*/
Method.prototype.setHandler = function(handler) {
this.handler = handler;
};
/**
* @summary Prepare parameters for the method handler
* @private
*/
Method.prototype._getHandlerParams = function(params) {
const options = this.options;
const isObjectParams = !isArray(params) && isObject(params) && params;
const isArrayParams = isArray(params);
switch(true) {
// handler always gets an array
case options.params === Array:
return isArrayParams ? params : toArray(params);
// handler always gets an object
case options.params === Object:
return isObjectParams ? params : toPlainObject(params);
// handler gets a list of defined properties that should always be set
case isArray(options.params): {
const undefinedParams = reduce(options.params, function(undefinedParams, key) {
undefinedParams[key] = undefined;
return undefinedParams;
}, {});
return extend(undefinedParams, pick(params, keys(params)));
}
// handler gets a map of defined properties and their default values
case isPlainObject(options.params):
return extend({}, options.params, pick(params, keys(params)));
// give params as is
default:
return params;
}
};
/**
* @summary Executes this method in the context of a server
* @param {Server} server
* @param {Array|Object} requestParams
* @param {Object} [context]
* @param {Function} callback
*/
Method.prototype.execute = function(server, requestParams, context, callback) {
if(typeof(context) === 'function') {
callback = context;
context = {};
}
if(!context) {
context = {};
}
// when useContext is true, the handler gets a context object every time
const useContext = Boolean(this.options.useContext);
const handler = this.getHandler();
const params = this._getHandlerParams(requestParams);
const args = useContext ? [params, context, callback] : [params, callback];
return handler.call(server, ...args);
};