@belmonddev/catch-request-express-middleware 3.1.2 → 3.2.1

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 +22 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -67,24 +67,43 @@ const overrideHttpModule = (httpModule, requestSentCb) => {
67
67
  new URLSearchParams(request.query).entries()
68
68
  );
69
69
 
70
+ let responseCb;
70
71
  if (requestHasBody) {
72
+ let requestBody = '';
71
73
  override(reqObject, 'write', (result, data) => {
72
74
  const body = data.toString();
73
- const responseCb = requestSentCb({
75
+ requestBody += body;
76
+ });
77
+
78
+ override(reqObject, 'end', () => {
79
+ responseCb = requestSentCb({
74
80
  ...request,
75
81
  params,
76
- body,
82
+ body: requestBody,
77
83
  });
84
+
78
85
  if (responseCb) {
79
86
  catchHttpResponse(reqObject, responseCb);
80
87
  }
81
88
  });
82
89
  } else {
83
- const responseCb = requestSentCb({ ...request, params, body: null });
90
+ responseCb = requestSentCb({ ...request, params, body: null });
84
91
  if (responseCb) {
85
92
  catchHttpResponse(reqObject, responseCb);
86
93
  }
87
94
  }
95
+
96
+ reqObject.on('error', (err) => {
97
+ if (responseCb) {
98
+ const response = {
99
+ headers: {},
100
+ statusCode: null,
101
+ statusMessage: null,
102
+ body: err.toString(),
103
+ };
104
+ responseCb(response);
105
+ }
106
+ });
88
107
  });
89
108
  };
90
109
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@belmonddev/catch-request-express-middleware",
3
- "version": "3.1.2",
3
+ "version": "3.2.1",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "nodemon -L --experimental-specifier-resolution=node ./src/index.js"