UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

46 lines 5.43 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';import queryString from'query-string';/** * The Redirects class. */var Redirects=/*#__PURE__*/function(){/** * The constructor. */function Redirects(){_classCallCheck(this,Redirects);this.redirects=new Map();this.matcher=pathMatch({sensitive:false,strict:false,end:true});}/** * Returns a specified element from the internal "redirects" Map object. * @param {string} pathname The pathname to lookup. * @private * @returns {string|Function|Promise|null} */return _createClass(Redirects,[{key:"get",value:function get(pathname){return this.redirects.get(pathname)||null;}/** * Returns the redirect for a passed pathname. * @param {string} pathname The pathname to check. * @return {string|Function|Promise|null} */},{key:"getRedirect",value:function getRedirect(pathname){var _this=this;/** * Try to make a direct match with the pathname. * If we get lucky then we don't have to iterate over the protected patterns. */var redirect=this.get(pathname);/** * If we didn't find a direct match then we need to match * the given pathname against the protected patters. */if(!redirect){// Get the protected patterns as an array. var patterns=Array.from(this.redirects.keys());var _pathname$split3=pathname.split('?'),_pathname$split4=_slicedToArray(_pathname$split3,1),withoutParams=_pathname$split4[0];// Loop over the patterns until a match is found. var patternMatch=patterns.find(function(pattern){return!!_this.matcher(pattern)(withoutParams);});// Match found, set the redirect. if(patternMatch){redirect=this.redirects.get(patternMatch);}}return redirect;}/** * @typedef RedirectExtendedData * @property {string} matcher The value passed as "from" to set() * @property {string|Function|Promise} handler The value passed as "to" to set() * @property {Object} pathParams Decoded params from the pathname - defined within the matcher * @property {Object} queryParams Decoded query params from the pathname */ /** * Unlike "getRedirect" which only returns a matching handler for a passed pathname, this method * returns an object that contains some extended data. * * @param {string} pathname The pathname to check. * @returns {RedirectExtendedData} */},{key:"getRedirectExtended",value:function getRedirectExtended(pathname){var _queryString$parseUrl=queryString.parseUrl(pathname),url=_queryString$parseUrl.url,query=_queryString$parseUrl.query;// At the fist check if there is a redirect for the pathname var redirect=this.getRedirect(url);if(!redirect){return null;}// Retrieve the matching pattern for the redirect from the redirects collection var _ref=Array.from(this.redirects.entries()).find(function(_ref3){var _ref4=_slicedToArray(_ref3,2),handler=_ref4[1];return handler===redirect;})||[],_ref2=_slicedToArray(_ref,1),patternMatch=_ref2[0];var result={handler:redirect,queryParams:JSON.parse(JSON.stringify(query))};if(patternMatch){// decode params from route patterns (e.g. /item/:productCode) var matcherResult=this.matcher(patternMatch)(url);result.matcher=patternMatch;result.pathParams=matcherResult;}return result;}/** * Adds a redirect handler to the collection. * @param {string} from The link to redirect from. Route patterns are also supported. * @param {string|Function|Promise} to redirect / handle to create a dynamic link. * @param {boolean} force Whether or not to forcefully set the redirect. */},{key:"set",value:function set(){var from=arguments.length>0&&arguments[0]!==undefined?arguments[0]:null;var to=arguments.length>1&&arguments[1]!==undefined?arguments[1]:null;var force=arguments.length>2&&arguments[2]!==undefined?arguments[2]:false;if(!from||!to){return;}if(!force&&this.redirects.has(from)){return;}this.redirects.set(from,to);}/* eslint-disable extra-rules/potential-point-free */ /** * Removes a specified element from the internal "redirects" Map object. * @param {string} pathname The pathname to remove. */},{key:"unset",value:function unset(pathname){this.redirects["delete"](pathname);}/* eslint-enable extra-rules/potential-point-free */}]);}();export default new Redirects();