@belmonddev/catch-request-express-middleware 1.2.3 → 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.
Files changed (2) hide show
  1. package/index.js +23 -7
  2. 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
- responseCb({
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
- responseCb({
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
  }
@@ -106,12 +116,18 @@ const callResponseCb = (responseCb, res) => {
106
116
  } catch (e) {
107
117
  body = res.bodyWritten;
108
118
  }
109
- responseCb({
119
+
120
+ const response = {
110
121
  headers: res.getHeaders(),
111
122
  statusCode: res.statusCode,
112
123
  statusMessage: res.statusMessage,
113
124
  body,
114
- });
125
+ };
126
+ if (responseCb?.then) {
127
+ responseCb.then(() => response);
128
+ } else {
129
+ responseCb(response);
130
+ }
115
131
  };
116
132
 
117
133
  const callReceivedCbs = (req, res, requestReceivedCb) => {
@@ -124,6 +140,7 @@ const callReceivedCbs = (req, res, requestReceivedCb) => {
124
140
  query: req.query,
125
141
  body: req.body,
126
142
  });
143
+
127
144
  if (responseCb) {
128
145
  req.socket.setMaxListeners(100);
129
146
  if (req.socketClosed) {
@@ -158,7 +175,6 @@ const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
158
175
  req.on('data', function (data) {
159
176
  body += data;
160
177
  });
161
-
162
178
  req.on('end', function () {
163
179
  req.body = body;
164
180
  callReceivedCbs(req, res, requestReceivedCb);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@belmonddev/catch-request-express-middleware",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "nodemon -L --experimental-specifier-resolution=node ./src/index.js"