Skip to content

Commit

Permalink
Respect GlobalLogConfig (#76)
Browse files Browse the repository at this point in the history
* Add all properties into default global log config

* Remove unused code property from configs

* Respect status and statusText config

* Make logger optional in configs

* Fix custom logger use

Co-authored-by: Haegul Pyun <[email protected]>
  • Loading branch information
gurisko and hg-pyun authored Dec 23, 2020
1 parent 4115102 commit 77b5a31
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 31 deletions.
3 changes: 3 additions & 0 deletions src/common/__test__/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('Default globalConfig properties should be all true + console should be the
url: true,
data: true,
status: true,
statusText: true,
logger: console.log,
dateFormat: false,
prefixText: DEFAULT_PREFIX,
Expand All @@ -29,6 +30,7 @@ test('setGlobalConfig should set config. getGlobalConfig should return globalCon
url: false,
data: true,
status: true,
statusText: true,
logger: customLoggerFunction,
dateFormat: false,
prefixText: DEFAULT_PREFIX,
Expand All @@ -54,6 +56,7 @@ test('assembleBuildConfig should return merged with globalConfig object.', () =>
url: true,
data: false,
status: true,
statusText: true,
logger: console.log,
dateFormat: 'hh:mm:ss',
prefixText: DEFAULT_PREFIX,
Expand Down
5 changes: 3 additions & 2 deletions src/common/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ErrorLogConfig, GlobalLogConfig, RequestLogConfig, ResponseLogConfig } from './types';

let globalConfig: GlobalLogConfig = {
let globalConfig: Required<GlobalLogConfig> = {
method: true,
url: true,
data: true,
status: true,
statusText: true,
logger: console.log,
prefixText: 'Axios',
dateFormat: false,
Expand All @@ -22,7 +23,7 @@ function setGlobalConfig(config: GlobalLogConfig) {
};
}

function assembleBuildConfig(config?: RequestLogConfig | ResponseLogConfig | ErrorLogConfig) {
function assembleBuildConfig(config: RequestLogConfig | ResponseLogConfig | ErrorLogConfig): Required<GlobalLogConfig> {
return {
...globalConfig,
...config,
Expand Down
9 changes: 0 additions & 9 deletions src/common/print.ts

This file was deleted.

6 changes: 4 additions & 2 deletions src/common/string-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ class StringBuilder {
return this;
}

makeStatus(status?:number, statusText?: string) {
if(status && statusText) this.printQueue.push(`${status}:${statusText}`);
makeStatus(status?: number, statusText?: string) {
if(this.config.status && this.config.statusText && status && statusText) this.printQueue.push(`${status}:${statusText}`);
else if(this.config.status && status) this.printQueue.push(`${status}`);
else if(this.config.statusText && statusText) this.printQueue.push(statusText);
return this;
}

Expand Down
4 changes: 1 addition & 3 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface CommonConfig {
prefixText?: string | boolean,
dateFormat?: string | boolean,
headers?: boolean,
logger: (text: string) => any,
logger?: (text: string) => any,
}

export interface GlobalLogConfig extends CommonConfig {
Expand All @@ -11,7 +11,6 @@ export interface GlobalLogConfig extends CommonConfig {
url?: boolean,
status?: boolean,
statusText?: boolean,
code?: boolean,
}

export interface RequestLogConfig extends CommonConfig {
Expand All @@ -28,5 +27,4 @@ export interface ResponseLogConfig extends CommonConfig {

export interface ErrorLogConfig extends CommonConfig {
data?: boolean,
code?: boolean,
}
12 changes: 10 additions & 2 deletions src/logger/__test__/error.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { setGlobalConfig } from '../../index';
import { printLog } from '../../common/print';
import { errorLoggerWithoutPromise } from '../error';

jest.mock('../../common/print');
const printLog = jest.fn(() => {});

const axiosError = {
code: 500,
Expand All @@ -20,6 +19,7 @@ const axiosError = {

beforeEach(() => {
printLog.mockClear();
setGlobalConfig({ logger: printLog });
});

test('response should be return immutable axiosError', () => {
Expand Down Expand Up @@ -89,3 +89,11 @@ test('if prefixText is false, remove prefix', () => {
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.not.stringContaining('[Axios]'));
});

test('if custom logger is respected', () => {
const customPrintLog = jest.fn(() => {});

errorLoggerWithoutPromise(axiosError, { logger: customPrintLog });
expect(printLog).not.toHaveBeenCalled();
expect(customPrintLog).toHaveBeenCalled();
});
12 changes: 10 additions & 2 deletions src/logger/__test__/request.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { requestLogger, setGlobalConfig } from '../../index';
import { printLog } from '../../common/print';

jest.mock('../../common/print');
const printLog = jest.fn(() => {});

const axiosRequestConfig = {
data: {
Expand All @@ -14,6 +13,7 @@ const axiosRequestConfig = {

beforeEach(() => {
printLog.mockClear();
setGlobalConfig({ logger: printLog });
});

test('request should be return immutable AxiosRequestConfig', () => {
Expand Down Expand Up @@ -77,3 +77,11 @@ test('if prefixText is false, remove prefix', () => {
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.not.stringContaining('[Axios]'));
});

test('if custom logger is respected', () => {
const customPrintLog = jest.fn(() => {});

requestLogger(axiosRequestConfig, { logger: customPrintLog });
expect(printLog).not.toHaveBeenCalled();
expect(customPrintLog).toHaveBeenCalled();
});
12 changes: 10 additions & 2 deletions src/logger/__test__/response.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { responseLogger, setGlobalConfig } from '../../index';
import { printLog } from '../../common/print';

jest.mock('../../common/print');
const printLog = jest.fn(() => {});

const axiosResponse = {
config: {
Expand All @@ -16,6 +15,7 @@ const axiosResponse = {

beforeEach(() => {
printLog.mockClear();
setGlobalConfig({ logger: printLog });
});

test('response should be return immutable AxiosResponse', () => {
Expand Down Expand Up @@ -87,3 +87,11 @@ test('if prefixText is false, remove prefix', () => {
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.not.stringContaining('[Axios]'));
});

test('if custom logger is respected', () => {
const customPrintLog = jest.fn(() => {});

responseLogger(axiosResponse, { logger: customPrintLog });
expect(printLog).not.toHaveBeenCalled();
expect(customPrintLog).toHaveBeenCalled();
});
5 changes: 2 additions & 3 deletions src/logger/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { AxiosError } from 'axios';
import { ErrorLogConfig } from '../common/types';
import { assembleBuildConfig } from '../common/config';
import StringBuilder from '../common/string-builder';
import { printLog } from '../common/print';

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

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

Expand All @@ -29,7 +28,7 @@ function errorLoggerWithoutPromise(error: AxiosError, config?: ErrorLogConfig) {
.makeData(data)
.build();

printLog(log);
buildConfig.logger(log);

return error;
}
Expand Down
5 changes: 2 additions & 3 deletions src/logger/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { AxiosRequestConfig } from 'axios';
import { RequestLogConfig } from '../common/types';
import { assembleBuildConfig } from '../common/config';
import StringBuilder from '../common/string-builder';
import { printLog } from '../common/print';

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

const {url, method, data, headers} = request;
const buildConfig = assembleBuildConfig(config);
Expand All @@ -19,7 +18,7 @@ function requestLogger(request: AxiosRequestConfig, config?: RequestLogConfig) {
.makeData(data)
.build();

printLog(log);
buildConfig.logger(log);

return request;
}
Expand Down
5 changes: 2 additions & 3 deletions src/logger/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { AxiosResponse } from 'axios';
import { ResponseLogConfig } from '../common/types';
import { assembleBuildConfig } from '../common/config';
import StringBuilder from '../common/string-builder';
import { printLog } from '../common/print';

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

Expand All @@ -19,7 +18,7 @@ function responseLogger(response: AxiosResponse, config?: ResponseLogConfig) {
.makeData(data)
.build();

printLog(log);
buildConfig.logger(log);

return response;
}
Expand Down

0 comments on commit 77b5a31

Please sign in to comment.