UNPKG

@quienxmi/sdk-iframe-project

Version:

SDK to control an iframe for requesting quotes via Qxm.

3 lines (2 loc) 4.18 kB
(function(n,o){typeof exports=="object"&&typeof module<"u"?module.exports=o():typeof define=="function"&&define.amd?define(o):(n=typeof globalThis<"u"?globalThis:n||self,n.QxmIframeProject=o())})(this,function(){"use strict";var b=Object.defineProperty;var v=(n,o,c)=>o in n?b(n,o,{enumerable:!0,configurable:!0,writable:!0,value:c}):n[o]=c;var l=(n,o,c)=>v(n,typeof o!="symbol"?o+"":o,c);const n={SDK_CREATE:"The SDK could not be found.",IFRAME_NOT_FOUND:"The specified iframe could not be found.",EXPIRED_TOKEN:"Your token has expired. Please generate a new one.",DOM_NOT_IFRAME:"Cannot build the iframe as the DOM element is not an iframe.",INVALID_TOKEN:"The token is invalid or has expired. Please generate a new token.",INVALID_ORIGIN:"The origin of the source is not supported.",INVALID_DOMAIN:"The domain of the source is not supported.",ERROR_LOADING_IFRAME:"Failed to load the iframe correctly."};function o(a){try{return new URL(a).origin===window.location.origin}catch{return!1}}const c=[".sandboxqxm.com",".quienpormi.com",".quienxmi.com",".qxm.com."];function u(a){try{const e=new URL(a);if(e.hostname==="localhost")return e.port==="8000";if(e.protocol!=="https:")return!1;const r=e.hostname;return c.some(t=>t.endsWith(".")?r.slice(0,-2).endsWith(t):r.endsWith(t))}catch{return!1}}const p=["iss","iat","exp","data"];function I(a){const e=Object.keys(a);return p.every(r=>e.includes(r))}function _(a){try{const r=a.split(".")[1].replace(/-/g,"+").replace(/_/g,"/"),t=JSON.parse(decodeURIComponent(atob(r).split("").map(function(i){return"%"+("00"+i.charCodeAt(0).toString(16)).slice(-2)}).join(""))),s=Math.floor(Date.now()/1e3);return t.exp>s&&I(t)?t:null}catch{return null}}const E="1.0.7",d={all:[],resize:[],event:[],error:[]};class f{constructor(e,r){l(this,"_domIframe");l(this,"_observers",d);l(this,"_checkExp");l(this,"_logs",!1);try{const{scrolling:t,resize:s,logs:i}=r??{};if(typeof e=="string"?this._domIframe=document.querySelector(e):this._domIframe=e,i===!0&&(this._logs=!0),this._logs&&console.info("[QxmIframe]: Version "+E),!this._domIframe){this.pushError("IFRAME_NOT_FOUND");return}if(!(this._domIframe instanceof HTMLIFrameElement)){this.pushError("DOM_NOT_IFRAME");return}this._domIframe.setAttribute("frameborder","0"),t||this._domIframe.setAttribute("scrolling","no"),this.createListener(),(s??!0)&&this.subscribe("resize",this.resize)}catch(t){this.pushError("SDK_CREATE",null,t),this.destroy()}}subscribe(e="all",r){this._observers[e].push(r)}subscribeError(e){this.subscribe("error",e)}subscribeEvent(e){this.subscribe("event",e)}async setToken(e){e=e.trim();const r=_(e);return clearInterval(this._checkExp),r?r.aud&&!o(r.aud)?(this.pushError("INVALID_ORIGIN"),null):u(r.iss)?(this._checkExp=setInterval(()=>{const t=Math.floor(Date.now()/1e3);r.exp<t&&(clearInterval(this._checkExp),this.pushError("EXPIRED_TOKEN"))},1e3),await this.setSrcIframe(r.iss,e)?r:null):(this.pushError("INVALID_DOMAIN"),null):(this.pushError("INVALID_TOKEN"),null)}destroy(){this._domIframe=void 0,this._observers=d,clearInterval(this._checkExp)}setSrcIframe(e,r){return new Promise(t=>{const s=this._domIframe,i=()=>{m(),t(!0)},h=()=>{this.pushError("ERROR_LOADING_IFRAME"),m(),t(!1)},m=()=>{s.removeEventListener("load",i),s.removeEventListener("error",h)};try{s.src=e+"/api/iframe/project/create?token="+r,s.addEventListener("load",i),s.addEventListener("error",h)}catch{h()}})}createListener(){window.addEventListener("message",e=>{const{origin:r,data:t}=e;if(u(r)){this._logs&&console.log("[QxmIframe]:",t);let s=t.type??"all";if(this._observers[s]||(s="all"),s==="error"){this.pushError(t.code,t.message);return}this._observers[s].forEach(i=>i({_domIframe:this._domIframe,data:t}))}})}pushError(e,r=null,t=null){r||(r=n[e]??e),this._logs&&console.error("[QxmIframe]:",r,t),this._observers.error.forEach(s=>s({_domIframe:this._domIframe,code:e,message:r,error:t}))}resize(e){const{_domIframe:r,data:t}=e,s=window.getComputedStyle(r),i=parseInt(s.paddingTop)+parseInt(s.paddingBottom);r.style.setProperty("height",`${t.height+i}px`,"important")}}return typeof window<"u"&&(window.QxmIframeProject=f),f}); //# sourceMappingURL=qxm-iframe-project.umd.js.map