-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.js
477 lines (414 loc) · 12.6 KB
/
response.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
var
_ = require('underscore')
// Local libs
, HashSanidator = require('./sanidator').HashSanidator
, utils = require('./utils')
;
var
error = utils.scopedError('easyPI')
, log = utils.scopedLog('easyPI')
, slice = Array.prototype.slice
;
var pushUniqueIdsArray = function (doc, paths, arr, i) {
var v = doc.get(paths[i++])
if (v instanceof Array) {
_.each(v, function (attr) {
pushUniqueIdsArray(attr, paths, arr, i);
})
return
}
if (!v) return;
if (paths.length > i) {
if (v.get) {
return pushUniqueIdsArray(v, paths, arr, i);
} else {
return;
}
}
utils.pushUnique(arr, v)
}
/**
* Hook into the Response Object
*/
var Response = function (epi, req, res, next) {
this.epi = epi;
this.req = req;
this.res = res;
this.next = next;
// Models to be sent
this._models = {}
// Model which will be expanded prior to sending the response
// This is indexed by the ModelName and has a set of unique ids
this._modelExpansions = {}
// Generic request parameters
this.body = req.body;
this.query = req.query;
this.params = req.params;
this.errors = {
// Body parameters error
body:new HashSanidator(this.body),
// Query parameters error
query:new HashSanidator(this.query),
// Params
params:new HashSanidator(this.params),
// Generic errors
generic:[]
};
}
Response.prototype = {
// Indicates whether operations should automatically send results
autoSend:true,
autoNext:false,
_extra:null,
_models:null,
_result:null,
_status:null,
raise:function (s) {
this._status = 500;
this.err('There has been an unexpected server error.')
log(s)
this.send();
},
// Indicates an operation has been finished
_finished:function () {
if (this.autoSend) return this.send();
if (this.autoNext) return this.next();
},
//
// Easy shortcuts
//
// Authentication required
auth:function (msg) {
this._status = 401;
this.err(msg || 'Authentication required')
this._finished();
return this;
},
// Action forbidden by user
forbidden:function (msg) {
this._status = 403;
this.err(msg || 'Action forbidden for the current user')
this._finished();
return this;
},
// Couldn't find the resource
lost:function (msg) {
this._status = 404;
this.err(msg || 'Resource not found')
this._finished();
return this;
},
// Bad request
bad:function (name, msg) {
this._status = 400;
if (name) {
this.err(name, msg)
}
this._finished()
return this;
},
// Indicates this wa a 201 CREATE operation
created:function (model, models) {
this._status = 201;
this.result(model);
this.models(models)
this._finished()
return this;
},
// Indicates a resource has been updated
updated:function (model) {
this._status = 200;
this.result(model)
this._finished()
return this;
},
// Indicates a resource has been removed
deleted:function (doc) {
if (doc) {
this._status = 200;
this.result(doc);
} else {
this._status = 204;
}
this._finished()
return this;
},
extra:function (extra) {
this._extra = extra;
return this;
},
// Indicates this was a GET / operation
fetched:function (result, models, hasMore) {
this._status = 200;
this.result(result);
this.models(models)
this.extra({hasMore:hasMore})
this._finished()
return this;
},
// Stops the processing and returns the errors, if any
validate:function () {
if (this.hasErrors()) {
this.bad()
return false;
}
return true;
},
// Wraps the function around error detection
cb:function (cb) {
var self = this;
return function (err) {
if (err) return self.raise(err);
cb.apply(this, slice.call(arguments, 1))
}
},
//
// Error control
//
// Sets validation rules
rules:function (target, rules) {
if (!rules){
rules = target;
target = 'body'
}
return this.errors[target].rules(rules)
},
// Adiciona uma única regra de validação
rule:function (target, param, rule) {
if (!rule){
rule = param
param = target
target = 'body'
}
return this.errors[target].rule(param, rule);
},
// Adds a validation error to the response
error:function (name, msg) {
if (msg === undefined) return this.errors.generic.push(name);
this.errors['body'].error(name, msg);
},
// Indicates whether there is any validation error
hasErrors:function () {
if (!this.errors) return false;
if (this.errors.generic.length) return true;
if (this.errors.body.hasErrors()) return true;
if (this.errors.query.hasErrors()) return true;
if (this.errors.params.hasErrors()) return true;
return false;
},
//
// Model Expansion
//
// Helper function to register model expansions which will be
// fetched prior to sending the response
expandModel:function (model, expandOptions) {
// Finding the unique ids
var self = this;
_.each(expandOptions, function (expansion, paths) {
paths = paths.split('.')
var arr = self._modelExpansions[expansion.name] || (self._modelExpansions[expansion.name] = [])
if (model instanceof Array) {
model.forEach(function (model) {
pushUniqueIdsArray(model, paths, arr, 0);
})
} else {
pushUniqueIdsArray(model, paths, arr, 0);
}
})
},
// Indicates whether this response conveys (or will) any model expansion
hasModelExpansion:function (modelName) {
return this._modelExpansions[modelName] && this._modelExpansions[modelName].length;
},
// Fetches expanded models and add them to the models to be sent
_fetchExpansions:function (done) {
// This means we want to use a set of models as the result,
// but for this we must see if there are any expansions which
// will pollute it
this._expandResultIds();
var self = this
// Count of hanging expansions
, expandedCount = 1
// Callback to find out if we are done
, cb = this.cb(function (docs) {
if (docs) self.models(docs)
if (--expandedCount <= 0) done();
});
// Fetching them from the database
_.each(this._modelExpansions, function (ids, name) {
if (!ids.length) return;
expandedCount++;
self.epi.models[name].find({_id:{$in:ids}}, cb)
})
cb();
},
//
// Request parameters inflation (model lookup)
//
inflateBody:function (inflate, after) {
var self = this
, c = 1
, next = function () { if (--c <= 0) after(); }
, inflateParam = function (name, model) {
if (!self.body[name] || (self.body[name] instanceof model)) return;
if (!self.rule(name, function () { this.mongoId() })) return;
c++;
model.findById(self.body[name], self.cb(function (doc) {
if (!doc) return self.error(name, 'resource_not_found');
self.body[name] = doc;
next();
}))
}
_.each(inflate, function (inflation, path) {
inflateParam(path, inflation.model)
})
next();
},
//
// Lower level API
//
//
// Results
//
// Configures the result for this session
result:function (result) {
if (this._result) this.raise('The result has already been defined for this response');
this._result = result;
return this;
},
// Return the ids of the models for the current result
resultIds:function () {
var r = this._result;
if (!r) return [];
if (r instanceof Array) {
return Array.prototype.slice.call(r, 1);
} else if (typeof r == 'string') {
var ids = []
_.each(this._models[r], function (model) {
ids.push(model.id)
})
return ids;
} else if (r.id) {
return [r.id]
}
return [];
},
// Expands the result, if it's a string, to the ids of the current
// models in the corresponding set
_expandResultIds:function () {
var result = this._result, models;
if (typeof result != 'string' || !this.hasModelExpansion(result)) return;
models = this._models[result];
// We have pending expansions, so we'll get the id of the fetched
// models
result = [result];
if (models instanceof Array) {
result = result.concat(this.resultIds())
} else if (models && models.id) {
result.push(models.id);
}
this._result = result;
},
// Removes nested associations from the _result object
_flattenObject:function (obj) {
var self = this
if (!obj || typeof obj != 'object') return;
_.each(obj, function (val, name) {
if (!val || typeof val != 'object') return;
// mongoose model
if (val.constructor && val.constructor.modelName && val.id) {
self.models(val);
obj[name] = val.id;
} else {
self._flattenObject(val);
}
})
},
//
// Response
//
// Adds models to be packaged in the response
models:function (models) {
var self = this, name, modelsArray;
if (!models) return this;
// We assume an array of models is homogeneous
if (models instanceof Array) {
// Well, nothing to add
if (!models.length) return this;
models.forEach(function (model) {
self.models(model);
})
}
// it's just a single model
else {
// Okay, let's index the models by their name
name = models.constructor.modelName
if (!name) this.raise('Missing model name');
modelsArray = this._models[name] || (this._models[name] = [])
utils.pushUnique(modelsArray, models, models.id)
}
return this;
},
send:function (extra) {
// Default error status = 500 Internal, because it's unhandled
if (!this._status && this.hasErrors()) {
this._status = 500;
}
var self = this
, done = function () {
var built = self.build();
self.res.json(self._status, _.extend(extra || {}, self._extra, built))
}
// No errors, let's fetch the expansions and flatten the objects
if (!this.hasErrors()) {
if (this._result) {
this._result = this._result.toJSON ? this._result.toJSON() : this._result;
this._flattenObject(this._result);
}
this._fetchExpansions(done);
} else {
done()
}
},
_buildErrors:function () {
var errors = {};
if (this.errors.generic.length) {
errors.generic = this.errors.generic;
}
if (this.errors.body.hasErrors()) {
errors.body = this.errors.body.errors;
}
if (this.errors.query.hasErrors()) {
errors.query = this.errors.query.errors;
}
if (this.errors.params.hasErrors()) {
errors.params = this.errors.params.errors;
}
for (var e in errors) {
return errors;
}
return null;
},
build:function () {
if (!this._status) {
this.raise('Invalid status');
}
var errors = this._buildErrors()
if (errors) {
return {
status:this._status,
errors:errors
}
}
return {
status:this._status,
result:this._result,
models:this._models
}
}
}
//
// Sanidator delegation
//
Response.prototype['err'] = Response.prototype['error']
module.exports = Response;