@belmonddev/catch-request-express-middleware 1.2.3 → 1.2.5

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 +24 -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,16 +116,23 @@ 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) => {
118
134
  const responseCb = requestReceivedCb({
135
+ originalRequest: req,
119
136
  headers: req.headers,
120
137
  method: req.method,
121
138
  host: req.headers.host,
@@ -124,6 +141,7 @@ const callReceivedCbs = (req, res, requestReceivedCb) => {
124
141
  query: req.query,
125
142
  body: req.body,
126
143
  });
144
+
127
145
  if (responseCb) {
128
146
  req.socket.setMaxListeners(100);
129
147
  if (req.socketClosed) {
@@ -158,7 +176,6 @@ const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
158
176
  req.on('data', function (data) {
159
177
  body += data;
160
178
  });
161
-
162
179
  req.on('end', function () {
163
180
  req.body = body;
164
181
  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.5",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "nodemon -L --experimental-specifier-resolution=node ./src/index.js"