forked from Neil-UWA/loopback-remote-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote-routing.js
74 lines (59 loc) · 1.92 KB
/
remote-routing.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
var _ = require('lodash');
var utils = require('./lib/utils.js');
var RemoteMethods = require('./lib/remote-methods.js');
module.exports = function(Model, options) {
options = options || {};
Model.on('attached', function() {
RemoteRouting(Model, options);
});
var remoteMethod = Model.remoteMethod;
Model.remoteMethod = function(name, config) {
var disable;
if (!config.hasOwnProperty('isStatic')) {
config.isStatic = true;
}
remoteMethod.call(Model, name, config);
if (options.only && options.only.length) {
disable = !_.includes(options.only, name);
}
if (options.except && options.except.length) {
disable = disable || _.includes(options.except, name);
}
if (disable) {
Model.disableRemoteMethod(name, config.isStatic);
}
}
};
//options : {only: [], except: []}
//only: only expose specified methods, disable others
//except: expose all methods, except specified ones
//symbol @ donates the method is static
function RemoteRouting(Model, options) {
options = options || {};
var methods = RemoteMethods(Model);
if (options.only && options.only.length) {
methods = _.difference(methods, options.only);
}
if (options.except && options.except.length) {
methods = _.filter(methods, function(method){
return _.includes(options.except, method);
});
}
methods.forEach(function(method){
if(Model.disableRemoteMethodByName) {
// since Model.disableRemoteMethod has deprecated in loopback 3.X
// use Model.disableRemoteMethodByName instead
if (/^@/.test(method)) {
Model.disableRemoteMethodByName(method.replace(/^@/, ''));
} else {
Model.disableRemoteMethodByName('prototype.' + method);
}
} else {
if (/^@/.test(method)) {
Model.disableRemoteMethod(method.replace(/^@/, ''), true);
} else {
Model.disableRemoteMethod(method, false);
}
}
});
}