Skip to content

Commit abb0908

Browse files
committed
Add experimental support for Nuxt.js
1 parent 2fe403a commit abb0908

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
### Unreleased
22

3+
### 1.1.0
4+
- Add experimental support for using the plugin in Nuxt.js applications
35

46
### 1.0.0
57
- Created the library by extracting code from https://github.com/ecamp/ecamp3

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,48 @@ this.api.reload(someEntity)
6060
<li v-for="book in api.get('/all/my/books').items" :key="book._meta.self">...</li>
6161
```
6262

63+
### Nuxt.js (experimental support)
64+
To install in a Nuxt.js application:
65+
```js
66+
// First, make sure the Nuxt.js app uses Vuex, by adding an index.js to your store/ directory.
67+
68+
// Then, create a file plugins/hal-json-vuex.js with the following content:
69+
import Vue from 'vue'
70+
import HalJsonVuex from 'hal-json-vuex'
71+
72+
export default function ({ store, $axios }, nuxtInject) {
73+
if (!Vue.$api) {
74+
Vue.use(HalJsonVuex(store, $axios, { nuxtInject }))
75+
}
76+
}
77+
78+
// Add the plugin to nuxt.config.js:
79+
export default {
80+
plugins: [
81+
{ src: '~/plugins/hal-json-vuex.js' }
82+
],
83+
// ...
84+
}
85+
```
86+
Then, you can use `$api` on both the server side and the client side:
87+
```js
88+
// On the server
89+
async asyncData({ $api }) {
90+
const books = await $api.get().books()._meta.load
91+
return { books }
92+
}
93+
```
94+
95+
```js
96+
// On the client, in a computed or method or lifecycle hook of a Vue component
97+
let someEntity = this.$api.get('/some/endpoint')
98+
```
99+
100+
```html
101+
<!-- On the client, in the <template> part of a Vue component -->
102+
<li v-for="book in $api.get('/all/my/books').items" :key="book._meta.self">...</li>
103+
```
104+
63105
# Available options
64106

65107
### apiName

src/index.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,31 @@ export class ServerException extends Error {
3434
function HalJsonVuex (store, axios, options) {
3535
const defaultOptions = {
3636
forceRequestedSelfLink: false,
37-
apiName: 'api'
37+
apiName: 'api',
38+
nuxtInject: null
3839
}
3940
const opts = { ...defaultOptions, ...options }
4041

4142
store.registerModule(opts.apiName, { state: {}, ...storeModule })
4243

4344
const storeValueProxy = StoreValueProxyCreator(axios.defaults.baseURL, get)
4445

46+
if (opts.nuxtInject !== null) axios = adaptNuxtAxios(axios)
47+
48+
/**
49+
* Since Nuxt.js uses $get, $post etc., we need to use an adapter in the case of a Nuxt.js app...
50+
* @param $axios
51+
*/
52+
function adaptNuxtAxios ($axios) {
53+
return {
54+
get: $axios.$get,
55+
patch: $axios.$patch,
56+
post: $axios.$post,
57+
delete: $axios.$delete,
58+
...$axios
59+
}
60+
}
61+
4562
/**
4663
* Sends a POST request to the backend, in order to create a new entity. Note that this does not
4764
* reload any collections that this new entity might be in, the caller has to do that on its own.
@@ -417,13 +434,21 @@ function HalJsonVuex (store, axios, options) {
417434
const halJsonVuex = { post, get, reload, del, patch, purge, purgeAll, href }
418435

419436
function install (Vue) {
420-
Object.defineProperties(Vue.prototype, {
421-
[opts.apiName]: {
422-
get () {
423-
return halJsonVuex
437+
if (this.installed) return
438+
439+
if (opts.nuxtInject === null) {
440+
// Normal installation in a Vue app
441+
Object.defineProperties(Vue.prototype, {
442+
[opts.apiName]: {
443+
get () {
444+
return halJsonVuex
445+
}
424446
}
425-
}
426-
})
447+
})
448+
} else {
449+
// Support for Nuxt-style inject installation
450+
opts.nuxtInject(opts.apiName, halJsonVuex)
451+
}
427452
}
428453

429454
return { ...halJsonVuex, install }

0 commit comments

Comments
 (0)