Skip to content

Commit 2ca95e5

Browse files
mercs600pi0
authored andcommittedDec 17, 2019
feat: allow creating new instances (#306)
1 parent 6425515 commit 2ca95e5

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed
 

‎docs/extend.md

+23
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,26 @@ export default function ({ $axios, redirect }) {
3232
})
3333
}
3434
```
35+
36+
### Create new axios instance based on defaults
37+
38+
If you need to create your own axios instance which based on $axios defaults, you can use `create` method.
39+
40+
```js
41+
export default function ({ $axios, redirect }, inject) {
42+
// Create a custom axios instance
43+
const api = $axios.create({
44+
headers: {
45+
common: {
46+
Accept: 'text/plain, */*'
47+
}
48+
}
49+
})
50+
51+
// Set baseURL to something different
52+
api.setBaseURL('https://my_api.com')
53+
54+
// Inject to context as $api
55+
inject('api', api)
56+
}
57+
```

‎lib/plugin.js

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Axios from 'axios'
2+
import defu from 'defu'
23
<% if (options.retry) { %>import axiosRetry from 'axios-retry'<% } %>
34

45
// Axios.prototype cannot be modified
@@ -34,6 +35,9 @@ const axiosExtra = {
3435
onError(fn) {
3536
this.onRequestError(fn)
3637
this.onResponseError(fn)
38+
},
39+
create(options) {
40+
return createAxiosInstance(defu(options, this.defaults))
3741
}
3842
}
3943

@@ -48,6 +52,24 @@ const extendAxiosInstance = axios => {
4852
}
4953
}
5054

55+
const createAxiosInstance = axiosOptions => {
56+
// Create new axios instance
57+
const axios = Axios.create(axiosOptions)
58+
axios.CancelToken = Axios.CancelToken
59+
axios.isCancel = Axios.isCancel
60+
61+
// Extend axios proto
62+
extendAxiosInstance(axios)
63+
64+
// Setup interceptors
65+
<% if (options.debug) { %>setupDebugInterceptor(axios) <% } %>
66+
<% if (options.credentials) { %>setupCredentialsInterceptor(axios)<% } %>
67+
<% if (options.progress) { %>setupProgress(axios) <% } %>
68+
<% if (options.retry) { %>axiosRetry(axios, <%= serialize(options.retry) %>)<% } %>
69+
70+
return axios
71+
}
72+
5173
<% if (options.debug) { %>
5274
const log = (level, ...messages) => console[level]('[Axios]', ...messages)
5375

@@ -91,7 +113,7 @@ const setupCredentialsInterceptor = axios => {
91113
}<% } %>
92114

93115
<% if (options.progress) { %>
94-
const setupProgress = (axios, ctx) => {
116+
const setupProgress = (axios) => {
95117
if (process.server) {
96118
return
97119
}
@@ -182,19 +204,7 @@ export default (ctx, inject) => {
182204
axiosOptions.headers.common['accept-encoding'] = 'gzip, deflate'
183205
}
184206

185-
// Create new axios instance
186-
const axios = Axios.create(axiosOptions)
187-
axios.CancelToken = Axios.CancelToken
188-
axios.isCancel = Axios.isCancel
189-
190-
// Extend axios proto
191-
extendAxiosInstance(axios)
192-
193-
// Setup interceptors
194-
<% if (options.debug) { %>setupDebugInterceptor(axios) <% } %>
195-
<% if (options.credentials) { %>setupCredentialsInterceptor(axios)<% } %>
196-
<% if (options.progress) { %>setupProgress(axios, ctx) <% } %>
197-
<% if (options.retry) { %>axiosRetry(axios, <%= serialize(options.retry) %>)<% } %>
207+
const axios = createAxiosInstance(axiosOptions)
198208

199209
// Inject axios to the context as $axios
200210
ctx.$axios = axios

‎test/axios.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ const testSuite = () => {
5454
})
5555
})
5656

57+
test('createCopy', async () => {
58+
const window = await nuxt.renderAndGetWindow(url('/mounted'))
59+
window.onNuxtReady(() => {
60+
const $axios = window.$nuxt.$axios
61+
const newInstance = $axios.create()
62+
expect(newInstance.defaults.xsrfHeaderName).toBe('X-CSRF-TOKEN')
63+
})
64+
})
65+
5766
test('ssr', async () => {
5867
const makeReq = login => axios
5968
.get(url('/ssr' + (login ? '?login' : '')))

‎test/fixture/pages/ssr.vue

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<div>
33
<div>session-{{ axiosSessionId }}</div>
44
<div>encoding-${{ axiosEncoding }}$</div>
5+
<div>newInstance session-{{ newInstanceSessionId }}</div>
6+
<div>newInstance headers-{{ newInstanceHeaders }}</div>
57
</div>
68
</template>
79

@@ -14,11 +16,23 @@ export default {
1416
axiosSessionId () {
1517
return this.$axios.defaults.headers.common.SessionId
1618
},
17-
1819
axiosEncoding () {
1920
return this.$axios.defaults.headers.common['accept-encoding']
21+
},
22+
newInstanceSessionId () {
23+
return this.newInstance.defaults.headers.common.SessionId
24+
},
25+
newInstanceHeaders () {
26+
return this.newInstance.defaults.headers
2027
}
2128
},
29+
created () {
30+
this.newInstance = this.$axios.create({
31+
headers: {
32+
'X-Requested-With': 'XMLHttpRequest'
33+
}
34+
})
35+
},
2236
fetch ({ app, route }) {
2337
const doLogin = route.query.login !== undefined
2438
if (doLogin) {

0 commit comments

Comments
 (0)
Please sign in to comment.