@belmonddev/catch-request-express-middleware 3.2.3 → 3.3.0

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 +32 -39
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -56,36 +56,28 @@ const catchHttpResponse = (reqObject, responseCb) => {
56
56
  });
57
57
  };
58
58
 
59
- const transformHeaderValues = (headers) => {
60
- let transformedHeaders = {};
61
- const headerEntries = Object.entries(headers);
62
- headerEntries.forEach(([key, value]) => {
63
- transformedHeaders[key] = typeof value === 'string' ? [value] : value;
64
- });
65
-
66
- return transformedHeaders;
67
- };
68
-
69
- const overrideHttpModule = (httpModule, requestSentCb) => {
70
- override(httpModule, 'request', (reqObject, urlOrRequest, request) => {
59
+ const overrideHttpModule = (httpModule, outgoingRequestCb) => {
60
+ override(httpModule, 'request', async (reqObject, urlOrRequest, request) => {
71
61
  // node-fetch v3+ will return the 2nd arg as url, so correctly identify the request object
72
62
  request = typeof urlOrRequest === 'string' ? request : urlOrRequest;
73
63
 
74
- // transform headers to use array values instead of string
75
- const headers = transformHeaderValues(request.headers);
76
- const contentLength = getHeaderValue(headers || {}, 'content-length');
64
+ const contentLength = getHeaderValue(request.headers, 'content-length');
77
65
  const requestHasBody = !!contentLength;
78
66
 
79
- const [pathname, query] = request.path.split('?');
80
- const params = Object.fromEntries(new URLSearchParams(query).entries());
67
+ const [pathname, search] = request.path.split('?');
68
+ const query = search
69
+ ? Object.fromEntries(new URLSearchParams(search).entries())
70
+ : {};
81
71
 
82
72
  // add values not included in node-fetch v3+
83
73
  request = {
84
- ...request,
85
- href: `${reqObject.protocol}//${reqObject.host}${request.path}`,
86
- protocol: reqObject.protocol,
74
+ requestDirection: 'outgoing',
75
+ originalRequest: request,
76
+ headers: request.headers,
77
+ protocol: reqObject.protocol?.replace(':', ''),
78
+ method: request.method,
87
79
  host: reqObject.host,
88
- search: `&${query}`,
80
+ search: search ? `?${search}` : null,
89
81
  pathname,
90
82
  query,
91
83
  };
@@ -98,10 +90,9 @@ const overrideHttpModule = (httpModule, requestSentCb) => {
98
90
  requestBody += body;
99
91
  });
100
92
 
101
- override(reqObject, 'end', () => {
102
- responseCb = requestSentCb({
93
+ override(reqObject, 'end', async () => {
94
+ responseCb = await outgoingRequestCb({
103
95
  ...request,
104
- params,
105
96
  body: requestBody,
106
97
  });
107
98
 
@@ -110,7 +101,7 @@ const overrideHttpModule = (httpModule, requestSentCb) => {
110
101
  }
111
102
  });
112
103
  } else {
113
- responseCb = requestSentCb({ ...request, params, body: null });
104
+ responseCb = await outgoingRequestCb({ ...request, body: {} });
114
105
  if (responseCb) {
115
106
  catchHttpResponse(reqObject, responseCb);
116
107
  }
@@ -140,14 +131,16 @@ const callResponseCb = (responseCb, res) => {
140
131
  responseCb(response);
141
132
  };
142
133
 
143
- const callReceivedCbs = (req, res, requestReceivedCb, body) => {
144
- const responseCb = requestReceivedCb({
134
+ const callIncomingRequestsCbs = (req, res, incomingRequestsCb, body) => {
135
+ const responseCb = incomingRequestsCb({
136
+ requestDirection: 'incoming',
145
137
  originalRequest: req,
146
138
  headers: req.headers,
139
+ protocol: req.protocol,
147
140
  method: req.method,
148
141
  host: req.headers.host,
142
+ search: req._parsedUrl.search,
149
143
  pathname: req._parsedUrl.pathname,
150
- params: req.params,
151
144
  query: req.query,
152
145
  body,
153
146
  });
@@ -164,7 +157,7 @@ const callReceivedCbs = (req, res, requestReceivedCb, body) => {
164
157
  }
165
158
  };
166
159
 
167
- const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
160
+ const catchExpressIncomingRequest = (req, res, incomingRequestCb) => {
168
161
  // we override write method ASAP cause otherwise if the request finishes too fast the response callback is not called
169
162
  let writeCount = 0;
170
163
  let firstData = '';
@@ -182,7 +175,7 @@ const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
182
175
  });
183
176
 
184
177
  if (req.body !== undefined) {
185
- callReceivedCbs(req, res, requestReceivedCb, req.body);
178
+ callIncomingRequestsCbs(req, res, incomingRequestCb, req.body);
186
179
  } else {
187
180
  const contentLength = getHeaderValue(req.headers, 'content-length');
188
181
  if (contentLength) {
@@ -191,25 +184,25 @@ const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
191
184
  body += data;
192
185
  });
193
186
  req.on('end', function () {
194
- callReceivedCbs(req, res, requestReceivedCb, body);
187
+ callIncomingRequestsCbs(req, res, incomingRequestCb, body);
195
188
  });
196
189
  } else {
197
- callReceivedCbs(req, res, requestReceivedCb, '');
190
+ callIncomingRequestsCbs(req, res, incomingRequestCb, '');
198
191
  }
199
192
  }
200
193
  };
201
194
 
202
195
  const catchRequestExpressMiddleware = (
203
- requestReceivedCb = null,
204
- requestSentCb = null
196
+ incomingRequestCb = null,
197
+ outgoingRequestCb = null
205
198
  ) => {
206
- if (requestSentCb) {
207
- overrideHttpModule(http, requestSentCb);
208
- overrideHttpModule(https, requestSentCb);
199
+ if (outgoingRequestCb) {
200
+ overrideHttpModule(http, outgoingRequestCb);
201
+ overrideHttpModule(https, outgoingRequestCb);
209
202
  }
210
203
  return (req, res, next) => {
211
- if (requestReceivedCb) {
212
- catchExpressReceivedRequest(req, res, requestReceivedCb);
204
+ if (incomingRequestCb) {
205
+ catchExpressIncomingRequest(req, res, incomingRequestCb);
213
206
  }
214
207
  next();
215
208
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@belmonddev/catch-request-express-middleware",
3
- "version": "3.2.3",
3
+ "version": "3.3.0",
4
4
  "main": "index.js",
5
5
  "author": "David Escalera",
6
6
  "license": "ISC",