Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Nuxt3 module & plugin #562

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
297 changes: 148 additions & 149 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -1,155 +1,154 @@
const path = require('path')
const consola = require('consola')
const defu = require('defu')
import { addPluginTemplate } from '@nuxt/kit'
import { resolve } from 'path'
import consola from 'consola'
import defu from 'defu'

const logger = consola.withScope('nuxt:axios')

function axiosModule (_moduleOptions) {
const { nuxt } = this

// Combine options
const moduleOptions = {
...nuxt.options.axios,
..._moduleOptions,
...(nuxt.options.runtimeConfig && nuxt.options.runtimeConfig.axios)
}

// 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'

/* istanbul ignore if */
if (defaultHost === '0.0.0.0') {
defaultHost = 'localhost'
}

// Transpile defu (IE11)
if (nuxt.options.build.transpile /* nuxt 1 */) {
nuxt.options.build.transpile.push(({ isClient }) => isClient && 'defu')
}

// Default prefix
const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/'

// HTTPS
const https = Boolean(this.options.server && this.options.server.https)

// Headers
const headers = {
common: {
Accept: 'application/json, text/plain, */*'
},
delete: {},
get: {},
head: {},
post: {},
put: {},
patch: {}
}

// Support baseUrl alternative
if (moduleOptions.baseUrl) {
moduleOptions.baseURL = moduleOptions.baseUrl
delete moduleOptions.baseUrl
}
if (moduleOptions.browserBaseUrl) {
moduleOptions.browserBaseURL = moduleOptions.browserBaseUrl
delete moduleOptions.browserBaseUrl
}

// Apply defaults
const options = defu(moduleOptions, {
baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
browserBaseURL: undefined,
credentials: false,
debug: false,
progress: true,
proxyHeaders: true,
proxyHeadersIgnore: [
'accept',
'cf-connecting-ip',
'cf-ray',
'content-length',
'content-md5',
'content-type',
'host',
'x-forwarded-host',
'x-forwarded-port',
'x-forwarded-proto'
],
proxy: false,
retry: false,
https,
headers
})

// ENV overrides

/* istanbul ignore if */
if (process.env.API_URL) {
options.baseURL = process.env.API_URL
}

/* istanbul ignore if */
if (process.env.API_URL_BROWSER) {
options.browserBaseURL = process.env.API_URL_BROWSER
}

// Default browserBaseURL
if (typeof options.browserBaseURL === 'undefined') {
options.browserBaseURL = options.proxy ? prefix : options.baseURL
}

// Normalize options
if (options.retry === true) {
options.retry = {}
}

// Convert http:// to https:// if https option is on
if (options.https === true) {
const https = s => s.replace('http://', 'https://')
options.baseURL = https(options.baseURL)
options.browserBaseURL = https(options.browserBaseURL)
}

// globalName
options.globalName = this.nuxt.options.globalName || 'nuxt'

// Register plugin
this.addPlugin({
src: path.resolve(__dirname, 'plugin.js'),
fileName: 'axios.js',
options
})

// Proxy integration
if (options.proxy) {
this.requireModule([
'@nuxtjs/proxy',
typeof options.proxy === 'object' ? options.proxy : {}
])
}

// Set _AXIOS_BASE_URL_ for dynamic SSR baseURL
process.env._AXIOS_BASE_URL_ = options.baseURL

logger.debug(`baseURL: ${options.baseURL}`)
logger.debug(`browserBaseURL: ${options.browserBaseURL}`)
export default async (_moduleOptions, nuxt) => {

// Combine options
const moduleOptions = {
...nuxt.options.axios,
..._moduleOptions,
...(nuxt.options.runtimeConfig && nuxt.options.runtimeConfig.axios)
}

// Default port
const defaultPort =
process.env.API_PORT ||
moduleOptions.port ||
process.env.PORT ||
process.env.npm_package_config_nuxt_port ||
(nuxt.options.server && nuxt.options.server.port) ||
3000

// Default host
let defaultHost =
process.env.API_HOST ||
moduleOptions.host ||
process.env.HOST ||
process.env.npm_package_config_nuxt_host ||
(nuxt.options.server && nuxt.options.server.host) ||
'localhost'

/* istanbul ignore if */
if (defaultHost === '0.0.0.0') {
defaultHost = 'localhost'
}

// Transpile defu (IE11)
if (nuxt.options.build.transpile && process.client/* nuxt 1 */) {
nuxt.options.build.transpile.push('defu')
}

// Default prefix
const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/'

// HTTPS
const https = Boolean(nuxt.options.server && nuxt.options.server.https)

// Headers
const headers = {
common: {
Accept: 'application/json, text/plain, */*'
},
delete: {},
get: {},
head: {},
post: {},
put: {},
patch: {}
}

// Support baseUrl alternative
if (moduleOptions.baseUrl) {
moduleOptions.baseURL = moduleOptions.baseUrl
delete moduleOptions.baseUrl
}
if (moduleOptions.browserBaseUrl) {
moduleOptions.browserBaseURL = moduleOptions.browserBaseUrl
delete moduleOptions.browserBaseUrl
}

// Apply defaults
const options = defu(moduleOptions, {
baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
browserBaseURL: undefined,
credentials: false,
debug: false,
progress: true,
proxyHeaders: true,
proxyHeadersIgnore: [
'accept',
'cf-connecting-ip',
'cf-ray',
'content-length',
'content-md5',
'content-type',
'host',
'if-none-match',
'if-modified-since',
'x-forwarded-host',
'x-forwarded-port',
'x-forwarded-proto'
],
proxy: false,
retry: false,
https,
headers
})

// ENV overrides

/* istanbul ignore if */
if (process.env.API_URL) {
options.baseURL = process.env.API_URL
}

/* istanbul ignore if */
if (process.env.API_URL_BROWSER) {
options.browserBaseURL = process.env.API_URL_BROWSER
}

// Default browserBaseURL
if (typeof options.browserBaseURL === 'undefined') {
options.browserBaseURL = options.proxy ? prefix : options.baseURL
}

// Normalize options
if (options.retry === true) {
options.retry = {}
}

// Convert http:// to https:// if https option is on
if (options.https === true) {
const https = s => s.replace('http://', 'https://')
options.baseURL = https(options.baseURL)
options.browserBaseURL = https(options.browserBaseURL)
}

// globalName
options.globalName = nuxt.options.globalName || 'nuxt'

// Register plugin
addPluginTemplate({
src: resolve(__dirname, 'plugin.js'),
filename: 'axios.options.mjs',
options
})

// Proxy integration
if (options.proxy) {
nuxt.options.proxy = typeof options.proxy === 'object' ? options.proxy : (typeof nuxt.options.proxy === 'object' ? nuxt.options.proxy : {})
nuxt['__module_container__'].requireModule('@nuxtjs/proxy')
}

// Set _AXIOS_BASE_URL_ for dynamic SSR baseURL
process.env._AXIOS_BASE_URL_ = options.baseURL

logger.debug(`baseURL: ${options.baseURL}`)
logger.debug(`browserBaseURL: ${options.browserBaseURL}`)
}

module.exports = axiosModule
module.exports.meta = require('../package.json')
Loading