Skip to content

Commit

Permalink
chore: Improve HTTP request/response log
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-valenta committed Dec 22, 2020
1 parent 05c74ef commit 9b77818
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/internal/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,28 @@ export const HttpClient = {
},
});

debug('Executing HTTP Call to %s: %O', finalUrl, params);
const requestHeaders: Record<string, string> = {};
if (headers) {
headers.forEach((value, headerName) => {
requestHeaders[headerName] = value;
});
}
debug('Executing HTTP Call');
debug(`\t${params.method} ${finalUrl} HTTP/1.1`);
Object.entries(requestHeaders).forEach(([headerName, value]) =>
debug(`\t${headerName}: ${value}`)
);
if (params.body) {
debug(`\n\t${params.body}`);
}
const response = await fetch(finalUrl, params);
debug('Received response: %O', response);

let body: unknown;

const responseHeaders: Record<string, string> = {};
response.headers.forEach((value, headerName) => {
responseHeaders[headerName] = value;
});
const requestHeaders: Record<string, string> = {};
if (headers) {
headers.forEach((value, headerName) => {
requestHeaders[headerName] = value;
});
}

if (
(responseHeaders['content-type'] &&
Expand All @@ -243,6 +249,13 @@ export const HttpClient = {
body = await response.text();
}

debug('Received response');
debug(`\tHTTP/1.1 ${response.status} ${response.statusText}`);
Object.entries(responseHeaders).forEach(([headerName, value]) =>
debug(`\t${headerName}: ${value}`)
);
debug('\n\t%j', body);

return {
statusCode: response.status,
body,
Expand Down

0 comments on commit 9b77818

Please sign in to comment.