2017-08-13 11:22:10 +00:00
|
|
|
const chalk = require('chalk')
|
|
|
|
const path = require('path')
|
2017-08-30 17:14:18 +00:00
|
|
|
const debug = require('debug')('nuxt:axios')
|
2017-08-13 11:22:10 +00:00
|
|
|
|
2018-01-16 17:15:12 +00:00
|
|
|
module.exports = function nuxtAxios (_moduleOptions) {
|
|
|
|
// Combine options
|
|
|
|
const moduleOptions = Object.assign({}, this.options.axios, _moduleOptions)
|
|
|
|
|
|
|
|
// Default port
|
|
|
|
const defaultPort =
|
|
|
|
process.env.API_PORT ||
|
|
|
|
moduleOptions.port ||
|
|
|
|
process.env.PORT ||
|
|
|
|
process.env.npm_package_config_nuxt_port ||
|
|
|
|
3000
|
|
|
|
|
|
|
|
// Default host
|
|
|
|
let defaultHost =
|
|
|
|
process.env.API_HOST ||
|
|
|
|
moduleOptions.host ||
|
|
|
|
process.env.HOST ||
|
|
|
|
process.env.npm_package_config_nuxt_host ||
|
|
|
|
'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
|
|
|
}
|
|
|
|
|
2018-01-16 17:15:12 +00:00
|
|
|
// Default prefix
|
|
|
|
const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/'
|
|
|
|
|
2017-08-13 11:22:10 +00:00
|
|
|
// Apply defaults
|
2018-01-16 17:15:12 +00:00
|
|
|
const options = Object.assign(
|
|
|
|
{
|
|
|
|
baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
|
|
|
|
browserBaseURL: null,
|
2018-01-28 13:57:39 +00:00
|
|
|
credentials: false,
|
2018-01-28 18:40:35 +00:00
|
|
|
debug: false,
|
2018-01-28 18:22:32 +00:00
|
|
|
progress: true,
|
2018-01-28 18:40:35 +00:00
|
|
|
proxyHeaders: true,
|
2018-01-16 17:15:12 +00:00
|
|
|
proxyHeadersIgnore: ['accept', 'host'],
|
2018-02-04 18:17:05 +00:00
|
|
|
proxy: false,
|
|
|
|
retry: false
|
2018-01-16 17:15:12 +00:00
|
|
|
},
|
|
|
|
moduleOptions
|
|
|
|
)
|
2017-08-13 11:22:10 +00:00
|
|
|
|
2018-01-16 17:15:12 +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
|
2018-01-29 22:18:47 +00:00
|
|
|
} else {
|
|
|
|
process.env.API_URL = options.baseURL
|
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_BROWSER) {
|
|
|
|
options.browserBaseURL = process.env.API_URL_BROWSER
|
|
|
|
}
|
|
|
|
|
2018-01-16 17:15:12 +00:00
|
|
|
// Default browserBaseURL
|
2017-08-13 11:22:10 +00:00
|
|
|
if (!options.browserBaseURL) {
|
2018-01-28 19:10:23 +00:00
|
|
|
options.browserBaseURL = options.proxy ? prefix : options.baseURL
|
2017-08-13 11:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 18:28:25 +00:00
|
|
|
// Normalize options
|
|
|
|
if (options.retry === true) {
|
|
|
|
options.retry = {}
|
|
|
|
}
|
|
|
|
|
2017-08-13 11:22:10 +00:00
|
|
|
// Register plugin
|
2017-08-30 17:09:30 +00:00
|
|
|
this.addPlugin({
|
2017-08-13 12:10:55 +00:00
|
|
|
src: path.resolve(__dirname, 'plugin.template.js'),
|
2017-08-13 11:22:10 +00:00
|
|
|
fileName: 'axios.js',
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-13 11:22:10 +00:00
|
|
|
/* eslint-disable no-console */
|
2018-01-16 17:15:12 +00:00
|
|
|
debug(
|
|
|
|
`BaseURL: ${chalk.green(options.baseURL)} (Browser: ${chalk.green(
|
|
|
|
options.browserBaseURL
|
|
|
|
)})`
|
|
|
|
)
|
2017-08-13 11:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.meta = require('../package.json')
|