UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

17 lines 5.17 kB
function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}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;}import{SCANNER_SCOPE_DEFAULT,SCANNER_TYPE_BARCODE}from'@shopgate/pwa-core/constants/Scanner';import{getCategoryRoute}from'@shopgate/pwa-common-commerce/category';import{getProductRoute}from'@shopgate/pwa-common-commerce/product';import{getSearchRoute}from'@shopgate/pwa-common-commerce/search';import{QR_CODE_TYPE_HOMEPAGE,QR_CODE_TYPE_PRODUCT,QR_CODE_TYPE_PRODUCT_WITH_COUPON,QR_CODE_TYPE_COUPON,QR_CODE_TYPE_CATEGORY,QR_CODE_TYPE_SEARCH,QR_CODE_TYPE_PAGE,QR_CODE_HOST_2DS,SCANNER_PATH}from"../constants";/** * Set of parsers of different types */var typeParsers=_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({},QR_CODE_TYPE_HOMEPAGE,function(){return{link:'/'};}),QR_CODE_TYPE_PRODUCT,function(url){var _url$pathname$split=url.pathname.split('/'),_url$pathname$split2=_slicedToArray(_url$pathname$split,4),paramTwo=_url$pathname$split2[3];var decodedParamTwo=decodeURIComponent(decodeURIComponent(paramTwo));return{link:getProductRoute(decodedParamTwo),data:{productId:decodedParamTwo}};}),QR_CODE_TYPE_PRODUCT_WITH_COUPON,function(url){var _url$pathname$split3=url.pathname.split('/'),_url$pathname$split4=_slicedToArray(_url$pathname$split3,5),paramTwo=_url$pathname$split4[3],paramThree=_url$pathname$split4[4];var decodedParamTwo=decodeURIComponent(decodeURIComponent(paramTwo));var decodedParamThree=decodeURIComponent(decodeURIComponent(paramThree));return{link:"/cart_add_product/".concat(encodeURIComponent(decodedParamTwo),"/").concat(encodeURIComponent(decodedParamThree)),data:{productId:decodedParamTwo,couponCode:decodedParamThree}};}),QR_CODE_TYPE_COUPON,function(url){var _url$pathname$split5=url.pathname.split('/'),_url$pathname$split6=_slicedToArray(_url$pathname$split5,3),paramOne=_url$pathname$split6[2];return{link:"/cart_add_coupon/".concat(paramOne),data:{couponCode:paramOne}};}),QR_CODE_TYPE_CATEGORY,function(url){var _url$pathname$split7=url.pathname.split('/'),_url$pathname$split8=_slicedToArray(_url$pathname$split7,4),paramTwo=_url$pathname$split8[3];var decodedParamTwo=decodeURIComponent(decodeURIComponent(paramTwo));return{link:getCategoryRoute(decodedParamTwo),data:{categoryId:decodedParamTwo}};}),QR_CODE_TYPE_SEARCH,function(url){var _url$pathname$split9=url.pathname.split('/'),_url$pathname$split10=_slicedToArray(_url$pathname$split9,4),paramTwo=_url$pathname$split10[3];var decodedParamTwo=decodeURIComponent(decodeURIComponent(paramTwo));return{link:getSearchRoute(decodedParamTwo),data:{searchPhrase:decodedParamTwo}};}),QR_CODE_TYPE_PAGE,function(url){var _url$pathname$split11=url.pathname.split('/'),_url$pathname$split12=_slicedToArray(_url$pathname$split11,4),paramTwo=_url$pathname$split12[3];var decodedParamTwo=decodeURIComponent(decodeURIComponent(paramTwo));return{link:"/page/".concat(decodedParamTwo),data:{pageId:decodedParamTwo}};});/** * Check if links is 2ds. * @param {string} code The string from the QR Code * @returns {boolean} */export var is2dsQrCode=function is2dsQrCode(code){try{return new URL(code).host===QR_CODE_HOST_2DS;}catch(e){return false;}};/** * Parses 2ds QR Codes. * Generates a deeplink which can be used to open pages. * @param {string} code The string from the QR Code. * @returns {?{type, link, data}} The parsed type, link, data */export var parse2dsQrCode=function parse2dsQrCode(code){if(!is2dsQrCode(code)){return null;}var url=new URL(code);var _url$pathname$split13=url.pathname.split('/'),_url$pathname$split14=_slicedToArray(_url$pathname$split13,2),type=_url$pathname$split14[1];var link='/';var data={};var typeParser=typeParsers[type];if(typeParser){var _typeParser=typeParser(url);link=_typeParser.link;var _typeParser$data=_typeParser.data;data=_typeParser$data===void 0?{}:_typeParser$data;}return{type:type,link:link,data:data};};/** * Create a link to the scanner route. * @param {string} [scope=SCANNER_SCOPE_DEFAULT] The scanner scope to use for the scanner page * @param {string} [type=SCANNER_TYPE_BARCODE] The scanner type to use on the scanner page * @returns {string} */export var getScannerRoute=function getScannerRoute(){var scope=arguments.length>0&&arguments[0]!==undefined?arguments[0]:SCANNER_SCOPE_DEFAULT;var type=arguments.length>1&&arguments[1]!==undefined?arguments[1]:SCANNER_TYPE_BARCODE;return"".concat(SCANNER_PATH,"?scope=").concat(scope,"&type=").concat(type);};