2020-04-13 13:23:46 +00:00
|
|
|
import defu from 'defu'
|
2020-06-17 11:01:35 +00:00
|
|
|
import destr from 'destr'
|
2017-08-13 11:22:10 +00:00
|
|
|
|
2020-06-12 15:43:59 +00:00
|
|
|
const KY = process.server ? require('ky-universal/node') : require('ky-universal/browser').default
|
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
class HTTP {
|
2019-05-29 22:17:12 +00:00
|
|
|
constructor(defaults, ky = KY) {
|
2019-03-18 20:38:46 +00:00
|
|
|
this._defaults = {
|
|
|
|
hooks: {},
|
|
|
|
...defaults
|
|
|
|
}
|
2019-05-21 21:20:04 +00:00
|
|
|
|
2019-05-22 20:40:38 +00:00
|
|
|
this._ky = ky
|
2019-03-18 20:38:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 13:23:46 +00:00
|
|
|
setBaseURL (baseURL) {
|
|
|
|
this._defaults.prefixUrl = baseURL
|
|
|
|
}
|
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
setHeader(name, value) {
|
|
|
|
if (!value) {
|
|
|
|
delete this._defaults.headers[name];
|
|
|
|
} else {
|
|
|
|
this._defaults.headers[name] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setToken(token, type) {
|
|
|
|
const value = !token ? null : (type ? type + ' ' : '') + token
|
2020-05-26 17:38:55 +00:00
|
|
|
this.setHeader('Authorization', value)
|
2019-03-18 20:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_hook(name, fn) {
|
|
|
|
if (!this._defaults.hooks[name]) {
|
|
|
|
this._defaults.hooks[name] = []
|
|
|
|
}
|
|
|
|
this._defaults.hooks[name].push(fn)
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:01:00 +00:00
|
|
|
onRequest(fn) {
|
2019-03-18 20:38:46 +00:00
|
|
|
this._hook('beforeRequest', fn)
|
|
|
|
}
|
|
|
|
|
2019-11-02 11:02:50 +00:00
|
|
|
onRetry(fn) {
|
|
|
|
this._hook('beforeRetry', fn)
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:01:00 +00:00
|
|
|
onResponse(fn) {
|
2019-03-18 20:38:46 +00:00
|
|
|
this._hook('afterResponse', fn)
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:01:00 +00:00
|
|
|
onError(fn) {
|
2019-03-18 20:38:46 +00:00
|
|
|
this._hook('onError', fn)
|
2019-03-18 19:01:00 +00:00
|
|
|
}
|
2020-04-13 13:23:46 +00:00
|
|
|
|
|
|
|
create(options) {
|
|
|
|
const { retry, timeout, prefixUrl, headers } = this._defaults
|
|
|
|
|
2020-06-12 15:46:48 +00:00
|
|
|
return createHttpInstance(defu(options, { retry, timeout, prefixUrl, headers }))
|
2020-04-13 13:23:46 +00:00
|
|
|
}
|
2019-03-18 19:01:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 20:33:35 +00:00
|
|
|
for (let method of ['get', 'head', 'delete', 'post', 'put', 'patch']) {
|
|
|
|
const hasBody = ['post', 'put', 'patch'].includes(method)
|
|
|
|
|
|
|
|
HTTP.prototype[method] = async function (url, arg1, arg2) {
|
|
|
|
let options
|
|
|
|
|
|
|
|
if (!hasBody) {
|
|
|
|
options = arg1
|
|
|
|
} else {
|
|
|
|
options = arg2 || {}
|
|
|
|
if (arg1 !== undefined) {
|
2020-01-30 22:01:54 +00:00
|
|
|
if (arg1.constructor === Object || Array.isArray(arg1)) {
|
2019-04-09 20:33:35 +00:00
|
|
|
options.json = arg1
|
|
|
|
} else {
|
|
|
|
options.body = arg1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
const _options = { ...this._defaults, ...options }
|
|
|
|
|
2019-04-09 20:33:35 +00:00
|
|
|
if (/^https?/.test(url)) {
|
2019-03-18 20:38:46 +00:00
|
|
|
delete _options.prefixUrl
|
2020-04-29 17:54:55 +00:00
|
|
|
} else if (_options.prefixUrl && typeof url === 'string' && url.startsWith('/')) {
|
|
|
|
// Prevents `ky` from throwing "`input` must not begin with a slash when using `prefixUrl`"
|
|
|
|
url = url.substr(1)
|
2020-05-26 17:38:55 +00:00
|
|
|
}
|
2019-03-18 19:01:00 +00:00
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
try {
|
2019-04-09 20:33:35 +00:00
|
|
|
const response = await this._ky[method](url, _options)
|
2019-03-18 20:38:46 +00:00
|
|
|
return response
|
|
|
|
} catch (error) {
|
2020-06-17 11:01:35 +00:00
|
|
|
// Try to fill error with useful data
|
|
|
|
if (error.response) {
|
|
|
|
error.statusCode = error.response.status
|
|
|
|
try {
|
2020-06-29 10:43:15 +00:00
|
|
|
const text = await error.response.text()
|
|
|
|
error.response.text = () => Promise.resolve(text)
|
|
|
|
const json = destr(text)
|
|
|
|
error.response.json = () => Promise.resolve(json)
|
|
|
|
error.response.data = json
|
2020-06-17 11:01:35 +00:00
|
|
|
} catch (_) { }
|
|
|
|
}
|
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
// Call onError hook
|
|
|
|
if (_options.hooks.onError) {
|
2020-06-17 11:01:35 +00:00
|
|
|
for (const fn of _options.hooks.onError) {
|
|
|
|
const res = fn(error)
|
|
|
|
if (res !== undefined) {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
2019-03-18 20:38:46 +00:00
|
|
|
}
|
2020-06-17 11:01:35 +00:00
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
// Throw error
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 20:33:35 +00:00
|
|
|
HTTP.prototype['$' + method] = function (url, arg1, arg2) {
|
2020-06-17 11:01:35 +00:00
|
|
|
return this[method](url, arg1, arg2)
|
|
|
|
.then(response => (response && response.text) ? response.text() : response)
|
|
|
|
.then(body => destr(body))
|
2019-03-18 19:01:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:46:48 +00:00
|
|
|
const createHttpInstance = options => {
|
|
|
|
// Create new HTTP instance
|
|
|
|
const http = new HTTP(options)
|
|
|
|
|
|
|
|
// Setup interceptors
|
2020-07-16 17:17:51 +00:00
|
|
|
<% if (options.debug) { %>setupDebugInterceptor(http) <% } %>
|
2020-06-12 15:46:48 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}<% } %>
|
|
|
|
|
2018-01-28 15:47:47 +00:00
|
|
|
export default (ctx, inject) => {
|
2020-07-14 10:32:28 +00:00
|
|
|
// runtimeConfig
|
|
|
|
const runtimeConfig = ctx.$config && ctx.$config.http || {}
|
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
// prefixUrl
|
2019-03-18 19:01:55 +00:00
|
|
|
const prefixUrl = process.browser
|
2020-07-14 10:32:28 +00:00
|
|
|
? (runtimeConfig.browserBaseURL || '<%= options.browserBaseURL || '' %>')
|
|
|
|
: (runtimeConfig.baseURL || process.env._HTTP_BASE_URL_ || '<%= options.baseURL || '' %>')
|
2018-11-07 13:40:54 +00:00
|
|
|
|
2020-05-26 17:38:55 +00:00
|
|
|
const headers = <%= JSON.stringify(options.headers, null, 2) %>
|
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
// Defaults
|
2019-03-24 11:12:58 +00:00
|
|
|
const defaults = {
|
2020-01-30 22:03:27 +00:00
|
|
|
retry: <%= options.retry %>,
|
2019-05-30 08:31:27 +00:00
|
|
|
timeout: process.server ? <%= options.serverTimeout %> : <%= options.clientTimeout %>,
|
2019-04-18 16:04:44 +00:00
|
|
|
prefixUrl,
|
2020-05-26 17:38:55 +00:00
|
|
|
headers
|
2019-03-24 11:12:58 +00:00
|
|
|
}
|
2017-08-13 11:22:10 +00:00
|
|
|
|
2018-01-28 13:57:39 +00:00
|
|
|
<% if (options.proxyHeaders) { %>
|
|
|
|
// Proxy SSR request headers headers
|
2020-05-26 17:38:55 +00:00
|
|
|
if (process.server && ctx.req && ctx.req.headers) {
|
|
|
|
const reqHeaders = { ...ctx.req.headers }
|
|
|
|
for (let h of <%= serialize(options.proxyHeadersIgnore) %>) {
|
|
|
|
delete reqHeaders[h]
|
|
|
|
}
|
|
|
|
defaults.headers = { ...reqHeaders, ...defaults.headers }
|
|
|
|
}
|
|
|
|
<% } %>
|
2018-01-28 13:57:39 +00:00
|
|
|
|
2018-11-08 09:17:46 +00:00
|
|
|
if (process.server) {
|
|
|
|
// Don't accept brotli encoding because Node can't parse it
|
2019-05-21 18:06:47 +00:00
|
|
|
defaults.headers['accept-encoding'] = 'gzip, deflate'
|
2018-11-08 09:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 15:46:48 +00:00
|
|
|
const http = createHttpInstance(defaults)
|
2019-03-18 19:01:00 +00:00
|
|
|
|
2019-03-18 18:09:39 +00:00
|
|
|
// Inject http to the context as $http
|
2019-03-18 20:38:46 +00:00
|
|
|
ctx.$http = http
|
|
|
|
inject('http', http)
|
2017-08-13 11:22:10 +00:00
|
|
|
}
|