-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
71 lines (62 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* eslint-disable no-var */
'use strict'
var axios = require('axios')
var axiosDebug = require('debug')('axios')
var URL_KEY = '__AXIOS-DEBUG-LOG_URL__'
var options = {
request: function (debug, config) {
var url = axios.getUri(config)
Object.defineProperty(config, URL_KEY, { value: url })
debug(
config.method.toUpperCase() + ' ' + url
)
},
response: function (debug, response) {
var url = response.config[URL_KEY]
debug(
response.status + ' ' + response.statusText,
'(' + response.config.method.toUpperCase() + ' ' + url + ')'
)
},
error: function (debug, error) {
if (error.config) {
var url = error.config[URL_KEY]
debug(
error.name + ': ' + error.message,
'(' + error.config.method.toUpperCase() + ' ' + url + ')'
)
} else {
debug(error.name + ': ' + error.message)
}
}
}
function addLogger (instance, debug) {
if (debug === undefined) debug = axiosDebug
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
})
}
addLogger(axios)
axios.create = (function (originalCreate) {
return function create () {
var instance = originalCreate.apply(this, arguments)
addLogger(instance)
return instance
}
})(axios.create)
exports = module.exports = function (userOptions) {
for (var key in options) {
if (key in userOptions) {
options[key] = userOptions[key]
}
}
}
exports.addLogger = addLogger