Skip to content

Commit

Permalink
Version 2.2.0 (#35)
Browse files Browse the repository at this point in the history
* Fix request argument name

* Logging Header fields (#36)

* Add header print and configuration

* Add header usage
  • Loading branch information
hg-pyun authored Sep 30, 2019
1 parent d470619 commit a6c3fd4
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 12 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ instance.interceptors.request.use(AxiosLogger.requestLogger);
If you want to use your own interceptor, you can compose(mixin) with `requestLogger`.

```javascript
instance.interceptors.request.use((config) => {
instance.interceptors.request.use((request) => {
// write down your request intercept.
return AxiosLogger.requestLogger(config);
return AxiosLogger.requestLogger(request);
});
```

Expand Down Expand Up @@ -94,18 +94,20 @@ setGlobalConfig({
prefixText: 'your prefix',
dateFormat: 'HH:MM:ss',
status: false,
header: true,
});
```

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

```javascript
instance.interceptors.request.use((config) => {
instance.interceptors.request.use((request) => {
// write down your request intercept.
return AxiosLogger.requestLogger(config, {
return AxiosLogger.requestLogger(request, {
prefixText: 'your prefix',
dateFormat: 'HH:MM:ss',
status: false,
header: true,
});
});
```
Expand All @@ -114,6 +116,7 @@ instance.interceptors.request.use((config) => {

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

## CONTRIBUTE

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.1.0",
"version": "2.2.0",
"description": "Beautify Axios Logging Messages",
"main": "lib/index.js",
"keywords": [
Expand Down
16 changes: 16 additions & 0 deletions src/common/string-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import chalk from 'chalk';
class StringBuilder {
private config: GlobalLogConfig;
private printQueue: Array<string>;
private filteredHeaderList: Array<String>;

constructor(config: GlobalLogConfig) {
this.config = config;
this.printQueue = [];
this.filteredHeaderList = ['common', 'delete', 'get', 'head', 'post', 'put', 'patch', 'content-type', 'content-length', 'vary', 'date', 'connection', 'content-security-policy'];
}

makePrefix(logType: string | false) {
Expand All @@ -24,6 +26,20 @@ class StringBuilder {
return this;
}

makeHeader(headers?: { [key:string] : {value:string}}) {
if(this.config.headers && headers) {
const headerMap:{ [key:string] : {value:string}} = {};
for(let key in headers) {
if(!this.filteredHeaderList.includes(key)) {
headerMap[key] = headers[key];
}
}

this.printQueue.push(JSON.stringify(headerMap));
}
return this;
}

makeUrl(url?: string) {
if(url) this.printQueue.push(url);
return this;
Expand Down
4 changes: 1 addition & 3 deletions src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export interface CommonConfig {
prefixText?: string | boolean,
dateFormat?: string | boolean,
headers?: boolean,
}

export interface GlobalLogConfig extends CommonConfig {
data?: boolean,
url?: boolean,
method?: boolean,
headers?: boolean,
status?: boolean,
statusText?: boolean,
code?: boolean,
Expand All @@ -17,14 +17,12 @@ export interface RequestLogConfig extends CommonConfig {
data?: boolean,
url?: boolean,
method?: boolean,
headers?: boolean,
}

export interface ResponseLogConfig extends CommonConfig {
data?: boolean,
status?: boolean,
statusText?: boolean,
headers?: boolean,
}

export interface ErrorLogConfig extends CommonConfig {
Expand Down
4 changes: 3 additions & 1 deletion src/logger/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ function errorLoggerWithoutPromise(error: AxiosError, config?: ErrorLogConfig) {

const {config: {url, method}, response} = error;

let status, statusText, data;
let status, statusText, data, headers;
if(response){
status = response.status;
statusText = response.statusText;
data = response.data;
headers = response.headers;
}

const buildConfig = config ? config : mergeWithGlobalConfig(config);
Expand All @@ -24,6 +25,7 @@ function errorLoggerWithoutPromise(error: AxiosError, config?: ErrorLogConfig) {
.makeUrl(url)
.makeMethod(method)
.makeStatus(status, statusText)
.makeHeader(headers)
.makeData(data)
.build();

Expand Down
3 changes: 2 additions & 1 deletion src/logger/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { printLog } from '../common/print';

function requestLogger(request: AxiosRequestConfig, config?: RequestLogConfig) {

const {url, method, data} = request;
const {url, method, data, headers} = request;
const buildConfig = config ? config : mergeWithGlobalConfig(config);

const stringBuilder = new StringBuilder(buildConfig);
Expand All @@ -15,6 +15,7 @@ function requestLogger(request: AxiosRequestConfig, config?: RequestLogConfig) {
.makeDateFormat()
.makeUrl(url)
.makeMethod(method)
.makeHeader(headers)
.makeData(data)
.build();

Expand Down
3 changes: 2 additions & 1 deletion src/logger/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import StringBuilder from '../common/string-builder';
import { printLog } from '../common/print';

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

const stringBuilder = new StringBuilder(buildConfig);
Expand All @@ -15,6 +15,7 @@ function responseLogger(response: AxiosResponse, config?: ResponseLogConfig) {
.makeUrl(url)
.makeMethod(method)
.makeStatus(status, statusText)
.makeHeader(headers)
.makeData(data)
.build();

Expand Down
10 changes: 9 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ app.use(bodyParser.json({ limit: '50mb' }));
app.use(cookieParser());

app.get('/test/get', (req, res, next) => {
const instance = axios.create();
const instance = axios.create({
headers: { 'X-Custom-Header': 'foobar' },
});
instance.interceptors.request.use(AxiosLogger.requestLogger);
instance.interceptors.response.use(AxiosLogger.responseLogger);

Expand Down Expand Up @@ -52,13 +54,19 @@ app.get('/test/get/error', (req, res, next) => {
* /test/* -> /echo/*
*/
app.get('/echo/get', (req, res, next) => {
res.set({
'X-Custom-Response': 'foobar',
});
res.json({
status: 200,
message: 'echo get',
});
});

app.post('/echo/post', (req, res, next) => {
res.set({
'X-Custom-Response': 'foobar',
});
res.json({
status: 200,
message: 'echo post',
Expand Down

0 comments on commit a6c3fd4

Please sign in to comment.