http/lib/plugin.js

32 lines
842 B
JavaScript
Raw Normal View History

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
const baseURL = process.browser
? '<%= options.browserBaseURL %>'
2019-03-18 18:09:39 +00:00
: (process.env._HTTP_BASE_URL_ || '<%= options.baseURL %>')
2019-03-18 18:09:39 +00:00
const kyDefaults = {
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
<% } %><% } %>
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'
}
2019-03-18 18:09:39 +00:00
// Extend ky with defaults
const ky = KY.extend(kyDefaults)
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
}