@sumsub/react-native-mobilesdk-module
Version:
React Native Module exposing Sumsub MobileSDK
278 lines (231 loc) • 7.9 kB
JavaScript
import {NativeEventEmitter, NativeModules} from 'react-native';
const {SNSMobileSDKModule} = NativeModules;
const SUMSUB_SDK_HANDLERS = {
'onStatusChanged': null,
'onEvent': null,
'onLog': null,
'onActionResult': 'onActionResultCompleted'
}
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 () {
_currentInstance.sdkConf.tokenExpirationHandler()
.then(newToken => {
SNSMobileSDKModule.updateAccessToken(newToken)
})
.catch(err => {
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')
}
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
}
};