@sumsub/websdk-react
Version:
SumSub WebSdk React
2 lines (1 loc) • 14.5 kB
JavaScript
"use strict";function t(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach(function(i){if("default"!==i){var o=Object.getOwnPropertyDescriptor(t,i);Object.defineProperty(e,i,o.get?o:{enumerable:!0,get:function(){return t[i]}})}}),e.default=t,Object.freeze(e)}var e=t(require("react")),i=function(t,e){return i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])},i(t,e)};var o=function(){return o=Object.assign||function(t){for(var e,i=1,o=arguments.length;i<o;i++)for(var n in e=arguments[i])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t},o.apply(this,arguments)};function n(t){return t.endsWith("-v2")?2:1}var s=function(){function t(t,e,i,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",e.accessToken),this.config=e,this.config.packageVersion=this.getVersion(),this.config.origin=location.origin,this.callbacks=i,this.options=o}return t.prototype.getVersion=function(){return"2.4.1"},t.prototype.getCurrentBaseUrl=function(t,e){var i=["_act-sbx-jwt-","_act-jwt-"].find(function(t){return e.startsWith(t)});if(i)for(var o=0,n=e.replace(i,"").replace("-v2","").split(".").map(function(t){try{return atob(t)}catch(e){return t}}).map(function(t){try{return JSON.parse(t)}catch(e){return t}});o<n.length;o++){var s=n[o];if(null==s?void 0:s.url)return s.url}return t},t.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()))},t.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")},t.prototype.addViewportTag=function(){var t=document.getElementsByName("viewport");if(!t||0===t.length){var e=document.createElement("meta");e.setAttribute("name","viewport"),e.setAttribute("content","width=device-width,user-scalable=yes"),document.head.appendChild(e)}},t.prototype.createIframe=function(t){var e="string"==typeof t?document.querySelector(t):t;if(!e)return console.error("Provide a valid selector for the iframe container"),null;this.$container=e;var i=document.createElement("iframe");for(i.width="100%",i.scrolling="no",i.allow="camera; microphone; geolocation; clipboard-write",i.setAttribute("frameborder","0");e.firstChild;)e.removeChild(e.firstChild);return e.appendChild(i),i},t.prototype.getIframeSrc=function(){var t="?_="+this.iframeId,e=2===this.config.version?"/websdk":"/idensic",i=this.baseUrl+e+"/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)),i+t},t.prototype.registerEventListener=function(){window.addEventListener("message",this.onMessage)},t.prototype.onSdkMessage=function(t){var e;if(this.baseUrl===t.origin){var i=t.data;if(i.method&&~i.method.indexOf("idCheck")&&(!this.sessionId||this.sessionId===i.sessionId)&&(null===(e=this.iframe)||void 0===e?void 0:e.contentWindow)){if("idCheck.onReady"==i.method&&i.frameId===this.iframeId){this.sessionId=i.sessionId;var n={options:{adaptIframeHeight:this.options.adaptIframeHeight,enableScrollIntoView:this.options.enableScrollIntoView}};this.iframe.contentWindow.postMessage(o(o({method:"idCheck.init"},this.config),n),"*")}if("idCheck.onInitialized"==i.method&&(this.initialized=!0),"idCheck.onResize"==i.method&&this.options.adaptIframeHeight&&(this.iframe.style.height=i.height+"px"),"idCheck.scrollTo"==i.method&&this.options.adaptIframeHeight)this.scrollTo(i.top);else if("idCheck.getScrollPosition"!=i.method)if("idCheck.restoreScrollPosition"!=i.method){var s=i.method;delete i.method,delete i.frameId,delete i.sessionId,"idCheck.onError"===s&&"invalid-token"===i.code?this.callExpirationHandler():"idCheck.onError"===s&&"function"==typeof this.callbacks.onError?this.callbacks.onError(i):"function"==typeof this.callbacks.onMessage&&this.callbacks.onMessage(s,i)}else this.restoreScrollPosition();else this.getScrollPosition()}}},t.prototype.callExpirationHandler=function(){var t=this,e=this.callbacks.expirationHandler;e.legacy?e.handler(function(e){return t.updateAccessTokenOrReinitialize(e)}):e.handler().then(function(e){return t.updateAccessTokenOrReinitialize(e)},function(e){null!=e||(e="Failed to update access token"),e.message&&(e=e.message),"string"!=typeof e&&(e=String(e)),t.updateAccessToken(null,e)})},t.prototype.updateAccessTokenOrReinitialize=function(t){var e=t?n(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)},t.prototype.getScrolledElement=function(){for(var t,e=null===(t=this.iframe)||void 0===t?void 0:t.parentElement;0===(null==e?void 0:e.scrollTop)&&"BODY"!==(null==e?void 0:e.tagName);)e=null==e?void 0:e.parentElement;return e},t.prototype.getScrollPosition=function(){var t,e,i=this.getScrolledElement();this.scrollPosition=(null==i?void 0:i.scrollTop)||-1,this.scrollElement=this.scrollPosition>0&&i?i:null,null===(e=null===(t=this.iframe)||void 0===t?void 0:t.contentWindow)||void 0===e||e.postMessage({method:"idCheck.scrollPositionStored"},"*")},t.prototype.restoreScrollPosition=function(){this.scrollElement&&this.scrollPosition>=0&&(this.scrollElement.scrollTop=this.scrollPosition),this.scrollPosition=-1,this.scrollElement=null},t.prototype.scrollTo=function(t){var e;if(this.options.enableScrollIntoView){var i=this.getScrolledElement();if(0===(null==i?void 0:i.scrollTop)&&"BODY"===(null==i?void 0:i.tagName)){var o=(null===(e=this.iframe)||void 0===e?void 0:e.getBoundingClientRect().top)||0;window.scrollTo({top:o+t,behavior:"smooth"})}else null==i||i.scrollTo({top:t,behavior:"smooth"})}},t.prototype.updateAccessToken=function(t,e){var i,o;null===(o=null===(i=this.iframe)||void 0===i?void 0:i.contentWindow)||void 0===o||o.postMessage({method:"idCheck.updateAccessToken",accessToken:t,error:e},"*")},t.prototype.destroy=function(){for(window.removeEventListener("message",this.onMessage);this.$container&&this.$container.firstChild;)this.$container.removeChild(this.$container.firstChild);this.$container=null},t.prototype.navigateBack=function(){var t,e;null===(e=null===(t=this.iframe)||void 0===t?void 0:t.contentWindow)||void 0===e||e.postMessage({method:"idCheck.callNavigationBack"},"*")},t.prototype.singleStepNext=function(t){var e,i;null===(i=null===(e=this.iframe)||void 0===e?void 0:e.contentWindow)||void 0===i||i.postMessage({method:"idCheck.callSingleStepNext",step:t},"*")},t.prototype.setLanguage=function(t){var e,i;null===(i=null===(e=this.iframe)||void 0===e?void 0:e.contentWindow)||void 0===i||i.postMessage({method:"idCheck.callSetLanguage",language:t},"*")},t.prototype.setTheme=function(t){var e,i;null===(i=null===(e=this.iframe)||void 0===e?void 0:e.contentWindow)||void 0===i||i.postMessage({method:"idCheck.callSetTheme",theme:t},"*")},t}(),r=function(){function t(t,e){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 e)throw new Error("updateAccessToken callback is required");this.accessToken=t,this.updateAccessToken=e}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 e;return t.hasOwnProperty("adaptIframeHeight")&&(this.options.adaptIframeHeight=t.adaptIframeHeight),t.hasOwnProperty("addViewportTag")&&(this.options.addViewportTag=t.addViewportTag),this.options.enableScrollIntoView=null===(e=t.enableScrollIntoView)||void 0===e||e,this},t.prototype.on=function(t,e){return this.eventHandlers[t]=e,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,e,i,o,r,a,l,c,d,h,u,f,p,v,g,m=this,y=n(this.accessToken);return new s(this.baseUrl,{version:y,theme:null===(t=this.config)||void 0===t?void 0:t.theme,customizationName:null===(e=this.config)||void 0===e?void 0:e.customizationName,translationName:null===(i=this.config)||void 0===i?void 0:i.translationName,accessToken:this.accessToken,lang:null===(o=this.config)||void 0===o?void 0:o.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===(v=this.config)||void 0===v?void 0:v.singleStep,preferredCameras:null===(g=this.config)||void 0===g?void 0:g.preferredCameras},{expirationHandler:{legacy:!1,handler:this.updateAccessToken},onMessage:function(t,e){var i,o=m.eventHandlers[t];o?o(e):null===(i=m.anyEventHandler)||void 0===i||i.call(m,t,e)}},this.options)},t}(),a=(function(){function t(t,e){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=e}t.prototype.withAccessToken=function(t,e){if(this.accessToken=t,!e||"function"!=typeof e)throw new Error('Invalid parameter, "expirationHandler" must be a function');return this.expirationHandler=e,this},t.prototype.debug=function(t){return this.debugEnabled=t,this},t.prototype.withOptions=function(t){var e;return t.hasOwnProperty("adaptIframeHeight")&&(this.options.adaptIframeHeight=t.adaptIframeHeight),t.hasOwnProperty("addViewportTag")&&(this.options.addViewportTag=t.addViewportTag),this.options.enableScrollIntoView=null===(e=t.enableScrollIntoView)||void 0===e||e,this},t.prototype.withConf=function(t){return this.config=t,this},t.prototype.build=function(){var t,e,i,o,r,a,l,c,d,h,u,f,p,v,g;if(!this.accessToken||!this.expirationHandler)throw new Error("Configure access token end the expiration handler before");var m=n(this.accessToken);return new s(this.baseUrl,{version:m,theme:null===(t=this.config)||void 0===t?void 0:t.theme,customizationName:null===(e=this.config)||void 0===e?void 0:e.customizationName,translationName:null===(i=this.config)||void 0===i?void 0:i.translationName,accessToken:this.accessToken,flowName:this.flowName,lang:null===(o=this.config)||void 0===o?void 0:o.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===(v=this.config)||void 0===v?void 0:v.onMessage,onError:null===(g=this.config)||void 0===g?void 0:g.onError},{adaptIframeHeight:this.options.adaptIframeHeight,addViewportTag:this.options.addViewportTag,enableScrollIntoView:this.options.enableScrollIntoView,debug:this.debugEnabled})}}(),function(t,e){return new r(t,e)}),l=function(t){function o(i){var o=t.call(this,i)||this;return o.div=e.createRef(),o}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function o(){this.constructor=t}i(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}(o,t),o.prototype.shouldComponentUpdate=function(t){return!!t.force},o.prototype.componentDidMount=function(){this.renderSDK()},o.prototype.componentWillUnmount=function(){var t;null===(t=this.snsWebSdk)||void 0===t||t.destroy()},o.prototype.renderSDK=function(){var t=function(t){var e=t.expirationHandler,i=t.accessToken,o=t.config,n=void 0===o?null:o,s=t.options,r=void 0===s?null:s,l=t.onError,c=void 0===l?null:l,d=t.onMessage,h=void 0===d?null:d,u=t.testEnv,f=void 0!==u&&u,p=t.baseUrl,v=void 0===p?null:p,g=t.onNavigationUiControlsStateChanged,m=void 0===g?null:g,y=a(i,e);return n&&y.withConf(n),r&&y.withOptions(r),f&&y.onTestEnv(),v&&y.withBaseUrl(v),c&&y.on("idCheck.onError",c),h&&y.onMessage(h),m&&y.onNavigationUiControlsStateChanged(m),y}(this.props),e=this.div.current;this.snsWebSdk=t.build(),this.snsWebSdk.launch(e)},o.prototype.navigateBack=function(){this.snsWebSdk&&this.snsWebSdk.navigateBack()},o.prototype.singleStepNext=function(t){this.snsWebSdk&&this.snsWebSdk.singleStepNext(t)},o.prototype.setLanguage=function(t){this.snsWebSdk&&this.snsWebSdk.setLanguage(t)},o.prototype.setTheme=function(t){this.snsWebSdk&&this.snsWebSdk.setTheme(t)},o.prototype.render=function(){var t=this.props,i=t.style,o=void 0===i?{}:i,n=t.className,s=void 0===n?"":n;return e.createElement("div",{style:o,className:s,ref:this.div})},o}(e.Component);module.exports=l;