2017-08-13 11:22:10 +00:00
|
|
|
const path = require('path')
|
2018-03-31 17:52:54 +00:00
|
|
|
const consola = require('consola')
|
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
const logger = consola.withScope('nuxt:http')
|
2017-08-13 11:22:10 +00:00
|
|
|
|
2019-09-05 16:34:41 +00:00
|
|
|
function httpModule (_moduleOptions) {
|
2020-07-14 10:32:28 +00:00
|
|
|
const { nuxt, addPlugin, requireModule } = this
|
|
|
|
|
2018-01-16 17:15:12 +00:00
|
|
|
// Combine options
|
2020-07-14 10:32:28 +00:00
|
|
|
const moduleOptions = { ...nuxt.options.http, ..._moduleOptions }
|
2018-01-16 17:15:12 +00:00
|
|
|
|
|
|
|
// Default port
|
|
|
|
const defaultPort =
|
|
|
|
process.env.API_PORT ||
|
|
|
|
moduleOptions.port ||
|
|
|
|
process.env.PORT ||
|
|
|
|
process.env.npm_package_config_nuxt_port ||
|
2020-07-14 10:32:28 +00:00
|
|
|
(nuxt.options.server && nuxt.options.server.port) ||
|
2018-01-16 17:15:12 +00:00
|
|
|
3000
|
|
|
|
|
|
|
|
// Default host
|
|
|
|
let defaultHost =
|
|
|
|
process.env.API_HOST ||
|
|
|
|
moduleOptions.host ||
|
|
|
|
process.env.HOST ||
|
|
|
|
process.env.npm_package_config_nuxt_host ||
|
2020-07-14 10:32:28 +00:00
|
|
|
(nuxt.options.server && nuxt.options.server.host) ||
|
2018-01-16 17:15:12 +00:00
|
|
|
'localhost'
|
|
|
|
|
2017-08-13 13:47:38 +00:00
|
|
|
/* istanbul ignore if */
|
2018-01-16 17:15:12 +00:00
|
|
|
if (defaultHost === '0.0.0.0') {
|
|
|
|
defaultHost = 'localhost'
|
2017-08-13 13:47:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 20:36:17 +00:00
|
|
|
// Support baseUrl
|
|
|
|
if (moduleOptions.baseUrl && !moduleOptions.baseURL) {
|
|
|
|
moduleOptions.baseURL = moduleOptions.baseUrl
|
|
|
|
|
|
|
|
delete moduleOptions.baseUrl
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:46:48 +00:00
|
|
|
// Default prefix
|
|
|
|
const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/'
|
|
|
|
|
2020-06-06 18:03:18 +00:00
|
|
|
// HTTPS
|
2020-07-14 10:32:28 +00:00
|
|
|
const https = Boolean(nuxt.options.server && nuxt.options.server.https)
|
2020-06-06 18:03:18 +00:00
|
|
|
|
2017-08-13 11:22:10 +00:00
|
|
|
// Apply defaults
|
2019-02-19 08:43:56 +00:00
|
|
|
const options = {
|
2020-07-14 10:32:28 +00:00
|
|
|
baseURL: undefined,
|
2020-06-03 20:36:17 +00:00
|
|
|
browserBaseURL: undefined,
|
2020-06-12 15:46:48 +00:00
|
|
|
debug: false,
|
2019-02-19 08:43:56 +00:00
|
|
|
proxyHeaders: true,
|
2020-06-06 18:03:37 +00:00
|
|
|
proxyHeadersIgnore: ['accept', 'host', 'cf-ray', 'cf-connecting-ip', 'content-length', 'content-md5', 'content-type'],
|
2019-02-19 08:43:56 +00:00
|
|
|
proxy: false,
|
|
|
|
retry: false,
|
2019-05-29 22:14:54 +00:00
|
|
|
serverTimeout: false,
|
|
|
|
clientTimeout: false,
|
2020-06-06 18:03:18 +00:00
|
|
|
https,
|
2020-05-26 17:38:55 +00:00
|
|
|
headers: {},
|
2019-02-19 08:43:56 +00:00
|
|
|
...moduleOptions
|
|
|
|
}
|
2020-07-14 10:32:28 +00:00
|
|
|
const toHttps = s => options.https ? s.replace('http://', 'https://') : s
|
2017-08-13 11:22:10 +00:00
|
|
|
|
2018-01-16 17:15:12 +00:00
|
|
|
// ENV overrides
|
2017-08-13 13:47:38 +00:00
|
|
|
/* istanbul ignore if */
|
2017-08-13 11:22:10 +00:00
|
|
|
if (process.env.API_URL) {
|
|
|
|
options.baseURL = process.env.API_URL
|
|
|
|
}
|
|
|
|
|
2017-08-13 13:47:38 +00:00
|
|
|
/* istanbul ignore if */
|
2017-08-13 11:22:10 +00:00
|
|
|
if (process.env.API_URL_BROWSER) {
|
|
|
|
options.browserBaseURL = process.env.API_URL_BROWSER
|
|
|
|
}
|
|
|
|
|
2020-07-14 10:32:28 +00:00
|
|
|
// If no baseURL defined, get it from Nuxt server
|
|
|
|
if (!options.baseURL) {
|
|
|
|
options.baseURL = `http://${defaultHost}:${defaultPort}${prefix}`
|
|
|
|
|
|
|
|
// Update auto generated baseURL after listen for static target as we use random port
|
|
|
|
const publicRuntimeConfig = nuxt.options.publicRuntimeConfig = nuxt.options.publicRuntimeConfig || {}
|
|
|
|
publicRuntimeConfig.http = publicRuntimeConfig.http || {}
|
|
|
|
|
|
|
|
const privateRuntimeConfig = nuxt.options.privateRuntimeConfig = nuxt.options.privateRuntimeConfig || {}
|
|
|
|
privateRuntimeConfig.http = privateRuntimeConfig.http || {}
|
|
|
|
|
|
|
|
// For static exporting
|
|
|
|
if (nuxt.options.target === 'static') {
|
2020-07-14 11:01:11 +00:00
|
|
|
nuxt.hook('listen', (_, { host = 'localhost', port }) => {
|
2020-07-14 10:32:28 +00:00
|
|
|
publicRuntimeConfig.http.browserBaseURL = toHttps(publicRuntimeConfig.http.browserBaseURL || prefix || '/')
|
|
|
|
privateRuntimeConfig.http.baseURL = toHttps(privateRuntimeConfig.http.baseURL || `http://${host}:${port}${prefix}`)
|
|
|
|
})
|
|
|
|
}
|
2017-08-13 11:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 18:28:25 +00:00
|
|
|
// Normalize options
|
|
|
|
if (options.retry === true) {
|
2019-03-24 11:12:58 +00:00
|
|
|
options.retry = 2
|
|
|
|
} else if (!options.retry) {
|
|
|
|
options.retry = 0
|
2020-01-30 22:03:27 +00:00
|
|
|
} else if (!isNaN(options.retry)) {
|
|
|
|
options.retry = parseInt(options.retry)
|
|
|
|
} else if (typeof options.retry === 'object') {
|
|
|
|
options.retry = JSON.stringify(options.retry)
|
2018-02-04 18:28:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 10:32:28 +00:00
|
|
|
// Default browserBaseURL
|
|
|
|
if (typeof options.browserBaseURL === 'undefined') {
|
|
|
|
options.browserBaseURL = options.proxy ? prefix : options.baseURL
|
|
|
|
}
|
|
|
|
|
2020-06-03 20:36:17 +00:00
|
|
|
// Remove port 443 when https
|
|
|
|
if (options.baseURL.includes('https://')) {
|
|
|
|
options.baseURL = options.baseURL.replace(':443', '')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove port 80 when http
|
2020-09-26 10:51:46 +00:00
|
|
|
if (/^http:\/\/.*:80(\/|$)/.test(options.baseURL)) {
|
2020-06-03 20:36:17 +00:00
|
|
|
options.baseURL = options.baseURL.replace(':80', '')
|
|
|
|
}
|
|
|
|
|
2018-02-04 19:37:58 +00:00
|
|
|
// Convert http:// to https:// if https option is on
|
2020-07-14 10:32:28 +00:00
|
|
|
options.baseURL = toHttps(options.baseURL)
|
|
|
|
options.browserBaseURL = toHttps(options.browserBaseURL)
|
2018-02-04 19:37:58 +00:00
|
|
|
|
2017-08-13 11:22:10 +00:00
|
|
|
// Register plugin
|
2020-07-14 10:32:28 +00:00
|
|
|
addPlugin({
|
2019-02-09 14:04:47 +00:00
|
|
|
src: path.resolve(__dirname, 'plugin.js'),
|
2019-03-18 20:38:46 +00:00
|
|
|
fileName: 'http.js',
|
2017-08-13 11:22:10 +00:00
|
|
|
options
|
|
|
|
})
|
|
|
|
|
2018-01-28 19:10:23 +00:00
|
|
|
// Proxy integration
|
|
|
|
if (options.proxy) {
|
2020-07-14 10:32:28 +00:00
|
|
|
requireModule([
|
2018-02-04 18:02:30 +00:00
|
|
|
'@nuxtjs/proxy',
|
|
|
|
typeof options.proxy === 'object' ? options.proxy : {}
|
|
|
|
])
|
2018-01-28 19:10:23 +00:00
|
|
|
}
|
2020-06-06 18:03:18 +00:00
|
|
|
|
2020-06-12 15:43:59 +00:00
|
|
|
// Alias ky-universal
|
2020-07-14 10:32:28 +00:00
|
|
|
nuxt.options.alias['ky-universal'] = path.resolve(__dirname, '../ky-universal')
|
2020-06-12 15:43:59 +00:00
|
|
|
|
|
|
|
// Transpile ky and ky-universal
|
2020-07-14 10:34:24 +00:00
|
|
|
nuxt.options.build.transpile = nuxt.options.build.transpile || []
|
2020-07-14 10:32:28 +00:00
|
|
|
nuxt.options.build.transpile.push('ky')
|
|
|
|
nuxt.options.build.transpile.push('@nuxt/http')
|
2018-01-28 19:10:23 +00:00
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
// Set _HTTP_BASE_URL_ for dynamic SSR baseURL
|
|
|
|
process.env._HTTP_BASE_URL_ = options.baseURL
|
2018-02-08 21:34:47 +00:00
|
|
|
|
2018-03-31 19:16:54 +00:00
|
|
|
logger.debug(`baseURL: ${options.baseURL}`)
|
|
|
|
logger.debug(`browserBaseURL: ${options.browserBaseURL}`)
|
2017-08-13 11:22:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 20:38:46 +00:00
|
|
|
module.exports = httpModule
|
2017-08-13 11:22:10 +00:00
|
|
|
module.exports.meta = require('../package.json')
|