UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

34 lines 3.53 kB
function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}import pathMatch from'path-match';/** * Class to maintain the routes that should be protected by authentication. */var AuthRoutes=/*#__PURE__*/function(){/** * Constructor */function AuthRoutes(){_classCallCheck(this,AuthRoutes);this.routes=new Map();this.matcher=pathMatch({sensitive:false,strict:false,end:true});}/** * Finds a particular protected route. * @param {string} pattern The pattern to find. * @return {string|null} */return _createClass(AuthRoutes,[{key:"get",value:function get(pattern){return this.routes.get(pattern)||null;}/** * Returns all protected routes. * @return {Map} */},{key:"getAll",value:function getAll(){return this.routes;}/** * Sets a new route as protected. * @param {string} civilian The route to protect. * @param {string} bouncer The protector route. */},{key:"set",value:function set(civilian,bouncer){if(!civilian||!bouncer){return;}this.routes.set(civilian,bouncer);}/** * Check if the given pathname is a protector route. * @param {string} location The location to check. * @return {boolean} */},{key:"isProtector",value:function isProtector(location){return Array.from(this.routes.values()).includes(location);}/** * Returns the protector for a passed location. * @param {string} location The location to check. * @return {string|null} */},{key:"getProtector",value:function getProtector(location){var _this=this;/** * Try to make a direct match with the location. * If we get lucky then we don't have to iterate over the protected patterns. */var protector=this.get(location);/** * If we didn't find a direct match then we need to match * the given location against the protected patters. */if(!protector){// Get the protected patterns as an array. var patterns=Array.from(this.routes.keys());var _location$split3=location.split('?'),_location$split4=_slicedToArray(_location$split3,1),locationWithoutParams=_location$split4[0];// Loop over the patterns until a match is found. patterns.some(function(pattern){// Check for a match. var match=_this.matcher(pattern)(locationWithoutParams);// Match found, set the protector. if(match){protector=_this.routes.get(pattern);}return match;});}return protector;}}]);}();export default new AuthRoutes();