diff --git a/src/common/string-builder.ts b/src/common/string-builder.ts index 3031549..9e96ce7 100644 --- a/src/common/string-builder.ts +++ b/src/common/string-builder.ts @@ -43,8 +43,11 @@ class StringBuilder { return this; } - makeUrl(url?: string) { - if(this.config.url && url) this.printQueue.push(url); + makeUrl(url?: string, baseUrl?: string) { + if(this.config.url && url) { + if(baseUrl) url = this.combineURLs(baseUrl, url); + this.printQueue.push(url); + } return this; } @@ -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; diff --git a/src/logger/__test__/error.spec.js b/src/logger/__test__/error.spec.js index 3559e54..39ad9ef 100644 --- a/src/logger/__test__/error.spec.js +++ b/src/logger/__test__/error.spec.js @@ -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)); +}); diff --git a/src/logger/__test__/request.spec.js b/src/logger/__test__/request.spec.js index f346fc9..72299c3 100644 --- a/src/logger/__test__/request.spec.js +++ b/src/logger/__test__/request.spec.js @@ -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)); +}); diff --git a/src/logger/__test__/response.spec.js b/src/logger/__test__/response.spec.js index 776aaef..54ecd61 100644 --- a/src/logger/__test__/response.spec.js +++ b/src/logger/__test__/response.spec.js @@ -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)); +}); diff --git a/src/logger/error.ts b/src/logger/error.ts index bf80fd8..ffb61d4 100644 --- a/src/logger/error.ts +++ b/src/logger/error.ts @@ -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) { @@ -22,7 +22,7 @@ function errorLoggerWithoutPromise(error: AxiosError, config: ErrorLogConfig = { .makeLogTypeWithPrefix('Error') .makeDateFormat(new Date()) .makeMethod(method) - .makeUrl(url) + .makeUrl(url, baseURL) .makeStatus(status, statusText) .makeHeader(headers) .makeData(data) diff --git a/src/logger/request.ts b/src/logger/request.ts index 6ba58a7..acc0914 100644 --- a/src/logger/request.ts +++ b/src/logger/request.ts @@ -5,7 +5,7 @@ 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); @@ -13,7 +13,7 @@ function requestLogger(request: AxiosRequestConfig, config: RequestLogConfig = { .makeLogTypeWithPrefix('Request') .makeDateFormat(new Date()) .makeMethod(method) - .makeUrl(url) + .makeUrl(url, baseURL) .makeHeader(headers) .makeData(data) .build(); diff --git a/src/logger/response.ts b/src/logger/response.ts index 14c30a5..3ac126f 100644 --- a/src/logger/response.ts +++ b/src/logger/response.ts @@ -4,7 +4,7 @@ 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); @@ -12,7 +12,7 @@ function responseLogger(response: AxiosResponse, config: ResponseLogConfig = {}) .makeLogTypeWithPrefix('Response') .makeDateFormat(new Date()) .makeMethod(method) - .makeUrl(url) + .makeUrl(url, baseURL) .makeStatus(status, statusText) .makeHeader(headers) .makeData(data)