@belmonddev/catch-request-express-middleware 3.2.1 → 3.2.3
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 +31 -8
- package/package.json +3 -9
package/index.js
CHANGED
|
@@ -56,16 +56,39 @@ 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
|
+
|
|
59
69
|
const overrideHttpModule = (httpModule, requestSentCb) => {
|
|
60
|
-
override(httpModule, 'request', (reqObject, request) => {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
70
|
+
override(httpModule, 'request', (reqObject, urlOrRequest, request) => {
|
|
71
|
+
// node-fetch v3+ will return the 2nd arg as url, so correctly identify the request object
|
|
72
|
+
request = typeof urlOrRequest === 'string' ? request : urlOrRequest;
|
|
73
|
+
|
|
74
|
+
// transform headers to use array values instead of string
|
|
75
|
+
const headers = transformHeaderValues(request.headers);
|
|
76
|
+
const contentLength = getHeaderValue(headers || {}, 'content-length');
|
|
65
77
|
const requestHasBody = !!contentLength;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
);
|
|
78
|
+
|
|
79
|
+
const [pathname, query] = request.path.split('?');
|
|
80
|
+
const params = Object.fromEntries(new URLSearchParams(query).entries());
|
|
81
|
+
|
|
82
|
+
// add values not included in node-fetch v3+
|
|
83
|
+
request = {
|
|
84
|
+
...request,
|
|
85
|
+
href: `${reqObject.protocol}//${reqObject.host}${request.path}`,
|
|
86
|
+
protocol: reqObject.protocol,
|
|
87
|
+
host: reqObject.host,
|
|
88
|
+
search: `&${query}`,
|
|
89
|
+
pathname,
|
|
90
|
+
query,
|
|
91
|
+
};
|
|
69
92
|
|
|
70
93
|
let responseCb;
|
|
71
94
|
if (requestHasBody) {
|
package/package.json
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@belmonddev/catch-request-express-middleware",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.3",
|
|
4
4
|
"main": "index.js",
|
|
5
|
-
"scripts": {
|
|
6
|
-
"start": "nodemon -L --experimental-specifier-resolution=node ./src/index.js"
|
|
7
|
-
},
|
|
8
5
|
"author": "David Escalera",
|
|
9
6
|
"license": "ISC",
|
|
10
|
-
"
|
|
11
|
-
"devDependencies": {
|
|
12
|
-
"nodemon": "^2.0.7"
|
|
13
|
-
},
|
|
7
|
+
"description": "",
|
|
14
8
|
"type": "module",
|
|
15
|
-
"
|
|
9
|
+
"dependencies": {}
|
|
16
10
|
}
|