UNPKG

@sumsub/react-native-mobilesdk-module

Version:

React Native Module exposing Sumsub MobileSDK

290 lines (241 loc) 8.4 kB
import {NativeEventEmitter, NativeModules} from 'react-native'; const {SNSMobileSDKModule} = NativeModules; const SUMSUB_SDK_HANDLERS = { 'onStatusChanged': null, 'onEvent': null, 'onLog': null } const SUMSUB_SDK_DEPRECATED_HANDLERS = ['onActionResult']; var _currentInstance = null; function SNSMobileSDK(sdkConf) { this.sdkConf = sdkConf this.sdkConf.settings = { "appFrameworkName": "react-native", // "appFrameworkVersion": "..." } } SNSMobileSDK.prototype.dismiss = function () { SNSMobileSDKModule.dismiss() } SNSMobileSDK.prototype.getNewAccessToken = function () { console.log("Requesting new access token from tokenExpirationHandler") _currentInstance.sdkConf.tokenExpirationHandler() .then(newToken => { console.log("New access token received.") SNSMobileSDKModule.updateAccessToken(newToken) }) .catch(err => { console.log("Failed to get new access token!") console.error(err) SNSMobileSDKModule.updateAccessToken(null) }) } SNSMobileSDK.prototype.launch = function () { if (_currentInstance) { return Promise.reject(new Error("Aborted since another instance is in use!")); } _currentInstance = this const eventEmitter = new NativeEventEmitter(NativeModules.SNSMobileSDKModule); var listeners = [] listeners.push(eventEmitter.addListener('onTokenExpired', () => { console.log("Token expired event received") _currentInstance.getNewAccessToken() })); Object.keys(this.sdkConf.handlers).forEach(handlerName => { var handler = this.sdkConf.handlers[handlerName] if (!handler) { return; } var completionAction = SUMSUB_SDK_HANDLERS[handlerName]; listeners.push(eventEmitter.addListener(handlerName, (event) => { // console.log("Invoked ["+handlerName+"] with " + JSON.stringify(event)) if (!completionAction) { handler(event); return; } var onComplete = function (error, result) { SNSMobileSDKModule[completionAction]({ "error": error, "result": result }); } handler(event) .then(result => { onComplete(null, result) }) .catch(error => { onComplete(error || new Error("rejected"), null) }) })); }) const cleanup = () => { _currentInstance = null listeners.forEach(listener => { listener.remove() }) } return SNSMobileSDKModule.launch( this.sdkConf.apiUrl, this.sdkConf.accessToken, this.sdkConf.locale, this.sdkConf.debug, this.sdkConf.hasHandlers, this.sdkConf.applicantConf, this.sdkConf.preferredDocumentDefinitions, this.sdkConf.theme, this.sdkConf.settings, this.sdkConf.strings, this.sdkConf.isAnalyticsEnabled, this.sdkConf.autoCloseOnApprove ) .catch(error => { cleanup() return Promise.reject(error) }) .then(result => { cleanup() return result }) }; function Builder() { this.debug = false; this.isAnalyticsEnabled = true; this.handlers = {}; this.applicantConf = {}; this.preferredDocumentDefinitions = {}; this.autoCloseOnApprove = 3; this.settings = {}; return this; } Builder.prototype.withAccessToken = function (accessToken, expirationHandler) { this.accessToken = accessToken if (!expirationHandler || typeof expirationHandler !== 'function') { throw new Error('Invalid parameter, "expirationHandler" must be a function') } this.tokenExpirationHandler = expirationHandler return this } Builder.prototype.withHandlers = function (handlers) { if (!handlers || typeof handlers !== 'object') { throw new Error('Invalid parameter, "handlers" must be a hash') } SUMSUB_SDK_DEPRECATED_HANDLERS.forEach(deprecatedHandler => { if (deprecatedHandler in handlers) { var error = new Error('\"' + deprecatedHandler + '\" is a deprecated handler and is not supported'); console.error(error); throw error; } }); Object.keys(SUMSUB_SDK_HANDLERS).forEach(handlerName => { var handler = handlers[handlerName] if (handler) { if (typeof handler !== 'function') { throw new Error('Invalid parameter, "'+handlerName+'" must be a function, not '+typeof(handler)) } this.handlers[handlerName] = handler; } }) return this } Builder.prototype.withDebug = function (flag) { if (typeof flag !== 'boolean') { throw new Error('Invalid parameter, "withDebug" expects a boolean'); } this.debug = flag; return this; } Builder.prototype.withAnalyticsEnabled = function (flag) { if (typeof flag !== 'boolean') { throw new Error('Invalid parameter, "withAnalyticsEnabled" expects a boolean'); } this.isAnalyticsEnabled = flag; return this; } Builder.prototype.withLocale = function (locale) { if (typeof locale !== 'string') { throw new Error('Invalid parameter, "locale" must be a string'); } this.locale = locale; return this; } Builder.prototype.withApplicantConf = function (applicantConf) { if (!applicantConf || typeof applicantConf !== 'object') { throw new Error('Invalid parameter, "withApplicantConf" expects a hash') } this.applicantConf = applicantConf return this } Builder.prototype.withPreferredDocumentDefinitions = function (preferredDocumentDefinitions) { if (!preferredDocumentDefinitions || typeof preferredDocumentDefinitions !== 'object') { throw new Error('Invalid parameter, "withPreferredDocumentDefinitions" expects a hash') } this.preferredDocumentDefinitions = preferredDocumentDefinitions return this } Builder.prototype.withAutoCloseOnApprove = function (autoCloseOnApprove) { if (typeof autoCloseOnApprove !== 'number') { throw new Error('Invalid parameter, "autoCloseOnApprove" expects a number') } this.autoCloseOnApprove = autoCloseOnApprove return this } Builder.prototype.withSettings = function (settings) { if (!settings || typeof settings !== 'object') { throw new Error('Invalid parameter, "withSettings" expects a hash') } this.settings = settings return this } Builder.prototype.withStrings = function (strings) { if (!strings || typeof strings !== 'object') { throw new Error('Invalid parameter, "withStrings" expects a hash') } this.strings = strings; return this } Builder.prototype.withTheme = function (theme) { if (!theme || typeof theme !== 'object') { throw new Error('Invalid parameter, "withTheme" expects a hash') } this.theme = theme; return this } Builder.prototype.withBaseUrl = function (apiUrl) { if (typeof apiUrl !== 'string') { throw new Error('Invalid parameter, "baseUrl" must be a string'); } this.apiUrl = apiUrl; return this; } Builder.prototype.onTestEnv = function () { return this.withBaseUrl("https://test-api.sumsub.com") } Builder.prototype.build = function () { var hasHandlers = {} Object.keys(this.handlers).forEach(name => { hasHandlers[name] = true; }) return new SNSMobileSDK({ apiUrl: this.apiUrl, accessToken: this.accessToken, tokenExpirationHandler: this.tokenExpirationHandler, handlers: this.handlers, hasHandlers: hasHandlers, locale: this.locale, applicantConf: this.applicantConf, preferredDocumentDefinitions: this.preferredDocumentDefinitions, theme: this.theme, strings: this.strings, isAnalyticsEnabled: this.isAnalyticsEnabled, debug: this.debug, autoCloseOnApprove: this.autoCloseOnApprove }); } module.exports = { init: function (accessToken, expirationHandler) { return new Builder().withAccessToken(accessToken, expirationHandler) }, reset: function() { _currentInstance = null } };