axios-debug-log
Advanced tools
Comparing version 0.1.0 to 0.2.0
23
index.js
@@ -6,8 +6,8 @@ 'use strict' | ||
axios.interceptors.request.use(function (config) { | ||
function logRequest (config) { | ||
debug(config.method.toUpperCase() + ' ' + config.url) | ||
return config | ||
}) | ||
} | ||
axios.interceptors.response.use(function (response) { | ||
function logResponse (response) { | ||
debug( | ||
@@ -18,2 +18,17 @@ response.status + ' ' + response.statusText, | ||
return response | ||
}) | ||
} | ||
function addLogger (instance) { | ||
instance.interceptors.request.use(logRequest) | ||
instance.interceptors.response.use(logResponse) | ||
} | ||
addLogger(axios) | ||
axios.create = (function (originalCreate) { | ||
return function create () { | ||
var instance = originalCreate.apply(axios, arguments) | ||
addLogger(instance) | ||
return instance | ||
} | ||
})(axios.create) |
{ | ||
"name": "axios-debug-log", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Axios interceptor of logging requests & responses by debug.", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"unit": "ava", | ||
"coverage": "nyc ava && nyc report --reporter=html", | ||
"test": "npm run lint && npm run unit" | ||
"lint": "standard", | ||
"unit": "mocha --require should --require should-sinon", | ||
"test": "npm run lint && npm run unit", | ||
"coverage": "nyc npm test && nyc report --reporter=html" | ||
}, | ||
@@ -31,11 +31,9 @@ "repository": { | ||
"devDependencies": { | ||
"ava": "^0.17.0", | ||
"axios": "^0.15.0", | ||
"eslint": "^3.14.1", | ||
"eslint-config-standard": "^6.2.1", | ||
"eslint-plugin-promise": "^3.4.0", | ||
"eslint-plugin-standard": "^2.0.1", | ||
"mocha": "^3.2.0", | ||
"nyc": "^10.1.2", | ||
"proxyquire": "^1.7.11", | ||
"sinon": "^1.17.7" | ||
"should": "^11.2.0", | ||
"should-sinon": "0.0.5", | ||
"sinon": "^1.17.7", | ||
"standard": "^8.6.0" | ||
}, | ||
@@ -42,0 +40,0 @@ "peerDependencies": { |
49
test.js
@@ -1,12 +0,18 @@ | ||
import test from 'ava' | ||
/* eslint-env mocha */ | ||
const proxyquire = require('proxyquire') | ||
const axios = require('axios') | ||
const debug = require('debug') | ||
const sinon = require('sinon') | ||
const spy = sinon.spy() | ||
proxyquire('.', { debug: name => ({ axios: spy }[name]) }) | ||
before(() => { | ||
debug.enable('axios') | ||
debug.formatArgs = args => args | ||
require('.') | ||
}) | ||
const axios = require('axios') | ||
beforeEach(() => { | ||
debug.log = sinon.spy() | ||
}) | ||
test('Logging request', t => axios({ | ||
it('should logging request', () => axios({ | ||
method: 'FOO', | ||
@@ -20,8 +26,27 @@ url: 'http://example.com/', | ||
}).then(() => { | ||
t.is(spy.callCount, 2) | ||
const requestLogging = spy.firstCall | ||
t.is(requestLogging.args[0], 'FOO http://example.com/') | ||
const responseLogging = spy.secondCall | ||
t.is(responseLogging.args[0], '200 BAR') | ||
t.is(responseLogging.args[1], '(FOO http://example.com/)') | ||
debug.log.should.be.calledTwice() | ||
debug.log.firstCall.should.be.calledWithExactly( | ||
'FOO http://example.com/' | ||
) | ||
debug.log.secondCall.should.be.calledWithExactly( | ||
'200 BAR', '(FOO http://example.com/)' | ||
) | ||
})) | ||
it('should logging request of axios instance', () => axios.create()({ | ||
method: 'BAZ', | ||
url: 'http://example.com/', | ||
adapter: config => Promise.resolve({ | ||
status: 200, | ||
statusText: 'QUX', | ||
config | ||
}) | ||
}).then(() => { | ||
debug.log.should.be.calledTwice() | ||
debug.log.firstCall.should.be.calledWithExactly( | ||
'BAZ http://example.com/' | ||
) | ||
debug.log.secondCall.should.be.calledWithExactly( | ||
'200 QUX', '(BAZ http://example.com/)' | ||
) | ||
})) |
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
36010
7
72
9