@aligent/microservice-util-lib 1.3.3 → 1.3.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.
package/package.json
CHANGED
|
@@ -7,15 +7,18 @@ exports.logMiddleware = logMiddleware;
|
|
|
7
7
|
async function parseBody(source, contentType) {
|
|
8
8
|
const normalizedContentType = contentType ? contentType.toLowerCase() : 'application/json';
|
|
9
9
|
try {
|
|
10
|
+
if (!source.body) {
|
|
11
|
+
return 'null';
|
|
12
|
+
}
|
|
10
13
|
// JSON content
|
|
11
14
|
if (normalizedContentType.includes('application/json')) {
|
|
12
|
-
return await source.
|
|
15
|
+
return await source.json();
|
|
13
16
|
}
|
|
14
17
|
// Text content
|
|
15
18
|
if (normalizedContentType.includes('text/') ||
|
|
16
19
|
normalizedContentType.includes('application/xml') ||
|
|
17
20
|
normalizedContentType.includes('application/x-www-form-urlencoded')) {
|
|
18
|
-
return await source.
|
|
21
|
+
return await source.text();
|
|
19
22
|
}
|
|
20
23
|
// Binary or multipart content - don't parse
|
|
21
24
|
if (normalizedContentType.includes('multipart/form-data') ||
|
|
@@ -25,8 +28,8 @@ async function parseBody(source, contentType) {
|
|
|
25
28
|
normalizedContentType.includes('audio/')) {
|
|
26
29
|
return `[Binary content: ${contentType}]`;
|
|
27
30
|
}
|
|
28
|
-
// Unknown content type, try
|
|
29
|
-
return await source.
|
|
31
|
+
// Unknown content type, try TEXT as default
|
|
32
|
+
return await source.text();
|
|
30
33
|
}
|
|
31
34
|
catch (error) {
|
|
32
35
|
return `[Unable to parse ${contentType} body: ${error instanceof Error ? error.message : 'Unknown error'}]`;
|
|
@@ -91,14 +94,14 @@ function logMiddleware(clientName, logLevel = 'INFO', logger = console) {
|
|
|
91
94
|
baseUrl: options.baseUrl,
|
|
92
95
|
url: request.url,
|
|
93
96
|
params: params,
|
|
94
|
-
body: await parseBody(request, contentType),
|
|
97
|
+
body: await parseBody(request.clone(), contentType),
|
|
95
98
|
});
|
|
96
99
|
},
|
|
97
100
|
async onResponse({ response }) {
|
|
98
101
|
const contentType = response.headers.get('Content-Type');
|
|
99
102
|
log(`Response from ${clientName}`, {
|
|
100
103
|
status: response.status,
|
|
101
|
-
body: await parseBody(response, contentType),
|
|
104
|
+
body: await parseBody(response.clone(), contentType),
|
|
102
105
|
});
|
|
103
106
|
},
|
|
104
107
|
};
|