2019-03-18 18:09:39 +00:00
|
|
|
import KY from 'ky-universal'
|
2017-08-13 11:22:10 +00:00
|
|
|
|
2018-01-28 15:47:47 +00:00
|
|
|
export default (ctx, inject) => {
|
2019-03-18 18:09:39 +00:00
|
|
|
// Set baseURL
|
2018-11-07 13:40:54 +00:00
|
|
|
const baseURL = 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 18:09:39 +00:00
|
|
|
const kyDefaults = {
|
2018-11-07 13:40:54 +00:00
|
|
|
baseURL,
|
|
|
|
headers
|
2018-01-28 13:57:39 +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
|
2019-03-18 18:09:39 +00:00
|
|
|
kyDefaults.headers = (ctx.req && ctx.req.headers) ? { ...ctx.req.headers } : {}
|
|
|
|
<% for (let h of options.proxyHeadersIgnore) { %> delete kyDefaults.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 18:09:39 +00:00
|
|
|
kyDefaults.headers['Accept-Encoding'] = 'gzip, deflate'
|
2018-11-08 09:17:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 18:09:39 +00:00
|
|
|
// Extend ky with defaults
|
|
|
|
const ky = KY.extend(kyDefaults)
|
2017-08-30 17:50:43 +00:00
|
|
|
|
2019-03-18 18:09:39 +00:00
|
|
|
// Inject http to the context as $http
|
|
|
|
ctx.$http = ky
|
|
|
|
inject('http', http)
|
2017-08-13 11:22:10 +00:00
|
|
|
}
|