@belmonddev/catch-request-express-middleware 1.2.2 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +32 -10
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -41,25 +41,35 @@ const catchHttpResponse = (reqObject, responseCb) => {
|
|
|
41
41
|
: zlib.inflate;
|
|
42
42
|
zlibFunc(buffer, (_, decoded) => {
|
|
43
43
|
const stringBody = decoded.toString('utf8');
|
|
44
|
-
|
|
44
|
+
const request = {
|
|
45
45
|
...responseObj,
|
|
46
46
|
body: /application\/json;?.*?$/.test(
|
|
47
47
|
response.headers['content-type']
|
|
48
48
|
)
|
|
49
49
|
? JSON.parse(stringBody)
|
|
50
50
|
: stringBody,
|
|
51
|
-
}
|
|
51
|
+
};
|
|
52
|
+
if (responseCb?.then) {
|
|
53
|
+
responseCb.then(() => request);
|
|
54
|
+
} else {
|
|
55
|
+
responseCb(request);
|
|
56
|
+
}
|
|
52
57
|
});
|
|
53
58
|
} else {
|
|
54
59
|
const stringBody = chunks.map((c) => c.toString('utf8')).join('');
|
|
55
|
-
|
|
60
|
+
const request = {
|
|
56
61
|
...responseObj,
|
|
57
62
|
body: /application\/json;?.*?$/.test(
|
|
58
63
|
response.headers['content-type']
|
|
59
64
|
)
|
|
60
65
|
? JSON.parse(stringBody)
|
|
61
66
|
: stringBody,
|
|
62
|
-
}
|
|
67
|
+
};
|
|
68
|
+
if (responseCb?.then) {
|
|
69
|
+
responseCb.then(() => request);
|
|
70
|
+
} else {
|
|
71
|
+
responseCb(request);
|
|
72
|
+
}
|
|
63
73
|
}
|
|
64
74
|
});
|
|
65
75
|
}
|
|
@@ -98,14 +108,26 @@ const overrideHttpModule = (httpModule, requestSentCb) => {
|
|
|
98
108
|
};
|
|
99
109
|
|
|
100
110
|
const callResponseCb = (responseCb, res) => {
|
|
101
|
-
|
|
111
|
+
let body;
|
|
112
|
+
try {
|
|
113
|
+
body = /application\/json;?.*?$/.test(res.getHeaders()['content-type'])
|
|
114
|
+
? JSON.parse(res.bodyWritten)
|
|
115
|
+
: res.bodyWritten;
|
|
116
|
+
} catch (e) {
|
|
117
|
+
body = res.bodyWritten;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const response = {
|
|
102
121
|
headers: res.getHeaders(),
|
|
103
122
|
statusCode: res.statusCode,
|
|
104
123
|
statusMessage: res.statusMessage,
|
|
105
|
-
body
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
124
|
+
body,
|
|
125
|
+
};
|
|
126
|
+
if (responseCb?.then) {
|
|
127
|
+
responseCb.then(() => response);
|
|
128
|
+
} else {
|
|
129
|
+
responseCb(response);
|
|
130
|
+
}
|
|
109
131
|
};
|
|
110
132
|
|
|
111
133
|
const callReceivedCbs = (req, res, requestReceivedCb) => {
|
|
@@ -118,6 +140,7 @@ const callReceivedCbs = (req, res, requestReceivedCb) => {
|
|
|
118
140
|
query: req.query,
|
|
119
141
|
body: req.body,
|
|
120
142
|
});
|
|
143
|
+
|
|
121
144
|
if (responseCb) {
|
|
122
145
|
req.socket.setMaxListeners(100);
|
|
123
146
|
if (req.socketClosed) {
|
|
@@ -152,7 +175,6 @@ const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
|
|
|
152
175
|
req.on('data', function (data) {
|
|
153
176
|
body += data;
|
|
154
177
|
});
|
|
155
|
-
|
|
156
178
|
req.on('end', function () {
|
|
157
179
|
req.body = body;
|
|
158
180
|
callReceivedCbs(req, res, requestReceivedCb);
|