feat: `debug` option (#109)

master
Ricardo Gobbo de Souza 2020-06-12 12:46:48 -03:00 committed by GitHub
parent 11d0b81657
commit 874b668e1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 6 deletions

View File

@ -46,6 +46,12 @@ Environment variable `API_URL_BROWSER` can be used to **override** `browserBaseU
If set to `true`, `http://` in both `baseURL` and `browserBaseURL` will be changed into `https://`. If set to `true`, `http://` in both `baseURL` and `browserBaseURL` will be changed into `https://`.
## `debug`
* Default: `false`
Adds interceptors that logs http request and responses.
## `proxy` ## `proxy`
* Default: `false` * Default: `false`

View File

@ -30,9 +30,6 @@ function httpModule (_moduleOptions) {
defaultHost = 'localhost' defaultHost = 'localhost'
} }
// Default prefix
const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/'
// Support baseUrl // Support baseUrl
if (moduleOptions.baseUrl && !moduleOptions.baseURL) { if (moduleOptions.baseUrl && !moduleOptions.baseURL) {
moduleOptions.baseURL = moduleOptions.baseUrl moduleOptions.baseURL = moduleOptions.baseUrl
@ -40,6 +37,9 @@ function httpModule (_moduleOptions) {
delete moduleOptions.baseUrl delete moduleOptions.baseUrl
} }
// Default prefix
const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/'
// HTTPS // HTTPS
const https = Boolean(this.options.server && this.options.server.https) const https = Boolean(this.options.server && this.options.server.https)
@ -47,6 +47,7 @@ function httpModule (_moduleOptions) {
const options = { const options = {
baseURL: `http://${defaultHost}:${defaultPort}${prefix}`, baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
browserBaseURL: undefined, browserBaseURL: undefined,
debug: false,
proxyHeaders: true, proxyHeaders: true,
proxyHeadersIgnore: ['accept', 'host', 'cf-ray', 'cf-connecting-ip', 'content-length', 'content-md5', 'content-type'], proxyHeadersIgnore: ['accept', 'host', 'cf-ray', 'cf-connecting-ip', 'content-length', 'content-md5', 'content-type'],
proxy: false, proxy: false,

View File

@ -55,7 +55,7 @@ class HTTP {
create(options) { create(options) {
const { retry, timeout, prefixUrl, headers } = this._defaults const { retry, timeout, prefixUrl, headers } = this._defaults
return new HTTP(defu(options, { retry, timeout, prefixUrl, headers })) return createHttpInstance(defu(options, { retry, timeout, prefixUrl, headers }))
} }
} }
@ -105,6 +105,61 @@ for (let method of ['get', 'head', 'delete', 'post', 'put', 'patch']) {
} }
} }
const createHttpInstance = options => {
// Create new HTTP instance
const http = new HTTP(options)
// Setup interceptors
<% if (options.debug) { %>setupDebugInterceptor(axios) <% } %>
return http
}
<% if (options.debug) { %>
const log = (level, ...messages) => console[level]('[Http]', ...messages)
const setupDebugInterceptor = http => {
// request
http.onRequest(req => {
log(
'info',
'Request:',
'[' + req.method.toUpperCase() + ']',
req.url
)
if (process.browser) {
console.log(req)
} else {
console.log(JSON.stringify(req, undefined, 2))
}
})
// response
http.onResponse(req, options, res => {
log(
'info',
'Response:',
'[' + (res.status + ' ' + res.statusText) + ']',
'[' + req.method.toUpperCase() + ']',
res.url,
)
if (process.browser) {
console.log(req, options, res)
} else {
console.log(JSON.stringify({ req, options, res }, undefined, 2))
}
return res
})
// error
http.onError(error => {
log('error', 'Error:', error)
})
}<% } %>
export default (ctx, inject) => { export default (ctx, inject) => {
// prefixUrl // prefixUrl
const prefixUrl = process.browser const prefixUrl = process.browser
@ -137,8 +192,7 @@ export default (ctx, inject) => {
defaults.headers['accept-encoding'] = 'gzip, deflate' defaults.headers['accept-encoding'] = 'gzip, deflate'
} }
// Create new HTTP instance const http = createHttpInstance(defaults)
const http = new HTTP(defaults)
// Inject http to the context as $http // Inject http to the context as $http
ctx.$http = http ctx.$http = http