Skip to content

Commit

Permalink
Feature/configuration (#30)
Browse files Browse the repository at this point in the history
* Merge two config field

* Add configuration settings

* Update version

* Add new build files
  • Loading branch information
hg-pyun authored Jul 14, 2019
1 parent ebe1d56 commit 89ee5ac
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 30 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,37 @@ instance.interceptors.response.use(AxiosLogger.responseLogger, (err) => {
});
```

## Configuration Settings

You can adjust several features as desired through configuration file.
If you want to set config globally, using `setGlobalConfig` method.

```javascript
setGlobalConfig({
prefixText: 'your prefix',
dateFormat: 'HH:MM:ss',
status: false,
});
```

Or it can also be passed on as a second argument and applied locally.

```javascript
instance.interceptors.request.use((config) => {
// write down your request intercept.
return AxiosLogger.requestLogger(config, {
prefixText: 'your prefix',
dateFormat: 'HH:MM:ss',
status: false,
});
});
```

#### Enable config list

- prefixText: string | false (default Axios)
- dateFormat: [dateformat](https://github.com/felixge/node-dateformat) | false (default isoDateTime)

## CONTRIBUTE

I always welcome Feedback and Pull Request :)
24 changes: 8 additions & 16 deletions lib/common/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ declare function mergeWithGlobalConfig(config?: RequestLogConfig | ResponseLogCo
status?: boolean | undefined;
statusText?: boolean | undefined;
code?: boolean | undefined;
usePrefix?: boolean | undefined;
prefixText?: string | undefined;
useDate?: boolean | undefined;
dateFormat?: string | undefined;
prefixText?: string | boolean | undefined;
dateFormat?: string | boolean | undefined;
} | {
data?: boolean | undefined;
url?: boolean | undefined;
method?: boolean | undefined;
headers?: boolean | undefined;
usePrefix?: boolean | undefined;
prefixText?: string | undefined;
useDate?: boolean | undefined;
dateFormat?: string | undefined;
prefixText?: string | boolean | undefined;
dateFormat?: string | boolean | undefined;
status?: boolean | undefined;
statusText?: boolean | undefined;
code?: boolean | undefined;
Expand All @@ -30,20 +26,16 @@ declare function mergeWithGlobalConfig(config?: RequestLogConfig | ResponseLogCo
status?: boolean | undefined;
statusText?: boolean | undefined;
headers?: boolean | undefined;
usePrefix?: boolean | undefined;
prefixText?: string | undefined;
useDate?: boolean | undefined;
dateFormat?: string | undefined;
prefixText?: string | boolean | undefined;
dateFormat?: string | boolean | undefined;
url?: boolean | undefined;
method?: boolean | undefined;
code?: boolean | undefined;
} | {
data?: boolean | undefined;
code?: boolean | undefined;
usePrefix?: boolean | undefined;
prefixText?: string | undefined;
useDate?: boolean | undefined;
dateFormat?: string | undefined;
prefixText?: string | boolean | undefined;
dateFormat?: string | boolean | undefined;
url?: boolean | undefined;
method?: boolean | undefined;
headers?: boolean | undefined;
Expand Down
2 changes: 1 addition & 1 deletion lib/common/string-builder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare class StringBuilder {
private config;
private printQueue;
constructor(config: GlobalLogConfig);
makePrefix(logType: string): this;
makePrefix(logType: string | false): this;
makeDateFormat(): this;
makeUrl(url?: string): this;
makeMethod(method?: string): this;
Expand Down
6 changes: 4 additions & 2 deletions lib/common/string-builder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions lib/common/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export interface CommonConfig {
usePrefix?: boolean;
prefixText?: string;
useDate?: boolean;
dateFormat?: string;
prefixText?: string | boolean;
dateFormat?: string | boolean;
}
export interface GlobalLogConfig extends CommonConfig {
data?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "axios-logger",
"version": "2.0.1",
"version": "2.1.0",
"description": "Beautify Axios Logging Messages",
"main": "lib/index.js",
"keywords": [
Expand Down
5 changes: 3 additions & 2 deletions src/common/string-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ class StringBuilder {
this.printQueue = [];
}

makePrefix(logType: string) {
const prefix = this.config.prefixText ? `[${this.config.prefixText}][${logType}]` : `[Axios][${logType}]`;
makePrefix(logType: string | false) {
const prefix = this.config.prefixText === false ? `[${logType}]` : `[${this.config.prefixText || 'Axios'}][${logType}]`;
this.printQueue.push(chalk.green(prefix));
return this;
}

makeDateFormat() {
// @ts-ignore
const dateFormat = dateformat(new Date(), this.config.dateFormat || 'isoDateTime');
this.printQueue.push(dateFormat);
return this;
Expand Down
6 changes: 2 additions & 4 deletions src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export interface CommonConfig {
usePrefix?: boolean,
prefixText?: string,
useDate?: boolean,
dateFormat?: string,
prefixText?: string | boolean,
dateFormat?: string | boolean,
}

export interface GlobalLogConfig extends CommonConfig {
Expand Down
11 changes: 11 additions & 0 deletions src/logger/__test__/error.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,14 @@ test('if both global and local config are defined, local config should override
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.stringContaining('[local custom prefix]'));
});

test('if prefixText is false, remove prefix', () => {
const globalConfig = {
prefixText: false,
};

setGlobalConfig(globalConfig);
errorLoggerWithoutPromise(axiosError);
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.not.stringContaining('[Axios]'));
});
11 changes: 11 additions & 0 deletions src/logger/__test__/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,14 @@ test('if both global and local config are defined, local config should override
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.stringContaining('[local custom prefix]'));
});

test('if prefixText is false, remove prefix', () => {
const globalConfig = {
prefixText: false,
};

setGlobalConfig(globalConfig);
requestLogger(axiosRequestConfig);
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.not.stringContaining('[Axios]'));
});
11 changes: 11 additions & 0 deletions src/logger/__test__/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,14 @@ test('if both global and local config are defined, local config should override
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.stringContaining('[local custom prefix]'));
});

test('if prefixText is false, remove prefix', () => {
const globalConfig = {
prefixText: false,
};

setGlobalConfig(globalConfig);
responseLogger(axiosResponse);
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.not.stringContaining('[Axios]'));
});

0 comments on commit 89ee5ac

Please sign in to comment.