Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ instance.interceptors.request.use((request) => {
| ------------ | ------------------------------------------------------------------- | -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `method` | boolean | `true` | Whether to include HTTP method or not. |
| `url` | boolean | `true` | Whether to include the URL or not. |
| `params` | boolean | `false` | Whether to include the URL params or not. |
| `data` | boolean | `true` | Whether to include request/response data or not. |
| `status` | boolean | `true` | Whether to include response statuses or not. |
| `headers` | boolean | `false` | Whether to include HTTP headers or not. |
Expand Down
5 changes: 4 additions & 1 deletion src/common/__test__/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { getGlobalConfig, assembleBuildConfig, setGlobalConfig } from '../config
const DEFAULT_PREFIX = 'Axios';
const customLoggerFunction = console.info;

test('Default globalConfig properties should be all true + console should be the logger', () => {
test('Default globalConfig properties should be equal to default values', () => {
expect(getGlobalConfig()).toEqual({
method: true,
url: true,
params: false,
data: true,
status: true,
statusText: true,
Expand All @@ -28,6 +29,7 @@ test('setGlobalConfig should set config. getGlobalConfig should return globalCon
expect(getGlobalConfig()).toEqual({
method: true,
url: false,
params: false,
data: true,
status: true,
statusText: true,
Expand All @@ -54,6 +56,7 @@ test('assembleBuildConfig should return merged with globalConfig object.', () =>
expect(buildConfig).toEqual({
method: true,
url: true,
params: false,
data: false,
status: true,
statusText: true,
Expand Down
8 changes: 8 additions & 0 deletions src/common/__test__/string-builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ test('makeUrl should add url', () => {
expect(result).toContain('https://github.com/hg-pyun');
});

test('makeParams should add params', () => {
const sb = new StringBuilder({ params: true });
const params = { param1: 'value1', param2: 'value2' };
const result = sb.makeParams(params).build();

expect(result).toContain(JSON.stringify(params));
});

test('makeMethod should add method with upper case', () => {
const sb = new StringBuilder(getGlobalConfig());
const result = sb.makeMethod('get').build();
Expand Down
1 change: 1 addition & 0 deletions src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ErrorLogConfig, GlobalLogConfig, RequestLogConfig, ResponseLogConfig }
let globalConfig: Required<GlobalLogConfig> = {
method: true,
url: true,
params: false,
data: true,
status: true,
statusText: true,
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 @@ -51,6 +51,11 @@ class StringBuilder {
return this;
}

makeParams(params?: object) {
if(this.config.params && params) this.printQueue.push(JSON.stringify(params));
return this;
}

makeMethod(method?: string) {
if(this.config.method && method) this.printQueue.push(chalk.yellow(method.toUpperCase()));
return this;
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,
params?: boolean,
}

export interface GlobalLogConfig extends CommonConfig {
Expand Down
3 changes: 2 additions & 1 deletion 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, baseURL, url }, response} = error;
const {config: { method, baseURL, params, url }, response} = error;

let status, statusText, data, headers;
if (response) {
Expand All @@ -23,6 +23,7 @@ function errorLoggerWithoutPromise(error: AxiosError, config: ErrorLogConfig = {
.makeDateFormat(new Date())
.makeMethod(method)
.makeUrl(url, baseURL)
.makeParams(params)
.makeStatus(status, statusText)
.makeHeader(headers)
.makeData(data)
Expand Down
3 changes: 2 additions & 1 deletion src/logger/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import StringBuilder from '../common/string-builder';

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

const {baseURL, url, method, data, headers} = request;
const {baseURL, url, params, method, data, headers} = request;
const buildConfig = assembleBuildConfig(config);

const stringBuilder = new StringBuilder(buildConfig);
Expand All @@ -14,6 +14,7 @@ function requestLogger(request: AxiosRequestConfig, config: RequestLogConfig = {
.makeDateFormat(new Date())
.makeMethod(method)
.makeUrl(url, baseURL)
.makeParams(params)
.makeHeader(headers)
.makeData(data)
.build();
Expand Down
4 changes: 3 additions & 1 deletion src/logger/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { assembleBuildConfig } from '../common/config';
import StringBuilder from '../common/string-builder';

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

const buildConfig = assembleBuildConfig(config);

const stringBuilder = new StringBuilder(buildConfig);
Expand All @@ -13,6 +14,7 @@ function responseLogger(response: AxiosResponse, config: ResponseLogConfig = {})
.makeDateFormat(new Date())
.makeMethod(method)
.makeUrl(url, baseURL)
.makeParams(params)
.makeStatus(status, statusText)
.makeHeader(headers)
.makeData(data)
Expand Down