forked from NiallBunting/express-partial-response
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
123 lines (107 loc) · 3.13 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
const jsonMask = require('json-mask');
const eachDeep = require('deepdash/eachDeep');
const badCode = code => code >= 300 || code < 200
module.exports = function (opt) {
opt = opt || {}
function isObject(obj) {
return obj != null && obj.constructor === Object;
}
function partialResponse(obj, req, res) {
// use filters to restrict result
let filters = [];
if (Array.isArray(req.customFilter)) {
filters = filters.concat(req.customFilter);
}
if (Array.isArray(opt.filter)) {
filters = filters.concat(opt.filter);
} else if ('function' === typeof opt.filter) {
obj = opt.filter(obj, req, res);
if(!obj){
return null;
}
}
// filter by array
if (filters.length) {
let breakLoop = false;
obj = eachDeep(obj, (value, key, parent, ctx) => {
if (ctx.isCircular || breakLoop) {
return false;
}
if(ctx.depth > 10){
console.error("filters failed, too much depth of ctx", ctx);
breakLoop = true;
}
//do removal
if (filters.indexOf(key) !== -1) {
if (isObject(parent)) {
delete parent[key]
}
else if (Array.isArray(parent)) {
parent.splice(key, 1);
}
}
}, {
checkCircular: true,
keepCircular: false
})
}
// get fields key
const fieldsKey = opt.query || 'fields';
const fields = req[fieldsKey];
if (fields) {
const fieldPrefix = opt.prefix || '';
obj = jsonMask(obj, fieldPrefix + fields);
}
return obj;
}
function wrap(orig) {
return function (obj) {
const req = this.req;
if (1 === arguments.length) {
if (badCode(this.statusCode)) {
return orig(this.statusCode, arguments[0]);
} else {
let ret = partialResponse(obj, req, req.res);
if(!ret){
// skip send json, when no need to send
return;
}
return orig(ret);
}
}
if ('number' === typeof arguments[1] && !badCode(arguments[1])) {
// res.json(body, status) backwards compat
let ret = partialResponse(obj, req, req.res);
if(!ret){
// skip send json, when no need to send
return;
}
return orig(ret, arguments[1]);
}
if ('number' === typeof obj && !badCode(obj)) {
// res.json(status, body) backwards compat
let ret = partialResponse(arguments[1], req, req.res);
if(!ret){
// skip send json, when no need to send
return;
}
return orig(obj, ret);
}
// The original actually returns this.send(body)
return orig(obj, arguments[1]);
}
}
return function (req, res, next) {
if (!res.__isJSONMaskWrapped) {
// get fields key
const fieldsKey = opt.query || 'fields';
// move query to fields
req[fieldsKey] = req.query[fieldsKey];
delete req.query[fieldsKey];
res.json = wrap(res.json.bind(res))
if (req.jsonp) res.jsonp = wrap(res.jsonp.bind(res))
res.__isJSONMaskWrapped = true
}
next()
}
}