index.js
1.24 KB
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
var merge = require('merge-descriptors');
module.exports = function (app, mode) {
mode = mode || 'extended';
var qs = require('querystring');
if (mode === 'extended') {
qs = require('qs');
}
var converter = null;
if (mode === 'strict') {
converter = function (value) {
if (!Array.isArray(value)) {
return [value];
}
return value;
};
} else if (mode === 'first') {
converter = function (value) {
if (Array.isArray(value)) {
return value[0];
}
return value;
};
}
merge(app.request, {
/**
* Get parsed query-string.
*
* @return {Object}
* @api public
*/
get query() {
var str = this.querystring;
if (!str) return {};
var c = this._querycache = this._querycache || {};
var query = c[str];
if (!query) {
c[str] = query = qs.parse(str);
if (converter) {
for (var 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;
};