Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dataLimit parameter that truncates the data if it is too large. #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,19 @@ instance.interceptors.request.use((request) => {

#### Enable config list

| Property | Type | Default | Description |
| ------------ | ------------------------------------------------------------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `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. |
| `statusText` | boolean | `true` | Whether to include response status text or not. |
| `headers` | boolean | `false` | Whether to include HTTP headers or not. |
| `prefixText` | string \| `false` | `'Axios'` | `false` => no prefix, otherwise, customize the prefix wanted. |
| `dateFormat` | [dateformat](https://github.com/felixge/node-dateformat) \| `false` | `false` | `false` => no timestamp, otherwise, customize its format |
| `logger` | function<string, any> | `console.log` | Allows users to customize the logger function to be used. e.g. Winston's `logger.info` could be leveraged, like this: `logger.info.bind(this)` |
| Property | Type | Default | Description |
| ------------ | ------------------------------------------------------------------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `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. |
| `dataLimit` | number | `0` (no limit) | If `data` equals `true` then the request/response data is truncated if length is bigger than `dataLimit`. |
| `status` | boolean | `true` | Whether to include response statuses or not. |
| `statusText` | boolean | `true` | Whether to include response status text or not. |
| `headers` | boolean | `false` | Whether to include HTTP headers or not. |
| `prefixText` | string \| `false` | `'Axios'` | `false` => no prefix, otherwise, customize the prefix wanted. |
| `dateFormat` | [dateformat](https://github.com/felixge/node-dateformat) \| `false` | `false` | `false` => no timestamp, otherwise, customize its format |
| `logger` | function<string, any> | `console.log` | Allows users to customize the logger function to be used. e.g. Winston's `logger.info` could be leveraged, like this: `logger.info.bind(this)` |

## CONTRIBUTE

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

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 equal to default values', () =>
url: true,
params: false,
data: true,
dataLimit: 0,
status: true,
statusText: true,
logger: console.log,
Expand All @@ -31,6 +32,7 @@ test('setGlobalConfig should set config. getGlobalConfig should return globalCon
url: false,
params: false,
data: true,
dataLimit: 0,
status: true,
statusText: true,
logger: customLoggerFunction,
Expand Down Expand Up @@ -58,6 +60,7 @@ test('assembleBuildConfig should return merged with globalConfig object.', () =>
url: true,
params: false,
data: false,
dataLimit: 0,
status: true,
statusText: true,
logger: console.log,
Expand Down
35 changes: 35 additions & 0 deletions src/common/__test__/string-builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,38 @@ test('makeData should not stringify data if configured not to', () => {
const result = sb.makeData(a).build();
expect(result).toEqual('');
});

test('makeData should truncate if data length exceeds dataLimit', () => {
const config = {
...getGlobalConfig(),
data: true,
dataLimit: 10,
};
const a = '12345678901234567890';
const sb = new StringBuilder(config);
const result = sb.makeData(a).build();
expect(result).toEqual('1234567890...');
});

test('makeData should not truncate if dataLimit is not specified', () => {
const config = {
...getGlobalConfig(),
data: true,
};
const a = '12345678901234567890';
const sb = new StringBuilder(config);
const result = sb.makeData(a).build();
expect(result).toEqual(a);
});

test('makeData should not crash if data has circular references', () => {
const config = {
...getGlobalConfig(),
data: true,
};
const a = {};
a.b = { a };
const sb = new StringBuilder(config);
const result = sb.makeData(a).build();
expect(result).toEqual('');
});
1 change: 1 addition & 0 deletions src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let globalConfig: Required<GlobalLogConfig> = {
url: true,
params: false,
data: true,
dataLimit: 0,
status: true,
statusText: true,
logger: console.log,
Expand Down
17 changes: 15 additions & 2 deletions src/common/string-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,22 @@ class StringBuilder {
}

makeData(data: object) {
if(this.config.data && data) {
const str = typeof data === `string` ? data : JSON.stringify(data);
if (this.config.data && data) {
let str;
try {
str = typeof data === `string` ? data : JSON.stringify(data);
} catch (e) {
// stringify failed, ignore
return this;
}

let dataLimit = this.config.dataLimit;
if (dataLimit && str?.length > dataLimit) {
str = str.substring(0, dataLimit) + '...';
}

this.printQueue.push(str);

}
return this;
}
Expand Down
4 changes: 4 additions & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface CommonConfig {

export interface GlobalLogConfig extends CommonConfig {
data?: boolean,
dataLimit?: number;
method?: boolean,
url?: boolean,
status?: boolean,
Expand All @@ -16,18 +17,21 @@ export interface GlobalLogConfig extends CommonConfig {

export interface RequestLogConfig extends CommonConfig {
data?: boolean,
dataLimit?: number;
method?: boolean,
url?: boolean,
}

export interface ResponseLogConfig extends CommonConfig {
data?: boolean,
dataLimit?: number;
status?: boolean,
statusText?: boolean,
}

export interface ErrorLogConfig extends CommonConfig {
data?: boolean,
dataLimit?: number;
status?: boolean,
statusText?: boolean,
}