Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: debug option #109

Merged
merged 1 commit into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -31,23 +31,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 @@ -54,7 +54,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 @@ -104,6 +104,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 @@ -136,8 +191,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