2019-03-18 18:09:39 +00:00
|
|
|
import KY from 'ky-universal'
|
2017-08-13 11:22:10 +00:00
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
class HTTP {
|
|
|
|
constructor(defaults, ky = KY) {
|
|
|
|
this._defaults = {
|
|
|
|
hooks: {},
|
|
|
|
headers: {},
|
|
|
|
retry: 0,
|
|
|
|
...defaults
|
|
|
|
}
|
|
|
|
this._ky = ky
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
this.setHeader('Authorization', value)
|
|
|
|
}
|
|
|
|
|
|
|
|
_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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let method of ['get', 'post', 'put', 'patch', 'head', 'delete']) {
|
2019-03-18 20:38:46 +00:00
|
|
|
HTTP.prototype[method] = async function (input, options) {
|
|
|
|
const _options = { ...this._defaults, ...options }
|
|
|
|
|
|
|
|
if (/^https?/.test(input)) {
|
|
|
|
delete _options.prefixUrl
|
|
|
|
}
|
2019-03-18 19:01:00 +00:00
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
try {
|
|
|
|
const response = await this._ky[method](input, _options)
|
|
|
|
return response
|
|
|
|
} catch (error) {
|
|
|
|
// Call onError hook
|
|
|
|
if (_options.hooks.onError) {
|
|
|
|
_options.hooks.onError.forEach(fn => fn(error))
|
|
|
|
}
|
|
|
|
// Throw error
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HTTP.prototype['$' + method] = function (input, options) {
|
|
|
|
return this[method](input, options).then(res => res.json())
|
2019-03-18 19:01:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 15:47:47 +00:00
|
|
|
export default (ctx, inject) => {
|
2019-03-18 20:38:46 +00:00
|
|
|
// prefixUrl
|
2019-03-18 19:01:55 +00:00
|
|
|
const prefixUrl = process.browser
|
2018-02-08 21:34:47 +00:00
|
|
|
? '<%= options.browserBaseURL %>'
|
2019-03-18 18:09:39 +00:00
|
|
|
: (process.env._HTTP_BASE_URL_ || '<%= options.baseURL %>')
|
2018-11-07 13:40:54 +00:00
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
// Defaults
|
|
|
|
const defaults = { prefixUrl }
|
2017-08-13 11:22:10 +00:00
|
|
|
|
2018-01-28 13:57:39 +00:00
|
|
|
<% if (options.proxyHeaders) { %>
|
|
|
|
// Proxy SSR request headers headers
|
2019-03-18 20:38:46 +00:00
|
|
|
defaults.headers = (ctx.req && ctx.req.headers) ? { ...ctx.req.headers } : {}
|
2019-03-18 20:40:45 +00:00
|
|
|
<% for (let h of options.proxyHeadersIgnore) { %>delete defaults.headers['<%= h %>']
|
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-03-18 20:38:46 +00:00
|
|
|
defaults.headers['Accept-Encoding'] = 'gzip, deflate'
|
2018-11-08 09:17:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
// Create new HTTP instance
|
|
|
|
const http = new HTTP(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
|
|
|
}
|