Skip to content

Commit aaa7c4a

Browse files
vademoOlivier Van den Mooter
andauthored
Complete features for fetch, remove axios (#22)
* Complete features for fetch, remove axios * cover --------- Co-authored-by: Olivier Van den Mooter <olivier.vandenmooter@digipolis.be>
1 parent d55998b commit aaa7c4a

8 files changed

Lines changed: 285 additions & 161 deletions

File tree

example/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const express = require('express');
2-
const axios = require('axios');
32

43
const { requestMiddleware, requestlogger } = require('../lib');
54

@@ -45,7 +44,7 @@ async function start() {
4544
try {
4645
initializeExpress();
4746
const startedapp = await startListening();
48-
await axios.get('http://localhost:2000?page=1').catch(() => {});
47+
await global.fetch('http://localhost:2000?page=1').catch(() => {});
4948
return startedapp;
5049
} catch (err) {
5150
console.log(`Error occured ${err}`);

lib/request.logger.js

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,54 @@ module.exports = (config) => {
1616
if(original) {
1717
global.fetch = async (requestUrl, options) => {
1818
const parsedUrl = url.parse(requestUrl);
19+
const params = Object.fromEntries(new URLSearchParams(parsedUrl.search));
1920
let correlationId;
2021
if (options && options.headers) {
2122
correlationId = options.headers['Dgp-Correlation'] || options.headers['dgp-correlation'] || validatedConfig.correlationIdfallback;
2223
}
2324

2425
const timer = startTimer();
25-
const response = await original(requestUrl, options)
26-
const duration = timer.getDuration();
2726
const req = {
2827
headers: options?.headers || undefined,
2928
host: parsedUrl.host,
30-
path: parsedUrl.path,
29+
path: parsedUrl.pathname,
3130
method: options?.method || 'GET',
32-
payload: options?.body || undefined,
31+
payload: options?.body || params,
3332
search: parsedUrl.search,
3433
};
35-
const res = {
36-
status: response.status,
37-
duration,
38-
};
39-
const log = logRequest(logger, correlationId, req, res, parsedUrl.protocol );
40-
let logged = false;
41-
if (validatedConfig.logResponsePayload) {
42-
const json = () =>
43-
response
44-
.clone()
45-
.json()
46-
.then((data) => {
47-
log(data);
48-
logged = true;
49-
return (data)
50-
});
51-
const text = () =>
52-
response
53-
.clone()
54-
.text()
55-
.then((data) => {
56-
log(data);
57-
logged = true;
58-
return (data)
59-
});
60-
// Response is not parsed json/text
61-
setTimeout(() => {
62-
if(!logged) log();
63-
}, 1)
64-
response.json = json;
65-
response.text = text;
66-
} else {
34+
try {
35+
const response = await original(requestUrl, options)
36+
const duration = timer.getDuration();
37+
const res = {
38+
headers: Object.fromEntries(response.headers),
39+
status: response.status,
40+
duration,
41+
};
42+
const log = logRequest(logger, correlationId, req, res, parsedUrl.protocol );
43+
if (validatedConfig.logResponsePayload) {
44+
try {
45+
if(response.json) {
46+
const json = await response.clone().json();
47+
log(json);
48+
}
49+
} catch {
50+
const text = await response.clone().text();
51+
log(text);
52+
}
53+
} else {
54+
log();
55+
}
56+
return response;
57+
} catch(e) {
58+
const duration = timer.getDuration();
59+
const res = {
60+
status: `${e.message}: ${e.cause?.code}`,
61+
duration,
62+
};
63+
const log = logRequest(logger, correlationId, req, res, parsedUrl.protocol );
6764
log();
65+
throw e;
6866
}
69-
return response;
7067
}
7168
}
7269

lib/request.middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = (config) => {
4545
payload: req.body,
4646
},
4747
response: {
48-
headers: res.getHeaders(),
48+
headers: { ...res.getHeaders() },
4949
status: res.statusCode,
5050
duration: timer.getDuration(),
5151
payload: responsebody,

package-lock.json

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
"@digipolis/log": "^1.1.1"
2727
},
2828
"devDependencies": {
29-
"@eslint/js": "^9.33.0",
29+
"@eslint/js": "^9.35.0",
3030
"eslint-plugin-mocha": "^11.1.0",
31-
"axios": "^1.11.0",
3231
"c8": "^10.1.3",
3332
"jsonschema": "^1.5.0",
3433
"express": "^5.1.0",
35-
"globals": "^16.3.0",
36-
"mocha": "^11.7.1",
34+
"globals": "^16.4.0",
35+
"mocha": "^11.7.2",
3736
"sinon": "^21.0.0"
3837
}
3938
}

test/helpers/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function initializeExpress(config) {
1414
});
1515
app.get('/externalcall', (req, res) => res.json({ ok: 'ok' }));
1616
app.post('/externalcall', (req, res) => res.json({ ok: 'ok' }));
17+
app.get('/externalcalltext', (req, res) => res.send('ok'));
1718
app.use(requestMiddleware(config));
1819

1920
app.get('/internalcall', (req, res) => res.json({ ok: 'ok' }));

0 commit comments

Comments
 (0)