http/lib/module.js

142 lines
3.8 KiB
JavaScript
Raw Normal View History

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-07-24 11:29:26 +00:00
const semver = require('semver')
2018-03-31 17:52:54 +00:00
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) {
// Combine options
2019-03-18 20:38:46 +00:00
const moduleOptions = { ...this.options.http, ..._moduleOptions }
// Default port
const defaultPort =
process.env.API_PORT ||
moduleOptions.port ||
process.env.PORT ||
process.env.npm_package_config_nuxt_port ||
(this.options.server && this.options.server.port) ||
3000
// Default host
let defaultHost =
process.env.API_HOST ||
moduleOptions.host ||
process.env.HOST ||
process.env.npm_package_config_nuxt_host ||
(this.options.server && this.options.server.host) ||
'localhost'
2017-08-13 13:47:38 +00:00
/* istanbul ignore if */
if (defaultHost === '0.0.0.0') {
defaultHost = 'localhost'
2017-08-13 13:47:38 +00:00
}
// Default prefix
const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/'
// Support baseUrl
if (moduleOptions.baseUrl && !moduleOptions.baseURL) {
moduleOptions.baseURL = moduleOptions.baseUrl
delete moduleOptions.baseUrl
}
// HTTPS
const https = Boolean(this.options.server && this.options.server.https)
2017-08-13 11:22:10 +00:00
// Apply defaults
2019-02-19 08:43:56 +00:00
const options = {
baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
browserBaseURL: undefined,
2019-02-19 08:43:56 +00:00
proxyHeaders: true,
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,
serverTimeout: false,
clientTimeout: false,
https,
headers: {},
2019-02-19 08:43:56 +00:00
...moduleOptions
}
2017-08-13 11:22:10 +00:00
// ENV overrides
2017-08-13 11:22:10 +00:00
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
}
// Default browserBaseURL
if (typeof options.browserBaseURL === 'undefined') {
2018-01-28 19:10:23 +00:00
options.browserBaseURL = options.proxy ? prefix : options.baseURL
2017-08-13 11:22:10 +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
} else if (!isNaN(options.retry)) {
options.retry = parseInt(options.retry)
} else if (typeof options.retry === 'object') {
options.retry = JSON.stringify(options.retry)
}
// Remove port 443 when https
if (options.baseURL.includes('https://')) {
options.baseURL = options.baseURL.replace(':443', '')
}
// Remove port 80 when http
if (options.baseURL.includes('http://')) {
options.baseURL = options.baseURL.replace(':80', '')
}
2018-02-04 19:37:58 +00:00
// Convert http:// to https:// if https option is on
if (options.https === true) {
const https = s => s.replace('http://', 'https://')
2018-02-04 19:37:58 +00:00
options.baseURL = https(options.baseURL)
options.browserBaseURL = https(options.browserBaseURL)
}
2017-08-13 11:22:10 +00:00
// Register plugin
this.addPlugin({
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) {
2018-02-04 18:02:30 +00:00
this.requireModule([
'@nuxtjs/proxy',
typeof options.proxy === 'object' ? options.proxy : {}
])
2018-01-28 19:10:23 +00:00
}
2019-07-24 11:29:26 +00:00
// Add `ky` to build.transpile
this.options.build = this.options.build || {}
this.options.build.transpile = this.options.build.transpile || {}
// transpile only for non-modern build
/* istanbul ignore next */
2019-07-24 11:29:26 +00:00
if (semver.gte(semver.coerce(this.nuxt.constructor.version), '2.9.0')) {
this.options.build.transpile.push(({ isLegacy }) => isLegacy && 'ky')
} else {
this.options.build.transpile.push('ky')
}
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-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')