-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
50 lines (43 loc) · 1.12 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
const merge = require('merge-descriptors');
module.exports = function (app, mode, options) {
mode = mode || 'extended';
const qs = (mode === 'extended') ? require('qs') : require('querystring');
const converter = (
mode === 'strict' && function (value) { return !Array.isArray(value) ? [value] : value }
||
mode === 'first' && function (value) { return Array.isArray(value) ? value[0] : value }
);
merge(app.request, {
/**
* Get parsed query-string.
*
* @return {Object}
* @api public
*/
get query() {
let str = this.querystring;
if (!str) return {};
let c = this._querycache = this._querycache || {};
let query = c[str];
if (!query) {
c[str] = query = qs.parse(str, options);
if (converter) {
for (let key in query) {
query[key] = converter(query[key]);
}
}
}
return query;
},
/**
* Set query-string as an object.
*
* @param {Object} obj
* @api public
*/
set query(obj) {
this.querystring = qs.stringify(obj);
},
});
return app;
};