@saasquatch/microsite-router 1.0.0-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ SaaSquatch microsite client-side router and hierarchical layout renderer.
package/dist/bundle.js ADDED
@@ -0,0 +1 @@
1
+ function e(e){this.message=e}e.prototype=new Error,e.prototype.name="InvalidCharacterError";var t="undefined"!=typeof window&&window.atob&&window.atob.bind(window)||function(t){var n=String(t).replace(/=+$/,"");if(n.length%4==1)throw new e("'atob' failed: The string to be decoded is not correctly encoded.");for(var o,r,a=0,i=0,s="";r=n.charAt(i++);~r&&(o=a%4?64*o+r:r,a++%4)?s+=String.fromCharCode(255&o>>(-2*a&6)):0)r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(r);return s};function n(e){var n=e.replace(/-/g,"+").replace(/_/g,"/");switch(n.length%4){case 0:break;case 2:n+="==";break;case 3:n+="=";break;default:throw"Illegal base64url string!"}try{return function(e){return decodeURIComponent(t(e).replace(/(.)/g,(function(e,t){var n=t.charCodeAt(0).toString(16).toUpperCase();return n.length<2&&(n="0"+n),"%"+n})))}(n)}catch(e){return t(n)}}function o(e){this.message=e}o.prototype=new Error,o.prototype.name="InvalidTokenError";function r(e){if(e)try{const t=function(e,t){if("string"!=typeof e)throw new o("Invalid token specified");var r=!0===(t=t||{}).header?0:1;try{return JSON.parse(n(e.split(".")[r]))}catch(e){throw new o("Invalid token specified: "+e.message)}}(e),r=t.exp;let a,i;if(function(e){return e.user&&e.user.id&&e.user.accountId}(t)&&(i=t.user.accountId,a=t.user.id),!a||!i)return;if(r&&Date.now()>=1e3*r)return;return{id:a,accountId:i,jwt:e}}catch(e){return}}const a=localStorage.getItem("debug");function i(e,...t){a&&console.debug(`sq:microsite:${e}`,...t)}const s={};let c=[];async function d(e){var t;const n=await async function(e,t){if(s[e])return i("fetchPage",`avoiding GraphQL call in lieu of cache for [${e}]`),s[e];try{const n=await fetch(`${window.SquatchPortal.appDomain}/api/v1/${window.SquatchPortal.tenantAlias}/graphql`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({operationName:"RenderMicrositePage",query:"\n query RenderMicrositePage($urlPath: String!, $locale: RSLocale) {\n renderMicrositePage(urlPath: $urlPath, locale: $locale) {\n micrositePageConfig {\n urlPath\n values\n allowedUsers\n disallowedUrlPath\n }\n micrositeLayoutConfigs {\n key\n values\n }\n }\n }\n",variables:{urlPath:e,locale:t}})});if(!n.ok)throw new Error("something bad happened");const o=await n.json();if(o.errors)throw new Error(JSON.stringify(o.errors,null,2));const r=o.data,a={page:r.renderMicrositePage.micrositePageConfig,layouts:r.renderMicrositePage.micrositeLayoutConfigs};return s[e]=a,a}catch(e){throw document.body.innerHTML=`\n <div style="padding: 12px;">\n <h3>Sorry, something went wrong</h3>\n <p>Please contact <a href="mailto:success@saasquatch.com">success@saasquatch.com</a>.</p>\n <p style="background-color: pink; padding: 12px; font-family: monospace; font-size: 12px;">${e.message}</p>\n </div>\n `,e}}(e),o=function(){const e=localStorage.getItem("sq:user-identity");if(e)try{const t=JSON.parse(e),n=r(t.jwt);return n?Object.assign(Object.assign({},t),n):void 0}catch(e){return}}();i("router",`routing to ${e}"`);const a=n.page.allowedUsers,d=n.page.disallowedUrlPath,l=o?(null===(t=o.managedIdentity)||void 0===t?void 0:t.emailVerified)?"VERIFIED":"UNVERIFIED":"PUBLIC";i("router",`pathname[${e}] userType[${l}] allowedUsers[${a}] disallowedUrlPath:[${d}]`),l!=a&&d?(i("router",`pathname[${e}] user is ${l}, but expected ${a}, redirecting to ${d}`),window.history.pushState(void 0,"",d)):function(e){const t=[];let n=document.body;c.length>0&&(c[c.length-1].element.innerHTML=""),[...e.layouts].reverse().forEach(((e,o)=>{var r;if((null===(r=c[o])||void 0===r?void 0:r.key)===e.key)i("render",`reusing element for layout [${e.key}]`),n=c[o].element,t.push({key:e.key,element:n});else{c.splice(o).forEach((e=>{i("render",`removing element for layout [${e.key}]`),e.element.remove()}));const r=document.createElement("div");r.style.display="contents",r.id=e.key,r.attachShadow({mode:"open"}),r.shadowRoot.innerHTML=e.values.html,n.appendChild(r),n=r,t.push({key:e.key,element:r}),i("render",`created element for layout [${e.key}]`)}})),n.innerHTML=e.page.values.html,document.title=e.page.values.title,c=t}(n)}window.addEventListener("load",(function(){function e(e){const t=window.history[e];return function(...n){const o=t.apply(window.history,n),r=new Event(e);return window.dispatchEvent(r),o}}history.pushState=e("pushState"),history.replaceState=e("replaceState"),window.addEventListener("popstate",(()=>d(window.location.pathname))),window.addEventListener("pushState",(()=>d(window.location.pathname))),window.addEventListener("replaceState",(()=>d(window.location.pathname))),window.history.replaceState(void 0,"","")}));
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@saasquatch/microsite-router",
3
+ "version": "1.0.0-0",
4
+ "description": "SaaSquatch microsite client-side router and hierarchical layout renderer.",
5
+ "main": "dist/bundle.js",
6
+ "module": "dist/bundle.js",
7
+ "type": "module",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "dev": "rollup --config rollup.config.ts --configPlugin typescript --watch",
13
+ "build": "rollup --config rollup.config.ts --configPlugin typescript",
14
+ "test": "echo \"Error: no test specified\" && exit 1"
15
+ },
16
+ "author": "Johan Venter <johan@saasquatch.com>",
17
+ "license": "ISC",
18
+ "dependencies": {
19
+ "jwt-decode": "^3.1.2",
20
+ "typescript": "^4.7.2"
21
+ },
22
+ "devDependencies": {
23
+ "@rollup/plugin-node-resolve": "^13.3.0",
24
+ "@rollup/plugin-typescript": "^8.3.2",
25
+ "rollup": "^2.75.4",
26
+ "rollup-plugin-serve": "^1.1.0",
27
+ "rollup-plugin-terser": "^7.0.2"
28
+ }
29
+ }