@@ -34,14 +34,31 @@ export class ServerException extends Error {
3434function 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