From 77b5a3152ec6c603cd1a2409e231b4614eb4b1b3 Mon Sep 17 00:00:00 2001 From: Matus Gura Date: Wed, 23 Dec 2020 07:41:17 +0100 Subject: [PATCH] Respect GlobalLogConfig (#76) * 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 --- src/common/__test__/config.spec.js | 3 +++ src/common/config.ts | 5 +++-- src/common/print.ts | 9 --------- src/common/string-builder.ts | 6 ++++-- src/common/types.ts | 4 +--- src/logger/__test__/error.spec.js | 12 ++++++++++-- src/logger/__test__/request.spec.js | 12 ++++++++++-- src/logger/__test__/response.spec.js | 12 ++++++++++-- src/logger/error.ts | 5 ++--- src/logger/request.ts | 5 ++--- src/logger/response.ts | 5 ++--- 11 files changed, 47 insertions(+), 31 deletions(-) delete mode 100644 src/common/print.ts diff --git a/src/common/__test__/config.spec.js b/src/common/__test__/config.spec.js index 81bd640..0038fd2 100644 --- a/src/common/__test__/config.spec.js +++ b/src/common/__test__/config.spec.js @@ -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, @@ -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, @@ -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, diff --git a/src/common/config.ts b/src/common/config.ts index 9b9410c..942a660 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -1,10 +1,11 @@ import { ErrorLogConfig, GlobalLogConfig, RequestLogConfig, ResponseLogConfig } from './types'; -let globalConfig: GlobalLogConfig = { +let globalConfig: Required = { method: true, url: true, data: true, status: true, + statusText: true, logger: console.log, prefixText: 'Axios', dateFormat: false, @@ -22,7 +23,7 @@ function setGlobalConfig(config: GlobalLogConfig) { }; } -function assembleBuildConfig(config?: RequestLogConfig | ResponseLogConfig | ErrorLogConfig) { +function assembleBuildConfig(config: RequestLogConfig | ResponseLogConfig | ErrorLogConfig): Required { return { ...globalConfig, ...config, diff --git a/src/common/print.ts b/src/common/print.ts deleted file mode 100644 index 7ecc967..0000000 --- a/src/common/print.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { getGlobalConfig } from './config'; - -function printLog (text: string) { - getGlobalConfig().logger(text); -} - -export { - printLog, -} diff --git a/src/common/string-builder.ts b/src/common/string-builder.ts index dcd4550..c373882 100644 --- a/src/common/string-builder.ts +++ b/src/common/string-builder.ts @@ -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; } diff --git a/src/common/types.ts b/src/common/types.ts index afe5e6b..0d50812 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -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 { @@ -11,7 +11,6 @@ export interface GlobalLogConfig extends CommonConfig { url?: boolean, status?: boolean, statusText?: boolean, - code?: boolean, } export interface RequestLogConfig extends CommonConfig { @@ -28,5 +27,4 @@ export interface ResponseLogConfig extends CommonConfig { export interface ErrorLogConfig extends CommonConfig { data?: boolean, - code?: boolean, } diff --git a/src/logger/__test__/error.spec.js b/src/logger/__test__/error.spec.js index 17b8e39..3559e54 100644 --- a/src/logger/__test__/error.spec.js +++ b/src/logger/__test__/error.spec.js @@ -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, @@ -20,6 +19,7 @@ const axiosError = { beforeEach(() => { printLog.mockClear(); + setGlobalConfig({ logger: printLog }); }); test('response should be return immutable axiosError', () => { @@ -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(); +}); diff --git a/src/logger/__test__/request.spec.js b/src/logger/__test__/request.spec.js index f7697f5..f346fc9 100644 --- a/src/logger/__test__/request.spec.js +++ b/src/logger/__test__/request.spec.js @@ -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: { @@ -14,6 +13,7 @@ const axiosRequestConfig = { beforeEach(() => { printLog.mockClear(); + setGlobalConfig({ logger: printLog }); }); test('request should be return immutable AxiosRequestConfig', () => { @@ -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(); +}); diff --git a/src/logger/__test__/response.spec.js b/src/logger/__test__/response.spec.js index 30cf460..776aaef 100644 --- a/src/logger/__test__/response.spec.js +++ b/src/logger/__test__/response.spec.js @@ -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: { @@ -16,6 +15,7 @@ const axiosResponse = { beforeEach(() => { printLog.mockClear(); + setGlobalConfig({ logger: printLog }); }); test('response should be return immutable AxiosResponse', () => { @@ -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(); +}); diff --git a/src/logger/error.ts b/src/logger/error.ts index 43f320a..bf80fd8 100644 --- a/src/logger/error.ts +++ b/src/logger/error.ts @@ -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; @@ -29,7 +28,7 @@ function errorLoggerWithoutPromise(error: AxiosError, config?: ErrorLogConfig) { .makeData(data) .build(); - printLog(log); + buildConfig.logger(log); return error; } diff --git a/src/logger/request.ts b/src/logger/request.ts index bc47e4e..6ba58a7 100644 --- a/src/logger/request.ts +++ b/src/logger/request.ts @@ -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); @@ -19,7 +18,7 @@ function requestLogger(request: AxiosRequestConfig, config?: RequestLogConfig) { .makeData(data) .build(); - printLog(log); + buildConfig.logger(log); return request; } diff --git a/src/logger/response.ts b/src/logger/response.ts index 4a62b41..14c30a5 100644 --- a/src/logger/response.ts +++ b/src/logger/response.ts @@ -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); @@ -19,7 +18,7 @@ function responseLogger(response: AxiosResponse, config?: ResponseLogConfig) { .makeData(data) .build(); - printLog(log); + buildConfig.logger(log); return response; }