Skip to content

Commit

Permalink
feat: debug option (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored Jun 12, 2020
1 parent 11d0b81 commit 874b668
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/api/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://`.

## `debug`

* Default: `false`

Adds interceptors that logs http request and responses.

## `proxy`

* Default: `false`
Expand Down
7 changes: 4 additions & 3 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,24 @@ function httpModule (_moduleOptions) {
defaultHost = 'localhost'
}

// Default prefix
const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/'

// Support baseUrl
if (moduleOptions.baseUrl && !moduleOptions.baseURL) {
moduleOptions.baseURL = moduleOptions.baseUrl

delete moduleOptions.baseUrl
}

// Default prefix
const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/'

// HTTPS
const https = Boolean(this.options.server && this.options.server.https)

// Apply defaults
const options = {
baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
browserBaseURL: undefined,
debug: false,
proxyHeaders: true,
proxyHeadersIgnore: ['accept', 'host', 'cf-ray', 'cf-connecting-ip', 'content-length', 'content-md5', 'content-type'],
proxy: false,
Expand Down
60 changes: 57 additions & 3 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class HTTP {
create(options) {
const { retry, timeout, prefixUrl, headers } = this._defaults

return new HTTP(defu(options, { retry, timeout, prefixUrl, headers }))
return createHttpInstance(defu(options, { retry, timeout, prefixUrl, headers }))
}
}

Expand Down Expand Up @@ -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) => {
// prefixUrl
const prefixUrl = process.browser
Expand Down Expand Up @@ -137,8 +192,7 @@ export default (ctx, inject) => {
defaults.headers['accept-encoding'] = 'gzip, deflate'
}

// Create new HTTP instance
const http = new HTTP(defaults)
const http = createHttpInstance(defaults)

// Inject http to the context as $http
ctx.$http = http
Expand Down

0 comments on commit 874b668

Please sign in to comment.