Skip to content

Commit

Permalink
feat: use baseUrl to display full URL
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-abrioux committed Oct 29, 2021
1 parent b81adee commit 5f1ba4e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
17 changes: 15 additions & 2 deletions src/common/string-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ class StringBuilder {
return this;
}

makeUrl(url?: string) {
if(this.config.url && url) this.printQueue.push(url);
makeUrl(baseUrl?: string, url?: string) {
if(this.config.url && url) {
if(baseUrl) url = this.combineURLs(baseUrl, url);
this.printQueue.push(url);
}
return this;
}

Expand All @@ -69,6 +72,16 @@ class StringBuilder {
build() {
return this.printQueue.join(' ');
}

/**
* Helper imported from Axios library
* @see /~https://github.com/axios/axios/blob/d99d5faac29899eba68ce671e6b3cbc9832e9ad8/lib/helpers/combineURLs.js
* */
combineURLs(baseURL: string, relativeURL?: string): string {
return relativeURL
? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
: baseURL;
};
}

export default StringBuilder;
13 changes: 13 additions & 0 deletions src/logger/__test__/error.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,16 @@ test('if custom logger is respected', () => {
expect(printLog).not.toHaveBeenCalled();
expect(customPrintLog).toHaveBeenCalled();
});

test('if baseUrl is taken into consideration', () => {
errorLoggerWithoutPromise({
...axiosError,
config: {
...axiosError.config,
baseURL: '/~https://github.com/',
url: '/hg-pyun',
},
});
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.stringContaining(axiosError.config.url));
});
6 changes: 6 additions & 0 deletions src/logger/__test__/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ test('if custom logger is respected', () => {
expect(printLog).not.toHaveBeenCalled();
expect(customPrintLog).toHaveBeenCalled();
});

test('if baseUrl is taken into consideration', () => {
requestLogger({ ...axiosRequestConfig, baseURL: '/~https://github.com/', url: '/hg-pyun' });
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.stringContaining(axiosRequestConfig.url));
});
9 changes: 9 additions & 0 deletions src/logger/__test__/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,12 @@ test('if custom logger is respected', () => {
expect(printLog).not.toHaveBeenCalled();
expect(customPrintLog).toHaveBeenCalled();
});

test('if baseUrl is taken into consideration', () => {
responseLogger({
...axiosResponse,
config: { ...axiosResponse.config, baseURL: '/~https://github.com/', url: '/hg-pyun' },
});
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.stringContaining(axiosResponse.config.url));
});
4 changes: 2 additions & 2 deletions src/logger/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import StringBuilder from '../common/string-builder';

function errorLoggerWithoutPromise(error: AxiosError, config: ErrorLogConfig = {}) {

const {config: { method, url }, response} = error;
const {config: { method, baseURL, url }, response} = error;

let status, statusText, data, headers;
if (response) {
Expand All @@ -22,7 +22,7 @@ function errorLoggerWithoutPromise(error: AxiosError, config: ErrorLogConfig = {
.makeLogTypeWithPrefix('Error')
.makeDateFormat(new Date())
.makeMethod(method)
.makeUrl(url)
.makeUrl(baseURL, url)
.makeStatus(status, statusText)
.makeHeader(headers)
.makeData(data)
Expand Down
4 changes: 2 additions & 2 deletions src/logger/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import StringBuilder from '../common/string-builder';

function requestLogger(request: AxiosRequestConfig, config: RequestLogConfig = {}) {

const {url, method, data, headers} = request;
const {baseURL, url, method, data, headers} = request;
const buildConfig = assembleBuildConfig(config);

const stringBuilder = new StringBuilder(buildConfig);
const log = stringBuilder
.makeLogTypeWithPrefix('Request')
.makeDateFormat(new Date())
.makeMethod(method)
.makeUrl(url)
.makeUrl(baseURL, url)
.makeHeader(headers)
.makeData(data)
.build();
Expand Down
4 changes: 2 additions & 2 deletions src/logger/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { assembleBuildConfig } from '../common/config';
import StringBuilder from '../common/string-builder';

function responseLogger(response: AxiosResponse, config: ResponseLogConfig = {}) {
const {config: {url, method}, status, statusText, data, headers} = response;
const {config: {baseURL, url, method}, status, statusText, data, headers} = response;
const buildConfig = assembleBuildConfig(config);

const stringBuilder = new StringBuilder(buildConfig);
const log = stringBuilder
.makeLogTypeWithPrefix('Response')
.makeDateFormat(new Date())
.makeMethod(method)
.makeUrl(url)
.makeUrl(baseURL, url)
.makeStatus(status, statusText)
.makeHeader(headers)
.makeData(data)
Expand Down

0 comments on commit 5f1ba4e

Please sign in to comment.