This commit is contained in:
2022-07-18 02:50:52 +00:00
parent befd344ab0
commit 06181b34d6
8569 changed files with 818704 additions and 352705 deletions
+27 -1
View File
@@ -37,6 +37,11 @@ function axiosModule (_moduleOptions) {
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 || '/'
@@ -56,6 +61,16 @@ function axiosModule (_moduleOptions) {
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}`,
@@ -64,7 +79,18 @@ function axiosModule (_moduleOptions) {
debug: false,
progress: true,
proxyHeaders: true,
proxyHeadersIgnore: ['accept', 'host', 'cf-ray', 'cf-connecting-ip', 'content-length', 'content-md5', 'content-type'],
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,
+19 -10
View File
@@ -8,10 +8,10 @@ const axiosExtra = {
this.defaults.baseURL = baseURL
},
setHeader (name, value, scopes = 'common') {
for (let scope of Array.isArray(scopes) ? scopes : [ scopes ]) {
for (const scope of Array.isArray(scopes) ? scopes : [ scopes ]) {
if (!value) {
delete this.defaults.headers[scope][name];
return
continue
}
this.defaults.headers[scope][name] = value
}
@@ -42,12 +42,12 @@ const axiosExtra = {
}
// Request helpers ($get, $post, ...)
for (let method of ['request', 'delete', 'get', 'head', 'options', 'post', 'put', 'patch']) {
for (const method of ['request', 'delete', 'get', 'head', 'options', 'post', 'put', 'patch']) {
axiosExtra['$' + method] = function () { return this[method].apply(this, arguments).then(res => res && res.data) }
}
const extendAxiosInstance = axios => {
for (let key in axiosExtra) {
for (const key in axiosExtra) {
axios[key] = axiosExtra[key].bind(axios)
}
}
@@ -61,11 +61,16 @@ const createAxiosInstance = axiosOptions => {
// Extend axios proto
extendAxiosInstance(axios)
// Intercept to apply default headers
axios.onRequest((config) => {
config.headers = { ...axios.defaults.headers.common, ...config.headers }
})
// Setup interceptors
<% if (options.debug) { %>setupDebugInterceptor(axios) <% } %>
<% if (options.credentials) { %>setupCredentialsInterceptor(axios)<% } %>
<% if (options.credentials) { %>setupCredentialsInterceptor(axios) <% } %>
<% if (options.progress) { %>setupProgress(axios) <% } %>
<% if (options.retry) { %>axiosRetry(axios, <%= serialize(options.retry) %>)<% } %>
<% if (options.retry) { %>axiosRetry(axios, <%= serialize(options.retry) %>) <% } %>
return axios
}
@@ -161,6 +166,10 @@ const setupProgress = (axios) => {
currentRequests--
if (Axios.isCancel(error)) {
if (currentRequests <= 0) {
currentRequests = 0
$loading().finish()
}
return
}
@@ -169,7 +178,7 @@ const setupProgress = (axios) => {
})
const onProgress = e => {
if (!currentRequests) {
if (!currentRequests || !e.total) {
return
}
const progress = ((e.loaded * 100) / (e.total * currentRequests))
@@ -185,8 +194,8 @@ export default (ctx, inject) => {
const runtimeConfig = ctx.$config && ctx.$config.axios || {}
// baseURL
const baseURL = process.browser
? (runtimeConfig.browserBaseURL || runtimeConfig.baseURL || '<%= options.browserBaseURL || '' %>')
: (runtimeConfig.baseURL || process.env._AXIOS_BASE_URL_ || '<%= options.baseURL || '' %>')
? (runtimeConfig.browserBaseURL || runtimeConfig.browserBaseUrl || runtimeConfig.baseURL || runtimeConfig.baseUrl || '<%= options.browserBaseURL || '' %>')
: (runtimeConfig.baseURL || runtimeConfig.baseUrl || process.env._AXIOS_BASE_URL_ || '<%= options.baseURL || '' %>')
// Create fresh objects for all default header scopes
// Axios creates only one which is shared across SSR requests!
@@ -202,7 +211,7 @@ export default (ctx, inject) => {
// Proxy SSR request headers headers
if (process.server && ctx.req && ctx.req.headers) {
const reqHeaders = { ...ctx.req.headers }
for (let h of <%= serialize(options.proxyHeadersIgnore) %>) {
for (const h of <%= serialize(options.proxyHeadersIgnore) %>) {
delete reqHeaders[h]
}
axiosOptions.headers.common = { ...reqHeaders, ...axiosOptions.headers.common }