axios-debug-log
Advanced tools
Comparing version 0.2.0 to 0.3.0
54
index.js
@@ -6,18 +6,36 @@ 'use strict' | ||
function logRequest (config) { | ||
debug(config.method.toUpperCase() + ' ' + config.url) | ||
return config | ||
var options = { | ||
request: function (debug, config) { | ||
debug(config.method.toUpperCase() + ' ' + config.url) | ||
}, | ||
response: function (debug, response) { | ||
debug( | ||
response.status + ' ' + response.statusText, | ||
'(' + response.config.method.toUpperCase() + ' ' + response.config.url + ')' | ||
) | ||
}, | ||
error: function (debug, error) { | ||
if (error.config) { | ||
debug( | ||
error.name + ': ' + error.message, | ||
'(' + error.config.method.toUpperCase() + ' ' + error.config.url + ')' | ||
) | ||
} else { | ||
debug(error.name + ': ' + error.message) | ||
} | ||
} | ||
} | ||
function logResponse (response) { | ||
debug( | ||
response.status + ' ' + response.statusText, | ||
'(' + response.config.method.toUpperCase() + ' ' + response.config.url + ')' | ||
) | ||
return response | ||
} | ||
function addLogger (instance) { | ||
instance.interceptors.request.use(logRequest) | ||
instance.interceptors.response.use(logResponse) | ||
instance.interceptors.request.use(function (config) { | ||
options.request(debug, config) | ||
return config | ||
}) | ||
instance.interceptors.response.use(function (response) { | ||
options.response(debug, response) | ||
return response | ||
}, function (error) { | ||
options.error(debug, error) | ||
throw error | ||
}) | ||
} | ||
@@ -29,3 +47,3 @@ | ||
return function create () { | ||
var instance = originalCreate.apply(axios, arguments) | ||
var instance = originalCreate.apply(this, arguments) | ||
addLogger(instance) | ||
@@ -35,1 +53,9 @@ return instance | ||
})(axios.create) | ||
module.exports = function (userOptions) { | ||
for (var key in options) { | ||
if (key in userOptions) { | ||
options[key] = userOptions[key] | ||
} | ||
} | ||
} |
{ | ||
"name": "axios-debug-log", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Axios interceptor of logging requests & responses by debug.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ # axios-debug-log | ||
Axios interceptor of logging requests & responses by [debug](https://www.npmjs.com/package/debug). | ||
[Axios](https://www.npmjs.com/package/axios) interceptor of logging requests & responses by [debug](https://www.npmjs.com/package/debug). | ||
@@ -21,3 +21,3 @@ ![Screenshot](screenshot.png "Screenshot") | ||
1. Install: add `require('axios-debug-log')` before any axios execution. | ||
2. Enable: set `DEBUG` environment variable to `axios` before start your fantastic Node.js application. | ||
2. Enable: set `DEBUG=axios` environment variables before start your fantastic Node.js application. | ||
@@ -31,4 +31,25 @@ ## Browser usage | ||
## Configuration | ||
```javascript | ||
// Log content type | ||
require('axios-debug-log')({ | ||
request: function (debug, config) { | ||
debug('Request with ' + config.headers['content-type']) | ||
}, | ||
response: function (debug, response) { | ||
debug( | ||
'Response with ' + response.headers['content-type'], | ||
'from ' + response.config.url | ||
) | ||
}, | ||
error: function (debug, error) { | ||
// Read https://www.npmjs.com/package/axios#handling-errors for more info | ||
debug('Boom', error) | ||
} | ||
}) | ||
``` | ||
## License | ||
MIT |
53
test.js
@@ -35,2 +35,30 @@ /* eslint-env mocha */ | ||
it('should log request error', () => axios({ | ||
method: 'FOO', | ||
url: 'http://example.com/', | ||
adapter: config => Promise.reject(Object.assign(TypeError('Boom'), { config })) | ||
}).catch(() => { | ||
debug.log.should.be.calledTwice() | ||
debug.log.firstCall.should.be.calledWithExactly( | ||
'FOO http://example.com/' | ||
) | ||
debug.log.secondCall.should.be.calledWithExactly( | ||
'TypeError: Boom', '(FOO http://example.com/)' | ||
) | ||
})) | ||
it('should log general error', () => axios({ | ||
method: 'FOO', | ||
url: 'http://example.com/', | ||
transformRequest: () => { throw ReferenceError('Boom') } | ||
}).catch(() => { | ||
debug.log.should.be.calledTwice() | ||
debug.log.firstCall.should.be.calledWithExactly( | ||
'FOO http://example.com/' | ||
) | ||
debug.log.secondCall.should.be.calledWithExactly( | ||
'ReferenceError: Boom' | ||
) | ||
})) | ||
it('should logging request of axios instance', () => axios.create()({ | ||
@@ -53,1 +81,26 @@ method: 'BAZ', | ||
})) | ||
it('should be able to set format of response & response logging', () => { | ||
const requestLogger = sinon.spy((debug, config) => debug(config.method)) | ||
const responseLogger = sinon.spy((debug, response) => debug(response.statusText)) | ||
require('.')({ | ||
request: requestLogger, | ||
response: responseLogger | ||
}) | ||
return axios({ | ||
method: 'FOO', | ||
url: 'http://example.com/', | ||
adapter: config => Promise.resolve({ | ||
status: 200, | ||
statusText: 'BAR', | ||
config | ||
}) | ||
}).then(response => { | ||
const debugLog = sinon.match({ namespace: 'axios' }) | ||
requestLogger.should.be.calledWith(debugLog, response.config) | ||
responseLogger.should.be.calledWith(debugLog, response) | ||
debug.log.should.be.calledTwice() | ||
debug.log.firstCall.should.be.calledWith('FOO') | ||
debug.log.secondCall.should.be.calledWith('BAR') | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38829
148
53