Skip to content
Closed
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
3 changes: 3 additions & 0 deletions src/common/__test__/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test('Default globalConfig properties should be all true + console should be the
dateFormat: false,
prefixText: DEFAULT_PREFIX,
headers: false,
responseTime: true,
});
});

Expand All @@ -35,6 +36,7 @@ test('setGlobalConfig should set config. getGlobalConfig should return globalCon
dateFormat: false,
prefixText: DEFAULT_PREFIX,
headers: false,
responseTime: true,
});
});

Expand All @@ -61,5 +63,6 @@ test('assembleBuildConfig should return merged with globalConfig object.', () =>
dateFormat: 'hh:mm:ss',
prefixText: DEFAULT_PREFIX,
headers: false,
responseTime: true,
});
});
1 change: 1 addition & 0 deletions src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let globalConfig: Required<GlobalLogConfig> = {
prefixText: 'Axios',
dateFormat: false,
headers: false,
responseTime: true,
};

function getGlobalConfig() {
Expand Down
5 changes: 5 additions & 0 deletions src/common/string-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class StringBuilder {
return this;
}

makeResponseTime(startDateTime?: number, endDateTime?: number) {
if(this.config.responseTime && startDateTime && endDateTime) this.printQueue.push(chalk.yellow(`${endDateTime - startDateTime} ms`));
return this;
}

build() {
return this.printQueue.join(' ');
}
Expand Down
1 change: 1 addition & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface CommonConfig {
dateFormat?: string | boolean,
headers?: boolean,
logger?: (text: string) => any,
responseTime?: boolean,
}

export interface GlobalLogConfig extends CommonConfig {
Expand Down
1 change: 1 addition & 0 deletions src/logger/__test__/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const axiosRequestConfig = {
},
method: 'GET',
url: 'https://github.com/hg-pyun',
headers: [],
};

beforeEach(() => {
Expand Down
1 change: 1 addition & 0 deletions src/logger/__test__/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const axiosResponse = {
config: {
url: 'https://github.com/hg-pyun',
method: 'GET',
headers: [],
},
data: 'dummy data',
status: 500,
Expand Down
2 changes: 2 additions & 0 deletions src/logger/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function requestLogger(request: AxiosRequestConfig, config: RequestLogConfig = {
const {url, method, data, headers} = request;
const buildConfig = assembleBuildConfig(config);

request.headers['Request-Start-Date-Time'] = new Date().getTime();

const stringBuilder = new StringBuilder(buildConfig);
const log = stringBuilder
.makeLogTypeWithPrefix('Request')
Expand Down
1 change: 1 addition & 0 deletions src/logger/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function responseLogger(response: AxiosResponse, config: ResponseLogConfig = {})
.makeDateFormat(new Date())
.makeMethod(method)
.makeUrl(url)
.makeResponseTime(response.config.headers['Request-Start-Date-Time'], new Date().getTime())
.makeStatus(status, statusText)
.makeHeader(headers)
.makeData(data)
Expand Down