druxt-router
Advanced tools
Comparing version 0.8.0 to 0.9.0
@@ -27,3 +27,9 @@ import { resolve } from 'path'; | ||
mapState({ | ||
entity: function (state) { return state.druxtRouter.entities[state.druxtRouter.route.data.entity.uuid]; }, | ||
entity: function (state) { | ||
if (!state.druxtRouter.route.data) { | ||
return undefined | ||
} | ||
return state.druxtRouter.entities[state.druxtRouter.route.data.entity.uuid] | ||
}, | ||
redirect: function (state) { return state.druxtRouter.redirect; }, | ||
@@ -379,6 +385,9 @@ route: function (state) { return state.druxtRouter.route.data; } | ||
var this$1 = this; | ||
if ( moduleOptions === void 0 ) moduleOptions = {}; | ||
var options = Object.assign({}, this.options['druxt-router'], | ||
moduleOptions); | ||
// Use root level Druxt options. | ||
if (typeof this.options === 'undefined' || !this.options.druxt) { | ||
throw new TypeError('Druxt settings missing.') | ||
} | ||
var options = this.options.druxt; | ||
options.router = options.router || {}; | ||
@@ -389,4 +398,4 @@ this.extendRoutes(function (routes, resolve) { | ||
// @TODO - Add test for custom component. | ||
if (!options.component) { | ||
options.component = resolve(this$1.options.buildDir, 'components/druxt-router.js'); | ||
if (!options.router.component) { | ||
options.router.component = resolve(this$1.options.buildDir, 'components/druxt-router.js'); | ||
this$1.addTemplate({ | ||
@@ -403,3 +412,3 @@ src: resolve(__dirname, '../nuxt/component.js'), | ||
path: '*', | ||
component: options.component, | ||
component: options.router.component, | ||
chunkName: 'druxt-router' | ||
@@ -406,0 +415,0 @@ }); |
@@ -1,1 +0,1 @@ | ||
var DruxtRouter=function(t,e,r,i,o){"use strict";i=i&&i.hasOwnProperty("default")?i.default:i,o=o&&o.hasOwnProperty("default")?o.default:o;var n={name:"druxt-router",head:function(){return{title:this.title,link:[{rel:"canonical",href:this.canonical||this.route.entity.canonical}],meta:this.metatags||!1}},computed:Object.assign({},{title:function(){return this.route.label}},r.mapState({entity:function(t){return t.druxtRouter.entities[t.druxtRouter.route.data.entity.uuid]},redirect:function(t){return t.druxtRouter.redirect},route:function(t){return t.druxtRouter.route.data}})),render:function(t){if(void 0!==this.entity)return t("div",{key:this.route.entity.uuid,props:{type:""+this.route.jsonapi.resourceName,uuid:this.route.entity.uuid}},JSON.stringify(this.entity.data))},fetch:async function(t){var e=t.store,r=t.redirect,i=t.route,o=await e.dispatch("druxtRouter/get",i.fullPath);o.redirect&&r(o.redirect)}},u=function(t,e){if(void 0===e&&(e={}),!t)throw new Error("The 'baseURL' parameter is required.");var r={baseURL:t};"object"==typeof e.axios&&(r=Object.assign(r,e.axios)),this.axios=i.create(r),this.setOptions(e)};u.prototype.setOptions=function(t){void 0===t&&(t={}),"function"==typeof t.preprocessEntity&&(this.preprocessEntity=t.preprocessEntity)},u.prototype.get=async function(t){var e=await this.getRoute(t),r=e.data,i=e.error;if(i)return{route:r,error:i};var o=this.getRedirect(t,r);return{entity:await this.getResourceByRoute(r),redirect:o,route:r}},u.prototype.getRedirect=function(t,e){if(Array.isArray(e.redirect)&&void 0!==e.redirect[0].to)return e.redirect[0].to;if(e.isHomePath)return"/"!==t&&"/";if("string"==typeof e.resolved){var r=new o(e.resolved);if(t!==r.pathname)return r.pathname}return!1},u.prototype.getResource=async function(t){var e=t.id,r=t.type,i="/api/"+r.replace("--","/")+"/"+e,o=await this.axios.get(i),n={id:e,type:r,data:o.data};return this.preprocessEntity&&(n._raw=n.data,n.data=await this.preprocessEntity(o)),n},u.prototype.getResourceByRoute=function(t){return this.getResource({id:t.entity.uuid,type:t.jsonapi.resourceName})},u.prototype.getRoute=async function(t){var e="/router/translate-path?path="+t,r=await this.axios.get(e,{validateStatus:function(t){return t<500}}),i=r.data,o=!1;return r.status>=200&&r.status<300||(o={statusCode:r.status,message:r.statusText}),{data:i,error:o}};var a=require("vuex"),s={props:{uuid:{type:String,required:!0},type:{type:String,required:!0}},created:function(){var t=this;void 0===this.entities[this.uuid]&&(this.loading=!0,this.getEntity({id:this.uuid,type:this.type}).then((function(e){t.loading="function"==typeof t.onDruxtRouterLoad&&!!t.onDruxtRouterLoad(e)})))},data:function(){return{loading:!1}},computed:Object.assign({},{entity:function(){return this.entities[this.uuid]},ready:function(){return!this.loading&&!!this.entity}},a.mapState({entities:function(t){return t.druxtRouter.entities}})),methods:Object.assign({},a.mapActions({getEntity:"druxtRouter/getEntity"}))};return t.DruxtRouter=u,t.DruxtRouterComponent=n,t.DruxtRouterEntityMixin=s,t.DruxtRouterStore=function(t){var e=t.store;if(void 0===e)throw new TypeError("Vuex store not found.");e.registerModule("druxtRouter",{namespaced:!0,state:function(){return{entities:{},redirect:!1,route:{},routes:{}}},mutations:{addEntity:function(t,e){void 0!==e.id&&(t.entities[e.id]=e)},setRedirect:function(t,e){t.redirect=e},addRoute:function(t,e){var r=e.path,i=e.route;"string"==typeof r&&void 0!==i&&(t.routes[r]=i)},setRoute:function(t,e){"string"==typeof e&&void 0!==t.routes[e]&&(t.route=t.routes[e])}},actions:{get:async function(t,e){var r=t.commit,i=t.dispatch,o=(t.state,await i("getRoute",e)),n=o.data,u=o.error;if(void 0!==u.statusCode)return this.app.context.error(u);r("setRoute",e);var a=this.$druxtRouter().getRedirect(e,n);return r("setRedirect",a),{entity:await i("getEntity",{id:n.entity.uuid,type:n.jsonapi.resourceName}),redirect:a,route:n}},getEntity:async function(t,e){var r=t.commit,i=t.state;if(void 0!==i.entities[e.id])return i.entities[e.id];var o=await this.$druxtRouter().getResource(e);return r("addEntity",o),o},getRoute:async function(t,e){var r=t.commit,i=t.state;if(void 0!==i.routes[e])return i.routes[e];var o=await this.$druxtRouter().getRoute(e);return r("addRoute",{path:e,route:o}),o}}},{preserveState:Boolean(e.state.druxtRouter)})},t.default=function(t){var r=this;void 0===t&&(t={});var i=Object.assign({},this.options["druxt-router"],t);this.extendRoutes((function(t,e){i.component||(i.component=e(r.options.buildDir,"components/druxt-router.js"),r.addTemplate({src:e(__dirname,"../nuxt/component.js"),fileName:"components/druxt-router.js",options:i})),t.push({name:"druxt-router",path:"*",component:i.component,chunkName:"druxt-router"})})),this.addPlugin({src:e.resolve(__dirname,"../nuxt/plugin.js"),fileName:"druxt-router.js",options:i}),this.addPlugin({src:e.resolve(__dirname,"../nuxt/store.js"),fileName:"store/druxt-router.js",options:i})},t}({},path,vuex,axios,Url); | ||
var DruxtRouter=function(t,e,r,i,o){"use strict";i=i&&i.hasOwnProperty("default")?i.default:i,o=o&&o.hasOwnProperty("default")?o.default:o;var n={name:"druxt-router",head:function(){return{title:this.title,link:[{rel:"canonical",href:this.canonical||this.route.entity.canonical}],meta:this.metatags||!1}},computed:Object.assign({},{title:function(){return this.route.label}},r.mapState({entity:function(t){if(t.druxtRouter.route.data)return t.druxtRouter.entities[t.druxtRouter.route.data.entity.uuid]},redirect:function(t){return t.druxtRouter.redirect},route:function(t){return t.druxtRouter.route.data}})),render:function(t){if(void 0!==this.entity)return t("div",{key:this.route.entity.uuid,props:{type:""+this.route.jsonapi.resourceName,uuid:this.route.entity.uuid}},JSON.stringify(this.entity.data))},fetch:async function(t){var e=t.store,r=t.redirect,i=t.route,o=await e.dispatch("druxtRouter/get",i.fullPath);o.redirect&&r(o.redirect)}},u=function(t,e){if(void 0===e&&(e={}),!t)throw new Error("The 'baseURL' parameter is required.");var r={baseURL:t};"object"==typeof e.axios&&(r=Object.assign(r,e.axios)),this.axios=i.create(r),this.setOptions(e)};u.prototype.setOptions=function(t){void 0===t&&(t={}),"function"==typeof t.preprocessEntity&&(this.preprocessEntity=t.preprocessEntity)},u.prototype.get=async function(t){var e=await this.getRoute(t),r=e.data,i=e.error;if(i)return{route:r,error:i};var o=this.getRedirect(t,r);return{entity:await this.getResourceByRoute(r),redirect:o,route:r}},u.prototype.getRedirect=function(t,e){if(Array.isArray(e.redirect)&&void 0!==e.redirect[0].to)return e.redirect[0].to;if(e.isHomePath)return"/"!==t&&"/";if("string"==typeof e.resolved){var r=new o(e.resolved);if(t!==r.pathname)return r.pathname}return!1},u.prototype.getResource=async function(t){var e=t.id,r=t.type,i="/api/"+r.replace("--","/")+"/"+e,o=await this.axios.get(i),n={id:e,type:r,data:o.data};return this.preprocessEntity&&(n._raw=n.data,n.data=await this.preprocessEntity(o)),n},u.prototype.getResourceByRoute=function(t){return this.getResource({id:t.entity.uuid,type:t.jsonapi.resourceName})},u.prototype.getRoute=async function(t){var e="/router/translate-path?path="+t,r=await this.axios.get(e,{validateStatus:function(t){return t<500}}),i=r.data,o=!1;return r.status>=200&&r.status<300||(o={statusCode:r.status,message:r.statusText}),{data:i,error:o}};var s=require("vuex"),a={props:{uuid:{type:String,required:!0},type:{type:String,required:!0}},created:function(){var t=this;void 0===this.entities[this.uuid]&&(this.loading=!0,this.getEntity({id:this.uuid,type:this.type}).then((function(e){t.loading="function"==typeof t.onDruxtRouterLoad&&!!t.onDruxtRouterLoad(e)})))},data:function(){return{loading:!1}},computed:Object.assign({},{entity:function(){return this.entities[this.uuid]},ready:function(){return!this.loading&&!!this.entity}},s.mapState({entities:function(t){return t.druxtRouter.entities}})),methods:Object.assign({},s.mapActions({getEntity:"druxtRouter/getEntity"}))};return t.DruxtRouter=u,t.DruxtRouterComponent=n,t.DruxtRouterEntityMixin=a,t.DruxtRouterStore=function(t){var e=t.store;if(void 0===e)throw new TypeError("Vuex store not found.");e.registerModule("druxtRouter",{namespaced:!0,state:function(){return{entities:{},redirect:!1,route:{},routes:{}}},mutations:{addEntity:function(t,e){void 0!==e.id&&(t.entities[e.id]=e)},setRedirect:function(t,e){t.redirect=e},addRoute:function(t,e){var r=e.path,i=e.route;"string"==typeof r&&void 0!==i&&(t.routes[r]=i)},setRoute:function(t,e){"string"==typeof e&&void 0!==t.routes[e]&&(t.route=t.routes[e])}},actions:{get:async function(t,e){var r=t.commit,i=t.dispatch,o=(t.state,await i("getRoute",e)),n=o.data,u=o.error;if(void 0!==u.statusCode)return this.app.context.error(u);r("setRoute",e);var s=this.$druxtRouter().getRedirect(e,n);return r("setRedirect",s),{entity:await i("getEntity",{id:n.entity.uuid,type:n.jsonapi.resourceName}),redirect:s,route:n}},getEntity:async function(t,e){var r=t.commit,i=t.state;if(void 0!==i.entities[e.id])return i.entities[e.id];var o=await this.$druxtRouter().getResource(e);return r("addEntity",o),o},getRoute:async function(t,e){var r=t.commit,i=t.state;if(void 0!==i.routes[e])return i.routes[e];var o=await this.$druxtRouter().getRoute(e);return r("addRoute",{path:e,route:o}),o}}},{preserveState:Boolean(e.state.druxtRouter)})},t.default=function(t){var r=this;if(void 0===this.options||!this.options.druxt)throw new TypeError("Druxt settings missing.");var i=this.options.druxt;i.router=i.router||{},this.extendRoutes((function(t,e){i.router.component||(i.router.component=e(r.options.buildDir,"components/druxt-router.js"),r.addTemplate({src:e(__dirname,"../nuxt/component.js"),fileName:"components/druxt-router.js",options:i})),t.push({name:"druxt-router",path:"*",component:i.router.component,chunkName:"druxt-router"})})),this.addPlugin({src:e.resolve(__dirname,"../nuxt/plugin.js"),fileName:"druxt-router.js",options:i}),this.addPlugin({src:e.resolve(__dirname,"../nuxt/store.js"),fileName:"store/druxt-router.js",options:i})},t}({},path,vuex,axios,Url); |
@@ -22,3 +22,9 @@ 'use strict';Object.defineProperty(exports,'__esModule',{value:true});function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}var path=require('path'),vuex$1=require('vuex'),axios=_interopDefault(require('axios')),Url=_interopDefault(require('url-parse'));var DruxtRouterComponent = { | ||
vuex$1.mapState({ | ||
entity: function (state) { return state.druxtRouter.entities[state.druxtRouter.route.data.entity.uuid]; }, | ||
entity: function (state) { | ||
if (!state.druxtRouter.route.data) { | ||
return undefined | ||
} | ||
return state.druxtRouter.entities[state.druxtRouter.route.data.entity.uuid] | ||
}, | ||
redirect: function (state) { return state.druxtRouter.redirect; }, | ||
@@ -366,6 +372,9 @@ route: function (state) { return state.druxtRouter.route.data; } | ||
var this$1 = this; | ||
if ( moduleOptions === void 0 ) moduleOptions = {}; | ||
var options = Object.assign({}, this.options['druxt-router'], | ||
moduleOptions); | ||
// Use root level Druxt options. | ||
if (typeof this.options === 'undefined' || !this.options.druxt) { | ||
throw new TypeError('Druxt settings missing.') | ||
} | ||
var options = this.options.druxt; | ||
options.router = options.router || {}; | ||
@@ -376,4 +385,4 @@ this.extendRoutes(function (routes, resolve) { | ||
// @TODO - Add test for custom component. | ||
if (!options.component) { | ||
options.component = resolve(this$1.options.buildDir, 'components/druxt-router.js'); | ||
if (!options.router.component) { | ||
options.router.component = resolve(this$1.options.buildDir, 'components/druxt-router.js'); | ||
this$1.addTemplate({ | ||
@@ -390,3 +399,3 @@ src: resolve(__dirname, '../nuxt/component.js'), | ||
path: '*', | ||
component: options.component, | ||
component: options.router.component, | ||
chunkName: 'druxt-router' | ||
@@ -393,0 +402,0 @@ }); |
@@ -1,3 +0,3 @@ | ||
import { DruxtRouterComponent } from '<%= options.importPath ? `${options.importPath}/..` : 'druxt-router' %>' | ||
import { DruxtRouterComponent } from 'druxt-router' | ||
export default DruxtRouterComponent |
@@ -1,3 +0,3 @@ | ||
import { DruxtRouter } from '<%= options.importPath ? options.importPath : 'druxt-router' %>' | ||
<% if (options.JSONAPIDeserializer) { %>import { Deserializer } from 'jsonapi-serializer'<% } %> | ||
import { DruxtRouter } from 'druxt-router' | ||
<% if (options.router.JSONAPIDeserializer) { %>import { Deserializer } from 'jsonapi-serializer'<% } %> | ||
@@ -13,6 +13,6 @@ export default (context, inject) => { | ||
<% if (options.JSONAPIDeserializer) { %> | ||
<% if (options.router.JSONAPIDeserializer) { %> | ||
// JSONAPIDeserialzer. | ||
options.preprocessEntity = async resource => { | ||
const options = <%= JSON.stringify(options.JSONAPIDeserializer) %> | ||
const options = <%= JSON.stringify(options.router.JSONAPIDeserializer) %> | ||
@@ -19,0 +19,0 @@ // Build map of relationships. |
@@ -1,3 +0,3 @@ | ||
import { DruxtRouterStore } from '<%= options.importPath ? `${options.importPath}/..` : 'druxt-router' %>' | ||
import { DruxtRouterStore } from 'druxt-router' | ||
export default DruxtRouterStore |
{ | ||
"name": "druxt-router", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"description": "Provides a Nuxt <-> Drupal router.", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -21,6 +21,11 @@ # Druxt router | ||
... | ||
['druxt-router', { | ||
baseUrl: 'https://example.com', | ||
}] | ||
] | ||
'druxt-router' | ||
], | ||
druxt: { | ||
baseUrl: 'https://example.com', | ||
router: { | ||
JSONAPIDeserializer: true | ||
} | ||
} | ||
} | ||
@@ -33,2 +38,6 @@ ``` | ||
### Base Druxt options | ||
These options are available to all Druxt modules. | ||
| Option | Type | Required | Default | Description | | ||
@@ -38,3 +47,10 @@ | --- | --- | --- | --- | --- | | ||
| `baseUrl` | `string` | Yes | `null` | Base URL for the Drupal installation. | | ||
| `JSONAPIDeserializer` | `boolean` | No | `false` | Enable/Disable [JSON API Serializer](https://www.npmjs.com/package/jsonapi-serializer) parsing of Drupal entities. | | ||
### Router specific options | ||
These options are specific to this module. | ||
| Option | Type | Required | Default | Description | | ||
| --- | --- | --- | --- | --- | | ||
| `router.JSONAPIDeserializer` | `boolean` | No | `false` | Enable/Disable [JSON API Serializer](https://www.npmjs.com/package/jsonapi-serializer) parsing of Drupal entities. | | ||
32135
727
54