UNPKG

@sumsub/websdk

Version:

SumSub WebSdk

2 lines (1 loc) 12.5 kB
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).snsWebSdk=i()}(this,(function(){"use strict";var t=function(){return t=Object.assign||function(t){for(var i,e=1,o=arguments.length;e<o;e++)for(var n in i=arguments[e])Object.prototype.hasOwnProperty.call(i,n)&&(t[n]=i[n]);return t},t.apply(this,arguments)};function i(t){return t.endsWith("-v2")?2:1}var e=function(){function e(t,i,e,o){var n=this;this.iframe=null,this.iframeId=null,this.sessionId="",this.$container=null,this.initialized=!1,this.scrollPosition=-1,this.scrollElement=null,this.onMessage=function(t){n.onSdkMessage(t)},this.baseUrl=t||this.getCurrentBaseUrl("https://api.sumsub.com",i.accessToken),this.config=i,this.config.packageVersion=this.getVersion(),this.config.origin=location.origin,this.callbacks=e,this.options=o}return e.prototype.getVersion=function(){return"2.3.18"},e.prototype.getCurrentBaseUrl=function(t,i){var e=["_act-sbx-jwt-","_act-jwt-"].find((function(t){return i.startsWith(t)}));if(e)for(var o=0,n=i.replace(e,"").replace("-v2","").split(".").map((function(t){try{return atob(t)}catch(i){return t}})).map((function(t){try{return JSON.parse(t)}catch(i){return t}}));o<n.length;o++){var s=n[o];if(null==s?void 0:s.url)return s.url}return t},e.prototype.getIframeId=function(){if(2===this.config.version)try{return btoa(this.config.accessToken)}catch(t){return this.config.accessToken}return String(Math.floor(1e8*Math.random()))},e.prototype.launch=function(t){this.options.addViewportTag&&this.addViewportTag(),this.iframe=this.createIframe(t),this.sessionId="",this.iframeId="id_"+this.getIframeId(),this.iframe&&this.config?(this.registerEventListener(),this.iframe.src=this.getIframeSrc()):console.error("Idensic was not initialized since either provided element was not found in the DOM or invalid config")},e.prototype.addViewportTag=function(){var t=document.getElementsByName("viewport");if(!t||0===t.length){var i=document.createElement("meta");i.setAttribute("name","viewport"),i.setAttribute("content","width=device-width,user-scalable=yes"),document.head.appendChild(i)}},e.prototype.createIframe=function(t){var i="string"==typeof t?document.querySelector(t):t;if(!i)return console.error("Provide a valid selector for the iframe container"),null;this.$container=i;var e=document.createElement("iframe");for(e.width="100%",e.scrolling="no",e.allow="camera; microphone; geolocation; clipboard-write",e.setAttribute("frameborder","0");i.firstChild;)i.removeChild(i.firstChild);return i.appendChild(e),e},e.prototype.getIframeSrc=function(){var t="?_="+this.iframeId,i=2===this.config.version?"/websdk":"/idensic",e=this.baseUrl+i+"/websdk.html";return 2===this.config.version&&this.config.theme&&(t+="&theme="+this.config.theme),2===this.config.version&&this.config.customizationName&&(t+="&customizationName="+encodeURIComponent(this.config.customizationName)),2===this.config.version&&this.config.translationName&&(t+="&translationName="+encodeURIComponent(this.config.translationName)),e+t},e.prototype.registerEventListener=function(){window.addEventListener("message",this.onMessage)},e.prototype.onSdkMessage=function(i){var e;if(this.baseUrl===i.origin){var o=i.data;if(o.method&&~o.method.indexOf("idCheck")&&(!this.sessionId||this.sessionId===o.sessionId)&&(null===(e=this.iframe)||void 0===e?void 0:e.contentWindow)){if("idCheck.onReady"==o.method&&o.frameId===this.iframeId){this.sessionId=o.sessionId;var n={options:{adaptIframeHeight:this.options.adaptIframeHeight,enableScrollIntoView:this.options.enableScrollIntoView}};this.iframe.contentWindow.postMessage(t(t({method:"idCheck.init"},this.config),n),"*")}if("idCheck.onInitialized"==o.method&&(this.initialized=!0),"idCheck.onResize"==o.method&&this.options.adaptIframeHeight&&(this.iframe.style.height=o.height+"px"),"idCheck.scrollTo"==o.method&&this.options.adaptIframeHeight)this.scrollTo(o.top);else if("idCheck.getScrollPosition"!=o.method)if("idCheck.restoreScrollPosition"!=o.method){var s=o.method;delete o.method,delete o.frameId,delete o.sessionId,"idCheck.onError"===s&&"invalid-token"===o.code?this.callExpirationHandler():"idCheck.onError"===s&&"function"==typeof this.callbacks.onError?this.callbacks.onError(o):"function"==typeof this.callbacks.onMessage&&this.callbacks.onMessage(s,o)}else this.restoreScrollPosition();else this.getScrollPosition()}}},e.prototype.callExpirationHandler=function(){var t=this,i=this.callbacks.expirationHandler;i.legacy?i.handler((function(i){return t.updateAccessTokenOrReinitialize(i)})):i.handler().then((function(i){return t.updateAccessTokenOrReinitialize(i)}),(function(i){null!=i||(i="Failed to update access token"),i.message&&(i=i.message),"string"!=typeof i&&(i=String(i)),t.updateAccessToken(null,i)}))},e.prototype.updateAccessTokenOrReinitialize=function(t){var e=t?i(t):this.config.version;if(t&&this.iframe&&!this.initialized&&this.config.version!=e)return this.sessionId="",this.config.accessToken=t,this.config.version=e,this.baseUrl=this.getCurrentBaseUrl(this.baseUrl,t),this.iframeId="id_"+this.getIframeId(),void(this.iframe.src=this.getIframeSrc());this.updateAccessToken(t)},e.prototype.getScrolledElement=function(){for(var t,i=null===(t=this.iframe)||void 0===t?void 0:t.parentElement;0===(null==i?void 0:i.scrollTop)&&"BODY"!==(null==i?void 0:i.tagName);)i=null==i?void 0:i.parentElement;return i},e.prototype.getScrollPosition=function(){var t,i,e=this.getScrolledElement();this.scrollPosition=(null==e?void 0:e.scrollTop)||-1,this.scrollElement=this.scrollPosition>0&&e?e:null,null===(i=null===(t=this.iframe)||void 0===t?void 0:t.contentWindow)||void 0===i||i.postMessage({method:"idCheck.scrollPositionStored"},"*")},e.prototype.restoreScrollPosition=function(){this.scrollElement&&this.scrollPosition>=0&&(this.scrollElement.scrollTop=this.scrollPosition),this.scrollPosition=-1,this.scrollElement=null},e.prototype.scrollTo=function(t){var i;if(this.options.enableScrollIntoView){var e=this.getScrolledElement();if(0===(null==e?void 0:e.scrollTop)&&"BODY"===(null==e?void 0:e.tagName)){var o=(null===(i=this.iframe)||void 0===i?void 0:i.getBoundingClientRect().top)||0;window.scrollTo({top:o+t,behavior:"smooth"})}else null==e||e.scrollTo({top:t,behavior:"smooth"})}},e.prototype.updateAccessToken=function(t,i){var e,o;null===(o=null===(e=this.iframe)||void 0===e?void 0:e.contentWindow)||void 0===o||o.postMessage({method:"idCheck.updateAccessToken",accessToken:t,error:i},"*")},e.prototype.destroy=function(){for(window.removeEventListener("message",this.onMessage);this.$container&&this.$container.firstChild;)this.$container.removeChild(this.$container.firstChild);this.$container=null},e.prototype.navigateBack=function(){var t,i;null===(i=null===(t=this.iframe)||void 0===t?void 0:t.contentWindow)||void 0===i||i.postMessage({method:"idCheck.callNavigationBack"},"*")},e.prototype.singleStepNext=function(t){var i,e;null===(e=null===(i=this.iframe)||void 0===i?void 0:i.contentWindow)||void 0===e||e.postMessage({method:"idCheck.callSingleStepNext",step:t},"*")},e.prototype.setLanguage=function(t){var i,e;null===(e=null===(i=this.iframe)||void 0===i?void 0:i.contentWindow)||void 0===e||e.postMessage({method:"idCheck.callSetLanguage",language:t},"*")},e.prototype.setTheme=function(t){var i,e;null===(e=null===(i=this.iframe)||void 0===i?void 0:i.contentWindow)||void 0===e||e.postMessage({method:"idCheck.callSetTheme",theme:t},"*")},e}(),o=function(){function t(t,i){if(this.config=null,this.eventHandlers={},this.anyEventHandler=null,this.options={adaptIframeHeight:!0,addViewportTag:!0,enableScrollIntoView:!0},"string"!=typeof t)throw new Error("Access token must be a string");if("function"!=typeof i)throw new Error("updateAccessToken callback is required");this.accessToken=t,this.updateAccessToken=i}return t.prototype.onTestEnv=function(){return this},t.prototype.withBaseUrl=function(t){return this.baseUrl=t,this},t.prototype.withConf=function(t){return this.config=t,this},t.prototype.withOptions=function(t){var i;return t.hasOwnProperty("adaptIframeHeight")&&(this.options.adaptIframeHeight=t.adaptIframeHeight),t.hasOwnProperty("addViewportTag")&&(this.options.addViewportTag=t.addViewportTag),this.options.enableScrollIntoView=null===(i=t.enableScrollIntoView)||void 0===i||i,this},t.prototype.on=function(t,i){return this.eventHandlers[t]=i,this},t.prototype.onMessage=function(t){return this.anyEventHandler=t,this},t.prototype.onNavigationUiControlsStateChanged=function(t){return this.eventHandlers["idCheck.onNavigationUiControlsStateChanged"]=t,this},t.prototype.build=function(){var t,o,n,s,r,a,l,c,d,h,u,f,p,g,v=this,m=i(this.accessToken);return new e(this.baseUrl,{version:m,theme:null===(t=this.config)||void 0===t?void 0:t.theme,customizationName:null===(o=this.config)||void 0===o?void 0:o.customizationName,translationName:null===(n=this.config)||void 0===n?void 0:n.translationName,accessToken:this.accessToken,lang:null===(s=this.config)||void 0===s?void 0:s.lang,email:null===(r=this.config)||void 0===r?void 0:r.email,phone:null===(a=this.config)||void 0===a?void 0:a.phone,country:null===(l=this.config)||void 0===l?void 0:l.country,uiConf:null===(c=this.config)||void 0===c?void 0:c.uiConf,i18n:null===(d=this.config)||void 0===d?void 0:d.i18n,documentsByCountries:null===(h=this.config)||void 0===h?void 0:h.documentsByCountries,documentDefinitions:null===(u=this.config)||void 0===u?void 0:u.documentDefinitions,autoSelectDocumentDefinitions:null===(f=this.config)||void 0===f?void 0:f.autoSelectDocumentDefinitions,controlledNavigationBack:null===(p=this.config)||void 0===p?void 0:p.controlledNavigationBack,singleStep:null===(g=this.config)||void 0===g?void 0:g.singleStep},{expirationHandler:{legacy:!1,handler:this.updateAccessToken},onMessage:function(t,i){var e,o=v.eventHandlers[t];o?o(i):null===(e=v.anyEventHandler)||void 0===e||e.call(v,t,i)}},this.options)},t}(),n=function(){function t(t,i){this.debugEnabled=!1,this.options={adaptIframeHeight:!0,addViewportTag:!0,enableScrollIntoView:!0},this.config=null,this.accessToken=null,this.expirationHandler=null,this.baseUrl=t,this.flowName=i}return t.prototype.withAccessToken=function(t,i){if(this.accessToken=t,!i||"function"!=typeof i)throw new Error('Invalid parameter, "expirationHandler" must be a function');return this.expirationHandler=i,this},t.prototype.debug=function(t){return this.debugEnabled=t,this},t.prototype.withOptions=function(t){var i;return t.hasOwnProperty("adaptIframeHeight")&&(this.options.adaptIframeHeight=t.adaptIframeHeight),t.hasOwnProperty("addViewportTag")&&(this.options.addViewportTag=t.addViewportTag),this.options.enableScrollIntoView=null===(i=t.enableScrollIntoView)||void 0===i||i,this},t.prototype.withConf=function(t){return this.config=t,this},t.prototype.build=function(){var t,o,n,s,r,a,l,c,d,h,u,f,p,g,v;if(!this.accessToken||!this.expirationHandler)throw new Error("Configure access token end the expiration handler before");var m=i(this.accessToken);return new e(this.baseUrl,{version:m,theme:null===(t=this.config)||void 0===t?void 0:t.theme,customizationName:null===(o=this.config)||void 0===o?void 0:o.customizationName,translationName:null===(n=this.config)||void 0===n?void 0:n.translationName,accessToken:this.accessToken,flowName:this.flowName,lang:null===(s=this.config)||void 0===s?void 0:s.lang,email:null===(r=this.config)||void 0===r?void 0:r.email,phone:null===(a=this.config)||void 0===a?void 0:a.phone,country:null===(l=this.config)||void 0===l?void 0:l.country,uiConf:null===(c=this.config)||void 0===c?void 0:c.uiConf,i18n:null===(d=this.config)||void 0===d?void 0:d.i18n,documentsByCountries:null===(h=this.config)||void 0===h?void 0:h.documentsByCountries,documentDefinitions:null===(u=this.config)||void 0===u?void 0:u.documentDefinitions,autoSelectDocumentDefinitions:null===(f=this.config)||void 0===f?void 0:f.autoSelectDocumentDefinitions,singleStep:null===(p=this.config)||void 0===p?void 0:p.singleStep},{expirationHandler:{legacy:!0,handler:this.expirationHandler},onMessage:null===(g=this.config)||void 0===g?void 0:g.onMessage,onError:null===(v=this.config)||void 0===v?void 0:v.onError},{adaptIframeHeight:this.options.adaptIframeHeight,addViewportTag:this.options.addViewportTag,enableScrollIntoView:this.options.enableScrollIntoView,debug:this.debugEnabled})},t}();return{Builder:function(t,i){return new n(t,i)},init:function(t,i){return new o(t,i)}}}));