druxt-router
Advanced tools
Comparing version 0.4.2 to 0.5.0
import { mapState } from 'vuex'; | ||
import axios from 'axios'; | ||
import Url from 'url-parse'; | ||
@@ -26,2 +27,3 @@ var DruxtRouterComponent = { | ||
entity: function (state) { return state.druxtRouter.entities[state.druxtRouter.route.entity.uuid]; }, | ||
redirect: function (state) { return state.druxtRouter.redirect; }, | ||
route: function (state) { return state.druxtRouter.route; } | ||
@@ -34,3 +36,3 @@ })), | ||
props: { | ||
type: ((this.route.entity.type) + "--" + (this.route.entity.bundle)), | ||
type: ("" + (this.route.jsonapi.resourceName)), | ||
uuid: this.route.entity.uuid | ||
@@ -41,7 +43,13 @@ } | ||
fetch: function fetch (ref) { | ||
fetch: async function fetch (ref) { | ||
var store = ref.store; | ||
var redirect = ref.redirect; | ||
var route = ref.route; | ||
return store.dispatch('druxtRouter/getEntityByRouter', route.fullPath) | ||
var result = await store.dispatch('druxtRouter/get', route.fullPath); | ||
// Process redirect. | ||
if (result.redirect) { | ||
redirect(result.redirect); | ||
} | ||
} | ||
@@ -82,2 +90,4 @@ }; | ||
var redirect = this.getRedirect(path, route); | ||
// Get entity from API. | ||
@@ -87,6 +97,36 @@ // @TODO - Add validation/error handling. | ||
return { entity: entity, route: route } | ||
return { entity: entity, redirect: redirect, route: route } | ||
}; | ||
/** | ||
* @param object route | ||
*/ | ||
DruxtRouter.prototype.getRedirect = function getRedirect (path, route) { | ||
// Redirect to route provided redirect. | ||
if (Array.isArray(route.redirect) && typeof route.redirect[0].to !== 'undefined') { | ||
return route.redirect[0].to | ||
} | ||
// Redirect to root if route is home path but path isn't root. | ||
if (route.isHomePath) { | ||
if (path !== '/') { | ||
return '/' | ||
} | ||
return false | ||
} | ||
// Redirect if path does not match resolved clean url path. | ||
if (typeof route.resolved === 'string') { | ||
var url = new Url(route.resolved); | ||
if (path !== url.pathname) { | ||
return url.pathname | ||
} | ||
} | ||
return false | ||
}; | ||
/** | ||
* Get a JSON:API resource by type and ID. | ||
@@ -101,7 +141,3 @@ * | ||
var ref$1 = this.convertResourceToEntityBundle(type); | ||
var entityType = ref$1.entityType; | ||
var bundle = ref$1.bundle; | ||
var url = "/api/" + entityType + "/" + bundle + "/" + id; | ||
var url = "/api/" + (type.replace('--', '/')) + "/" + id; | ||
var response = await this.axios.get(url); | ||
@@ -142,18 +178,2 @@ | ||
/** | ||
* Convert a JSON:API resource type to a Drupal Entity/Bundle. | ||
* | ||
* @todo Add support for JSON:API Extras. | ||
* | ||
* @param string type | ||
*/ | ||
DruxtRouter.prototype.convertResourceToEntityBundle = function convertResourceToEntityBundle (type) { | ||
var parts = type.split('--'); | ||
return { | ||
entityType: parts[0], | ||
bundle: parts[1] | ||
} | ||
}; | ||
var vuex = require('vuex'); | ||
@@ -232,2 +252,3 @@ | ||
entities: {}, | ||
redirect: false, | ||
route: {}, | ||
@@ -246,2 +267,6 @@ routes: {} | ||
setRedirect: function setRedirect (state, redirect) { | ||
state.redirect = redirect; | ||
}, | ||
addRoute: function addRoute (state, ref) { | ||
@@ -270,18 +295,36 @@ var path = ref.path; | ||
actions: { | ||
getEntityByRouter: async function getEntityByRouter (ref, path) { | ||
get: async function get (ref, path) { | ||
var commit = ref.commit; | ||
var dispatch = ref.dispatch; | ||
var state = ref.state; | ||
// Return cached data, if present. | ||
// @TODO - Handle cache busting. | ||
if (typeof state.routes[path] !== 'undefined' && typeof state.entities[state.routes[path].entity.uuid] !== 'undefined') { | ||
var route = state.entities[state.routes[path].entity.uuid]; | ||
var redirect = this.$druxtRouter().getRedirect(path, route); | ||
var entity = state.entities[state.routes[path].entity.uuid]; | ||
commit('setRoute', path); | ||
commit('setRedirect', redirect); | ||
return { entity: entity, redirect: redirect, route: route } | ||
} | ||
// Get data from router. | ||
try { | ||
var ref$1 = await this.$druxtRouter().get(path); | ||
var entity = ref$1.entity; | ||
var route = ref$1.route; | ||
var entity$1 = ref$1.entity; | ||
var redirect$1 = ref$1.redirect; | ||
var route$1 = ref$1.route; | ||
commit('addRoute', { path: path, route: route }); | ||
commit('addRoute', { path: path, redirect: redirect$1, route: route$1 }); | ||
commit('setRoute', path); | ||
commit('addEntity', entity); | ||
commit('setRedirect', redirect$1); | ||
return entity | ||
commit('addEntity', entity$1); | ||
return { entity: entity$1, redirect: redirect$1, route: route$1 } | ||
// Handle errors. | ||
} catch (err) { | ||
@@ -288,0 +331,0 @@ if (typeof err.response === 'undefined') { |
@@ -1,1 +0,1 @@ | ||
var DruxtRouter=function(t,e,r){"use strict";r=r&&r.hasOwnProperty("default")?r.default:r;var i={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}},e.mapState({entity:function(t){return t.druxtRouter.entities[t.druxtRouter.route.entity.uuid]},route:function(t){return t.druxtRouter.route}})),render:function(t){return t("div",{key:this.route.entity.uuid,props:{type:this.route.entity.type+"--"+this.route.entity.bundle,uuid:this.route.entity.uuid}},JSON.stringify(this.route))},fetch:function(t){var e=t.store,r=t.route;return e.dispatch("druxtRouter/getEntityByRouter",r.fullPath)}},n=function(t,e){if(void 0===e&&(e={}),!t)throw new Error("The 'baseURL' parameter is required.");this.axios=r.create({baseURL:t}),this.setOptions(e)};n.prototype.setOptions=function(t){void 0===t&&(t={}),"function"==typeof t.preprocessEntity&&(this.preprocessEntity=t.preprocessEntity)},n.prototype.get=async function(t){var e=await this.getRoute(t);return{entity:await this.getResourceByRoute(e),route:e}},n.prototype.getResource=async function(t){var e=t.id,r=t.type,i=this.convertResourceToEntityBundle(r),n="/api/"+i.entityType+"/"+i.bundle+"/"+e,o=await this.axios.get(n),u={id:e,type:r,data:o.data};return this.preprocessEntity&&(u._raw=u.data,u.data=await this.preprocessEntity(o)),u},n.prototype.getResourceByRoute=function(t){return this.getResource({id:t.entity.uuid,type:t.jsonapi.resourceName})},n.prototype.getRoute=async function(t){var e="/router/translate-path?path="+t;return(await this.axios.get(e)).data},n.prototype.convertResourceToEntityBundle=function(t){var e=t.split("--");return{entityType:e[0],bundle:e[1]}};var o=require("vuex"),u={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}},o.mapState({entities:function(t){return t.druxtRouter.entities}})),methods:Object.assign({},o.mapActions({getEntity:"druxtRouter/getEntity"}))};return t.DruxtRouter=n,t.DruxtRouterComponent=i,t.DruxtRouterEntityMixin=u,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:{},route:{},routes:{}}},mutations:{addEntity:function(t,e){void 0!==e.id&&(t.entities[e.id]=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:{getEntityByRouter:async function(t,e){var r=t.commit;t.dispatch,t.state;try{var i=await this.$druxtRouter().get(e),n=i.entity;return r("addRoute",{path:e,route:i.route}),r("setRoute",e),r("addEntity",n),n}catch(t){if(void 0===t.response)throw t;return this.app.context.error({statusCode:t.response.status,message:t.response.statusText})}},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 n=await this.$druxtRouter().getResource(e);return r("addEntity",n),n}}},{preserveState:Boolean(e.state.druxtRouter)})},t}({},vuex,axios); | ||
var DruxtRouter=function(t,e,r,i){"use strict";r=r&&r.hasOwnProperty("default")?r.default:r,i=i&&i.hasOwnProperty("default")?i.default:i;var o={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}},e.mapState({entity:function(t){return t.druxtRouter.entities[t.druxtRouter.route.entity.uuid]},redirect:function(t){return t.druxtRouter.redirect},route:function(t){return t.druxtRouter.route}})),render:function(t){return t("div",{key:this.route.entity.uuid,props:{type:""+this.route.jsonapi.resourceName,uuid:this.route.entity.uuid}},JSON.stringify(this.route))},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)}},n=function(t,e){if(void 0===e&&(e={}),!t)throw new Error("The 'baseURL' parameter is required.");this.axios=r.create({baseURL:t}),this.setOptions(e)};n.prototype.setOptions=function(t){void 0===t&&(t={}),"function"==typeof t.preprocessEntity&&(this.preprocessEntity=t.preprocessEntity)},n.prototype.get=async function(t){var e=await this.getRoute(t),r=this.getRedirect(t,e);return{entity:await this.getResourceByRoute(e),redirect:r,route:e}},n.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 i(e.resolved);if(t!==r.pathname)return r.pathname}return!1},n.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},n.prototype.getResourceByRoute=function(t){return this.getResource({id:t.entity.uuid,type:t.jsonapi.resourceName})},n.prototype.getRoute=async function(t){var e="/router/translate-path?path="+t;return(await this.axios.get(e)).data};var u=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}},u.mapState({entities:function(t){return t.druxtRouter.entities}})),methods:Object.assign({},u.mapActions({getEntity:"druxtRouter/getEntity"}))};return t.DruxtRouter=n,t.DruxtRouterComponent=o,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.state;if(void 0!==i.routes[e]&&void 0!==i.entities[i.routes[e].entity.uuid]){var o=i.entities[i.routes[e].entity.uuid],n=this.$druxtRouter().getRedirect(e,o),u=i.entities[i.routes[e].entity.uuid];return r("setRoute",e),r("setRedirect",n),{entity:u,redirect:n,route:o}}try{var s=await this.$druxtRouter().get(e),a=s.entity,d=s.redirect,c=s.route;return r("addRoute",{path:e,redirect:d,route:c}),r("setRoute",e),r("setRedirect",d),r("addEntity",a),{entity:a,redirect:d,route:c}}catch(t){if(void 0===t.response)throw t;return this.app.context.error({statusCode:t.response.status,message:t.response.statusText})}},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}}},{preserveState:Boolean(e.state.druxtRouter)})},t}({},vuex,axios,Url); |
@@ -1,2 +0,2 @@ | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:true});function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}var vuex$1=require('vuex'),axios=_interopDefault(require('axios'));var DruxtRouterComponent = { | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:true});function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}var vuex$1=require('vuex'),axios=_interopDefault(require('axios')),Url=_interopDefault(require('url-parse'));var DruxtRouterComponent = { | ||
name: 'druxt-router', | ||
@@ -23,2 +23,3 @@ | ||
entity: function (state) { return state.druxtRouter.entities[state.druxtRouter.route.entity.uuid]; }, | ||
redirect: function (state) { return state.druxtRouter.redirect; }, | ||
route: function (state) { return state.druxtRouter.route; } | ||
@@ -31,3 +32,3 @@ })), | ||
props: { | ||
type: ((this.route.entity.type) + "--" + (this.route.entity.bundle)), | ||
type: ("" + (this.route.jsonapi.resourceName)), | ||
uuid: this.route.entity.uuid | ||
@@ -38,7 +39,13 @@ } | ||
fetch: function fetch (ref) { | ||
fetch: async function fetch (ref) { | ||
var store = ref.store; | ||
var redirect = ref.redirect; | ||
var route = ref.route; | ||
return store.dispatch('druxtRouter/getEntityByRouter', route.fullPath) | ||
var result = await store.dispatch('druxtRouter/get', route.fullPath); | ||
// Process redirect. | ||
if (result.redirect) { | ||
redirect(result.redirect); | ||
} | ||
} | ||
@@ -77,2 +84,4 @@ };var DruxtRouter = function DruxtRouter (baseURL, options) { | ||
var redirect = this.getRedirect(path, route); | ||
// Get entity from API. | ||
@@ -82,6 +91,36 @@ // @TODO - Add validation/error handling. | ||
return { entity: entity, route: route } | ||
return { entity: entity, redirect: redirect, route: route } | ||
}; | ||
/** | ||
* @param object route | ||
*/ | ||
DruxtRouter.prototype.getRedirect = function getRedirect (path, route) { | ||
// Redirect to route provided redirect. | ||
if (Array.isArray(route.redirect) && typeof route.redirect[0].to !== 'undefined') { | ||
return route.redirect[0].to | ||
} | ||
// Redirect to root if route is home path but path isn't root. | ||
if (route.isHomePath) { | ||
if (path !== '/') { | ||
return '/' | ||
} | ||
return false | ||
} | ||
// Redirect if path does not match resolved clean url path. | ||
if (typeof route.resolved === 'string') { | ||
var url = new Url(route.resolved); | ||
if (path !== url.pathname) { | ||
return url.pathname | ||
} | ||
} | ||
return false | ||
}; | ||
/** | ||
* Get a JSON:API resource by type and ID. | ||
@@ -96,7 +135,3 @@ * | ||
var ref$1 = this.convertResourceToEntityBundle(type); | ||
var entityType = ref$1.entityType; | ||
var bundle = ref$1.bundle; | ||
var url = "/api/" + entityType + "/" + bundle + "/" + id; | ||
var url = "/api/" + (type.replace('--', '/')) + "/" + id; | ||
var response = await this.axios.get(url); | ||
@@ -135,18 +170,2 @@ | ||
return response.data | ||
}; | ||
/** | ||
* Convert a JSON:API resource type to a Drupal Entity/Bundle. | ||
* | ||
* @todo Add support for JSON:API Extras. | ||
* | ||
* @param string type | ||
*/ | ||
DruxtRouter.prototype.convertResourceToEntityBundle = function convertResourceToEntityBundle (type) { | ||
var parts = type.split('--'); | ||
return { | ||
entityType: parts[0], | ||
bundle: parts[1] | ||
} | ||
};var vuex = require('vuex'); | ||
@@ -223,2 +242,3 @@ | ||
entities: {}, | ||
redirect: false, | ||
route: {}, | ||
@@ -237,2 +257,6 @@ routes: {} | ||
setRedirect: function setRedirect (state, redirect) { | ||
state.redirect = redirect; | ||
}, | ||
addRoute: function addRoute (state, ref) { | ||
@@ -261,18 +285,36 @@ var path = ref.path; | ||
actions: { | ||
getEntityByRouter: async function getEntityByRouter (ref, path) { | ||
get: async function get (ref, path) { | ||
var commit = ref.commit; | ||
var dispatch = ref.dispatch; | ||
var state = ref.state; | ||
// Return cached data, if present. | ||
// @TODO - Handle cache busting. | ||
if (typeof state.routes[path] !== 'undefined' && typeof state.entities[state.routes[path].entity.uuid] !== 'undefined') { | ||
var route = state.entities[state.routes[path].entity.uuid]; | ||
var redirect = this.$druxtRouter().getRedirect(path, route); | ||
var entity = state.entities[state.routes[path].entity.uuid]; | ||
commit('setRoute', path); | ||
commit('setRedirect', redirect); | ||
return { entity: entity, redirect: redirect, route: route } | ||
} | ||
// Get data from router. | ||
try { | ||
var ref$1 = await this.$druxtRouter().get(path); | ||
var entity = ref$1.entity; | ||
var route = ref$1.route; | ||
var entity$1 = ref$1.entity; | ||
var redirect$1 = ref$1.redirect; | ||
var route$1 = ref$1.route; | ||
commit('addRoute', { path: path, route: route }); | ||
commit('addRoute', { path: path, redirect: redirect$1, route: route$1 }); | ||
commit('setRoute', path); | ||
commit('addEntity', entity); | ||
commit('setRedirect', redirect$1); | ||
return entity | ||
commit('addEntity', entity$1); | ||
return { entity: entity$1, redirect: redirect$1, route: route$1 } | ||
// Handle errors. | ||
} catch (err) { | ||
@@ -279,0 +321,0 @@ if (typeof err.response === 'undefined') { |
{ | ||
"name": "druxt-router", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "Provides a Nuxt <-> Drupal router.", | ||
@@ -49,3 +49,4 @@ "files": [ | ||
"axios": "^0.19.0", | ||
"codecov": "^3.6.5" | ||
"codecov": "^3.6.5", | ||
"url-parse": "^1.4.7" | ||
}, | ||
@@ -69,2 +70,3 @@ "devDependencies": { | ||
"jest": "^24.9.0", | ||
"jest-mock-axios": "^3.2.0", | ||
"nodemon": "^2.0.1", | ||
@@ -71,0 +73,0 @@ "nuxt-edge": "^2.11.0-26244009.75bb0884", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27837
634
3
23
+ Addedurl-parse@^1.4.7
+ Addedquerystringify@2.2.0(transitive)
+ Addedrequires-port@1.0.0(transitive)
+ Addedurl-parse@1.5.10(transitive)