Skip to content

Commit a341185

Browse files
author
Pooya Parsa
committedAug 30, 2017
feat: upgrade for nuxt rc8
BREAKING CHANGE: app.axios is not available anymore (without $) should always use app.$axios
1 parent a75097d commit a341185

File tree

7 files changed

+619
-142
lines changed

7 files changed

+619
-142
lines changed
 

‎README.md

+10-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
[![js-standard-style](https://cdn.rawgit.com/standard/standard/master/badge.svg)](http://standardjs.com)
1010

11-
> 🔒 Secure and easy [axios](https://github.com/mzabriskie/axios) integration with Nuxt.js.
11+
> Secure and Easy [Axios](https://github.com/mzabriskie/axios) integration with Nuxt.js.
1212
1313
[📖 **Release Notes**](./CHANGELOG.md)
1414

@@ -49,37 +49,28 @@
4949

5050
```js
5151
async asyncData({ app }) {
52-
const ip = await app.axios.$get('http://icanhazip.com')
52+
const ip = await app.$axios.$get('http://icanhazip.com')
5353
return { ip }
5454
}
5555
```
5656

5757
### Store `nuxtServerInit`
5858
```js
5959
async nuxtServerInit ({ commit }, { app }) {
60-
const ip = await app.axios.$get('http://icanhazip.com')
60+
const ip = await app.$axios.$get('http://icanhazip.com')
6161
commit('SET_IP', ip)
6262
}
6363
```
6464

6565
### Store actions
66-
If you need axios instance in store actions, you may have to pass it when dispatching.
66+
(Needs Nuxt >= 1.0.0-RC8)
6767

6868
```js
69-
// In components
70-
export default {
71-
methods: {
72-
updateIP() {
73-
this.$store.dispatch('getIP', { axios: this.$axios })
74-
}
75-
}
76-
}
77-
7869
// In store
7970
{
8071
actions: {
81-
async getIP ({ commit }, { axios }) {
82-
const ip = await axios.$get('http://icanhazip.com')
72+
async getIP ({ commit }) {
73+
const ip = await this.$axios.$get('http://icanhazip.com')
8374
commit('SET_IP', ip)
8475
}
8576
}
@@ -159,10 +150,10 @@ requestInterceptor: (config, { store }) => {
159150
Axios plugin also supports fetch style requests with `$` prefixed methods:
160151
```js
161152
// Normal usage with axios
162-
let data = (await axios.get('...')).data
153+
let data = (await $axios.get('...')).data
163154

164155
// Fetch Style
165-
let data = await axios.$get('...')
156+
let data = await $axios.$get('...')
166157
```
167158

168159
### `setHeader(name, value, scopes='common')`
@@ -245,9 +236,9 @@ Start Nuxt
245236

246237
Now you can make requests to backend: (Works fine in both SSR and Browser)
247238
```js
248-
async asyncData({app}) {
239+
async asyncData({ app }) {
249240
// Magically makes request to http://www.mocky.io/v2/59388bb4120000dc00a672e2
250-
const nuxt = await app.axios.$get('59388bb4120000dc00a672e2')
241+
const nuxt = await app.$axios.$get('59388bb4120000dc00a672e2')
251242

252243
return {
253244
nuxt // -> { nuxt: 'Works!' }

‎lib/index.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = function nuxtAxios (moduleOptions) {
4343
}
4444

4545
// Register plugin
46-
addPlugin.call(this, {
46+
this.addPlugin({
4747
src: path.resolve(__dirname, 'plugin.template.js'),
4848
fileName: 'axios.js',
4949
options
@@ -53,14 +53,4 @@ module.exports = function nuxtAxios (moduleOptions) {
5353
console.log(`[AXIOS] Base URL: ${chalk.green(options.baseURL)} , Browser: ${chalk.green(options.browserBaseURL)}`)
5454
}
5555

56-
// Temporary fix for nuxt/nuxt.js#1127
57-
function addPlugin (template) {
58-
const { dst } = this.addTemplate(template)
59-
// Add to nuxt plugins
60-
this.options.plugins.unshift({
61-
src: path.join(this.options.buildDir, dst),
62-
ssr: template.ssr
63-
})
64-
}
65-
6656
module.exports.meta = require('../package.json')

‎lib/plugin.template.js

+3-26
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
import Axios from 'axios'
22
import Vue from 'vue'
33

4-
const axiosPlugin = {
5-
install() {
6-
if(Vue.__nuxt_axios_installed__) {
7-
return
8-
}
9-
Vue.__nuxt_axios_installed__ = true
10-
11-
if (!Vue.prototype.hasOwnProperty('$axios')) {
12-
Object.defineProperty(Vue.prototype, '$axios', {
13-
get () {
14-
return this.$root.$options.$axios
15-
}
16-
})
17-
}
18-
}
19-
}
20-
21-
Vue.use(axiosPlugin)
22-
234
// We cannot extend Axios.prototype
245
const axiosExtraProto = {}
256

@@ -111,7 +92,7 @@ const baseURL = process.browser
11192
? (process.env.API_URL_BROWSER || '<%= options.browserBaseURL %>')
11293
: (process.env.API_URL || '<%= options.baseURL %>')
11394

114-
export default (ctx) => {
95+
export default (ctx, inject) => {
11596
const { app, store, req } = ctx
11697

11798
// Create a fresh objects for all default header scopes
@@ -182,12 +163,8 @@ export default (ctx) => {
182163
// Error handler
183164
axios.interceptors.response.use(undefined, errorHandler.bind(ctx));
184165

185-
// Make accessible using context
186-
app.axios = app.$axios = axios
187-
ctx.axios = ctx.$axios = axios
188-
if (store) {
189-
store.axios = store.$axios = axios
190-
}
166+
// Publish axios to the context
167+
inject('axios', axios)
191168

192169
// Setup axios helpers
193170
setupHelpers(axios)

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
"dependencies": {
3131
"axios": "^0.16.2",
3232
"chalk": "^2.1.0",
33-
"nuxt": "^1.0.0-rc4",
33+
"nuxt": "^1.0.0-rc8",
3434
"whatwg-url": "^6.1.0"
3535
},
3636
"devDependencies": {
3737
"codecov": "^2.3.0",
38-
"eslint": "^4.4.1",
38+
"eslint": "^4.5.0",
3939
"eslint-config-standard": "^10.2.1",
4040
"eslint-plugin-import": "^2.7.0",
4141
"eslint-plugin-jest": "^20.0.3",

‎test/fixture/pages/asyncData.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<script>
88
export default {
99
async asyncData ({ app }) {
10-
let res = await app.axios.$get('foo/bar')
10+
let res = await app.$axios.$get('foo/bar')
1111
return {
1212
res
1313
}

‎test/fixture/pages/ssr.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default {
1010
async fetch ({ app, route }) {
1111
let doLogin = route.query.login !== undefined
1212
if (doLogin) {
13-
app.axios.setHeader('sessionId', reqCtr++)
13+
app.$axios.setHeader('sessionId', reqCtr++)
1414
}
1515
},
1616
computed: {

0 commit comments

Comments
 (0)