-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathhooks.js
94 lines (89 loc) · 4.29 KB
/
hooks.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
var url = require('url'),
_ = require('lodash'),
iso = /^(\d{4})(?:-?W(\d+)(?:-?(\d+)D?)?|(?:-(\d+))?-(\d+))(?:[T ](\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?)?(?:Z(-?\d*))?$/;
/**
* Cleans query options object from Waterline-specific `where` object, by moving `where` values to main query options object.
* For example, given the query options object `{where:{firstName: "Tedd"}, limit: 1}, this function will modify the query options
* object to {firstName: "Tedd", limit: 1}
*
* @param {Request} req - SuperAgent HTTP Request object
* @param {String} method - HTTP request method
* @param {Object} config - configuration object used to hold request-specific configuration. this is used to avoid polluting the connection's own configuration object.
* @param {Object} conn - connection configuration object:
* - {Object} connection - Waterline connection configuration object
* - {String} collection - collection name. appended to API pathname.
* For example, given the api `http://localhost:8080/api/v1`,
* a collection named `user` will resolve to `http://localhost:8080/api/v1/user`.
* - {Object} options - query options object. contains query conditions (`where`), sort, limit etc. as per Waterline's API.
* - {Array<Object>} values - values of records to create.
* @param {Function} cb - function that is called when this hook finishes
*/
function removeWhereFromQuery(req, method, config, conn, cb){
var query = {};
if(_.isObject(conn.options) && conn.options.hasOwnProperty('where')){
query = conn.options.where;
delete conn.options.where;
conn.options = _.merge(conn.options, query);
}
cb();
}
/**
* Createn an HTTP request URL from connection configuration, collection name and query options object.
* If query options object contains an `id` field, the HTTP URL will be formatted as proto://pathname/collection/id.
* Otherwise the HTTP URL will be formatted as proto://pathname/collection.
*
* @param {Request} req - SuperAgent HTTP Request object
* @param {String} method - HTTP request method
* @param {Object} config - configuration object used to hold request-specific configuration. this is used to avoid polluting the connection's own configuration object.
* @param {Object} conn - connection configuration object:
* - {Object} connection - Waterline connection configuration object
* - {String} collection - collection name. appended to API pathname.
* For example, given the api `http://localhost:8080/api/v1`,
* a collection named `user` will resolve to `http://localhost:8080/api/v1/user`.
* - {Object} options - query options object. contains query conditions (`where`), sort, limit etc. as per Waterline's API.
* - {Array<Object>} values - values of records to create.
* @param {Function} cb - function that is called when this hook finishes
*/
function createEndpoint(req, method, config, conn, cb){
if(_.isObject(conn.options) && conn.options.hasOwnProperty('id')){
config.endpoint = url.resolve(conn.connection.endpoint + '/', conn.collection + '/' + conn.options.id);
delete conn.options.id;
} else {
config.endpoint = url.resolve(conn.connection.endpoint + '/', conn.collection);
}
cb();
}
/**
* Convert ISO formatted strings on response object into Javascript Date objects.
* Used to cast date fields returned from HTTP response into their correct Date type.
* @param {Object} record - response record object to process.
*/
function castRecordDateFields(record) {
_.forEach(record, function (value, key) {
if(_.isString(value) && iso.test(value)){
record[key] = new Date(value);
}
});
}
/**
* Process HTTP response. Converts response objects date fields from Strings to Dates.
* @param {Error} err - HTTP response error
* @param {Response} res - SuperAgent HTTP Response object
* @param {Function} cb - function that is called when this hook finishes
*/
function processResponse(err, res, cb){
if(!err) {
if(Array.isArray(res.body)){
res.body.forEach(function (body) {
castRecordDateFields(body);
});
} else if (_.isObject(res.body)) {
castRecordDateFields(res.body);
}
}
cb();
}
module.exports = {
before: [removeWhereFromQuery, createEndpoint],
after: [processResponse]
};