Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use baseUrl to display full URL #97

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(url?: string, baseUrl?: 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(url, baseURL)
.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(url, baseURL)
.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(url, baseURL)
.makeStatus(status, statusText)
.makeHeader(headers)
.makeData(data)
Expand Down