-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
233 lines (193 loc) · 8.19 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
var debug = require('debug')('loopback:connector:flexirest');
var RestConnector = require('loopback-connector-rest');
var path = require('path');
var walk = require('./lib/walk');
var url = require('url');
var transformerLib = require('./lib/transformer');
/**
* Export the initialize method to loopback-datasource-juggler
* @param {DataSource} dataSource The loopback data source instance
* @param {function} [callback] The callback function
*/
exports.initialize = function initializeDataSource(dataSource, callback) {
var settings = dataSource.settings || {};
var endpointPath = settings.path || 'endpoints';
var baseURL = settings.baseURL || settings.restPath || '';
// load endpoints folder and add to operations for loopback-connector-rest
if (endpointPath) {
var endpointTemplates = walk(path.resolve(endpointPath),'json');
settings.operations = [];
endpointTemplates.forEach(function(templateFile){
debug('Found JSON file', templateFile);
var defintion = require(templateFile);
defintion.file = templateFile;
if(defintion && defintion.template && defintion.functions) {
// file is a template json file
debug('Adding template to settings.operations');
if (baseURL) {
defintion.template.url = url.resolve(baseURL, defintion.template.url);
}
settings.operations.push(defintion);
}
});
}
RestConnector.initialize(dataSource, callback);
applyTransformerPatch(dataSource);
autoloadFunctions(dataSource);
};
var applyTransformerPatch = function(dataSource) {
var dao = dataSource.connector.DataAccessObject;
var connector = dataSource.connector;
Object.keys(dao).filter(function(key){
return key !== 'invoke';
}).forEach(function(key){
var originalfn = dao[key];
debug('patched function', key);
var argsList = originalfn.accepts.map(function(param){
return param.arg;
});
var newfn = function(cb){
var self = this;
var context = {'name':key};
var cb = null;
if (arguments.length > 0
&& 'function' === typeof arguments[arguments.length - 1]) {
cb = arguments[arguments.length - 1];
}
context.parameters = {};
var argsLength = arguments.length;
Array.prototype.slice.call(arguments)
.filter(function(arg,index){
// to avoid callback being added to
// parameters
return index < (argsLength -1);
})
.forEach(function(arg,index){
context.parameters[argsList[index]] = arg;
})
function outgoingTransform(context, done) {
if(self[key].outgoingTransform) {
debug('outgoingTransform called');
var transformedOutput = null;
try {
transformedOutput = self[key].outgoingTransform(context.parameters);
if(typeof transformedOutput !== 'object')
done(new Error('transformedOutput is not an object'));
Object.keys(context.parameters).filter(function(property){
return !transformedOutput.hasOwnProperty(property);
}).forEach(function(property){
transformedOutput[property] = context.parameters[property];
});
context.parameters = transformedOutput;
done();
} catch (e) {
done(e);
}
} else {
done();
}
}
function errorHandler(err, body, response,done) {
if(self[key].errorHandler) {
debug('errorHandler called');
self[key].errorHandler(err, body, response, done);
} else {
done(err, body, response);
}
}
function incomingTransform(context, done) {
if(self[key].incomingTransform) {
debug('incomingTransform called');
try {
context.body = self[key].incomingTransform(context.body);
} catch (e) {
done(e);
}
}
done(null, context.body, context.response);
}
connector.notifyObserversAround('outgoingTransform', context, outgoingTransform, function(err) {
//TODO: fix it properly
if(err)
return cb(err);
context.parameters = argsList.map(function(property){
return context.parameters[property];
});
var callback = function(err, body, response) {
errorHandler(err,body,response,function(err,body,response){
if(err) {
cb(err, body, response);
} else {
delete context.parameters;
context.body = body;
context.response = response;
connector.notifyObserversAround('incomingTransform', context, incomingTransform, function(err, body, response){
cb(err, body, response);
});
}
});
}
context.parameters.push(callback);
originalfn.apply(this, context.parameters);
});
};
['accepts','returns','shared','http'].forEach(function(prop){
newfn[prop] = originalfn[prop];
});
dao[key] = newfn;
});
};
var autoloadFunctions = function(dataSource) {
var dao = dataSource.connector.DataAccessObject;
var functionList = ['errorHandler','incomingTransform','outgoingTransform'];
var transformer = null;
if(dataSource.settings.transformer && dataSource.settings.transformer.library
&& dataSource.settings.transformer.ext){
transformer = transformerLib(dataSource.settings.transformer.library);
}
dataSource.settings.operations.forEach(function(defintion){
functionList.forEach(function(name){
var handlerString = defintion[name];
if(handlerString) {
var handler = null;
// check if template is used for transformation. Only for
// incoming and outgoing tranformation with transformer defined
if(name !== 'errorHandler'&& transformer
&& handlerString.indexOf(dataSource.settings.transformer.ext) !== -1) {
var templateFile = path.join(path.dirname(defintion.file),
handlerString);
handler = transformer.createFunction(templateFile);
debug('loading file for transformation',templateFile);
} else {
handler = loadHandler(path.dirname(defintion.file),
handlerString);
}
if(handler) {
for (var f in defintion.functions) {
debug('autoloading function',name,'for',f);
dao[f][name] = handler;
}
}
}
});
if(defintion.hasOwnProperty('shared')) {
var sharedFlag = defintion['shared'];
for (var f in defintion.functions) {
debug('marking function', f ,' with shared flag as',sharedFlag);
dao[f]['shared'] = sharedFlag;
}
}
});
};
var loadHandler = function(dir, handlerString) {
var filename = handlerString.split('.')[0];
var functionName = handlerString.split('.')[1];
var fn = null;
try {
fn = require(path.join(dir,filename))[functionName];
} catch(e) {
//ignore the error and dont load the function
}
return fn;
};