react-native-persona
Version:
Launch a mobile native implementation of the Persona inquiry flow from React Native.
562 lines (545 loc) • 19.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Environment = exports.DocumentStepData = void 0;
Object.defineProperty(exports, "Fields", {
enumerable: true,
get: function () {
return _fields.Fields;
}
});
exports.UiStepData = exports.ThemeSource = exports.SelfieStepData = exports.SelfieCaptureMethod = exports.InvalidTemplateVersion = exports.InvalidTemplateId = exports.InvalidInquiryId = exports.InvalidAccountId = exports.Inquiry = exports.GovernmentIdStepData = exports.GovernmentIdCaptureSide = exports.GovernmentIdCaptureMethod = void 0;
Object.defineProperty(exports, "Versions", {
enumerable: true,
get: function () {
return _versions.Versions;
}
});
exports.default = void 0;
var _reactNative = require("react-native");
var _util = require("./util");
var _fields = require("./fields");
var _versions = require("./versions");
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
const {
PersonaInquiry2
} = _reactNative.NativeModules;
// Using Opaque types + Smart Constructor enforces validation at
// instantiation time for IDS
class InvalidTemplateId extends Error {}
exports.InvalidTemplateId = InvalidTemplateId;
class InvalidTemplateVersion extends Error {}
exports.InvalidTemplateVersion = InvalidTemplateVersion;
class InvalidInquiryId extends Error {}
exports.InvalidInquiryId = InvalidInquiryId;
class InvalidAccountId extends Error {}
/**
* Run validations that the string is in proper Inquiry token format
* and do a type conversion to InquiryId.
*
* @param candidate
*/
exports.InvalidAccountId = InvalidAccountId;
function makeInquiryId(candidate) {
if (candidate && candidate.startsWith('inq_')) {
return candidate;
}
throw new InvalidInquiryId(`Valid template IDs start with "inq_". Received: ${candidate} `);
}
/**
* Run validations that the string is in proper Template token format
* and do a type conversion to TemplateId.
*
* @param candidate
*/
function makeTemplateId(candidate) {
if (candidate && candidate.startsWith('itmpl_')) {
return candidate;
}
throw new InvalidTemplateId(`Valid template IDs start with "itmpl_". Received: ${candidate} `);
}
/**
* Run validations that the string is in proper Template Version token format
* and do a type conversion to TemplateVersion.
*
* @param candidate
*/
function makeTemplateVersion(candidate) {
if (candidate && candidate.startsWith('itmplv_')) {
return candidate;
}
throw new InvalidTemplateVersion(`Valid template versions start with "itmplv_". Received: ${candidate} `);
}
/**
* Run validations that the string is in proper Template token format
* and do a type conversion to AccountId.
*
* @param candidate
*/
function makeAccountId(candidate) {
if (candidate && candidate.startsWith('act_')) {
return candidate;
}
throw new InvalidAccountId(`Valid account IDs start with "act_". Received: ${candidate} `);
}
/**
* String enum for environments. These strings will be parsed
* on the native side bridge into Kotlin / Swift enums.
*/
let Environment = /*#__PURE__*/function (Environment) {
Environment["SANDBOX"] = "sandbox";
Environment["PRODUCTION"] = "production";
return Environment;
}({});
/**
* An enum value which determines whether this sdk should use the theme values sent from the server.
*/
exports.Environment = Environment;
let ThemeSource = /*#__PURE__*/function (ThemeSource) {
ThemeSource["SERVER"] = "server";
ThemeSource["CLIENT"] = "client";
return ThemeSource;
}({});
/**
* Type for collected data that came directly from iOS/Android.
* Needs to be translated to TS classes before returning.
*/
exports.ThemeSource = ThemeSource;
class DocumentStepData {
constructor() {
_defineProperty(this, "stepName", void 0);
_defineProperty(this, "documents", void 0);
this.stepName = '';
this.documents = [];
}
}
exports.DocumentStepData = DocumentStepData;
class GovernmentIdStepData {
constructor() {
_defineProperty(this, "stepName", void 0);
_defineProperty(this, "captures", void 0);
this.stepName = '';
this.captures = [];
}
}
exports.GovernmentIdStepData = GovernmentIdStepData;
let GovernmentIdCaptureMethod = /*#__PURE__*/function (GovernmentIdCaptureMethod) {
GovernmentIdCaptureMethod["Manual"] = "Manual";
GovernmentIdCaptureMethod["Auto"] = "Auto";
GovernmentIdCaptureMethod["Upload"] = "Upload";
return GovernmentIdCaptureMethod;
}({});
exports.GovernmentIdCaptureMethod = GovernmentIdCaptureMethod;
let GovernmentIdCaptureSide = /*#__PURE__*/function (GovernmentIdCaptureSide) {
GovernmentIdCaptureSide["Front"] = "Front";
GovernmentIdCaptureSide["Back"] = "Back";
return GovernmentIdCaptureSide;
}({});
exports.GovernmentIdCaptureSide = GovernmentIdCaptureSide;
class SelfieStepData {
constructor() {
_defineProperty(this, "stepName", void 0);
_defineProperty(this, "centerCapture", void 0);
_defineProperty(this, "leftCapture", void 0);
_defineProperty(this, "rightCapture", void 0);
this.stepName = '';
this.centerCapture = null;
this.leftCapture = null;
this.rightCapture = null;
}
}
exports.SelfieStepData = SelfieStepData;
let SelfieCaptureMethod = /*#__PURE__*/function (SelfieCaptureMethod) {
SelfieCaptureMethod["Manual"] = "Manual";
SelfieCaptureMethod["Auto"] = "Auto";
return SelfieCaptureMethod;
}({});
exports.SelfieCaptureMethod = SelfieCaptureMethod;
class UiStepData {
constructor() {
_defineProperty(this, "stepName", void 0);
_defineProperty(this, "componentParams", void 0);
this.stepName = '';
this.componentParams = {};
}
}
exports.UiStepData = UiStepData;
const eventEmitter = new _reactNative.NativeEventEmitter(PersonaInquiry2);
class Inquiry {
constructor(options) {
_defineProperty(this, "templateId", void 0);
_defineProperty(this, "templateVersion", void 0);
_defineProperty(this, "inquiryId", void 0);
_defineProperty(this, "referenceId", void 0);
_defineProperty(this, "accountId", void 0);
_defineProperty(this, "environment", void 0);
_defineProperty(this, "environmentId", void 0);
_defineProperty(this, "themeSetId", void 0);
_defineProperty(this, "sessionToken", void 0);
_defineProperty(this, "returnCollectedData", void 0);
_defineProperty(this, "locale", void 0);
_defineProperty(this, "iosThemeObject", void 0);
_defineProperty(this, "themeSource", void 0);
_defineProperty(this, "fields", void 0);
_defineProperty(this, "onComplete", void 0);
_defineProperty(this, "onCanceled", void 0);
_defineProperty(this, "onError", void 0);
_defineProperty(this, "onCompleteListener", void 0);
_defineProperty(this, "onCanceledListener", void 0);
_defineProperty(this, "onErrorListener", void 0);
this.templateId = options.templateId;
this.templateVersion = options.templateVersion;
this.inquiryId = options.inquiryId;
this.referenceId = options.referenceId;
this.accountId = options.accountId;
this.environment = options.environment;
this.environmentId = options.environmentId;
this.themeSetId = options.themeSetId;
this.sessionToken = options.sessionToken;
this.fields = options.fields;
this.returnCollectedData = options.returnCollectedData;
this.locale = options.locale;
// Callbacks
this.onComplete = options.onComplete;
this.onCanceled = options.onCanceled;
this.onError = options.onError;
// Theme object
this.iosThemeObject = options.iosThemeObject;
this.themeSource = options.themeSource;
}
clearListeners() {
if (this.onCompleteListener) this.onCompleteListener.remove();
if (this.onCanceledListener) this.onCanceledListener.remove();
if (this.onErrorListener) this.onErrorListener.remove();
}
/**
* Create an Inquiry flow builder based on a template ID.
*
* You can find your template ID on the Dashboard under Inquiries > Templates.
* {@link https://app.withpersona.com/dashboard/inquiry-templates}
*
* @param templateId template ID from your Persona Dashboard
* @return builder for the Inquiry flow
*/
static fromTemplate(templateId) {
return new TemplateBuilder(makeTemplateId(templateId), null);
}
/**
* Create an Inquiry flow builder based on a template ID version.
*
* You can find your template ID version on the Dashboard under the
* settings view of a specific template.
* {@link https://app.withpersona.com/dashboard/inquiry-templates}
*
* @param templateVersion template version from your Persona Dashboard
* @return builder for the Inquiry flow
*/
static fromTemplateVersion(templateVersion) {
return new TemplateBuilder(null, makeTemplateVersion(templateVersion));
}
/**
* Create an Inquiry flow builder based on an inquiry ID.
*
* You will need to generate the inquiry ID on the server. To try it out, you can create an
* inquiry from the Persona Dashboard under "Inquiries". Click on the "Create Inquiry" button
* and copy the inquiry ID from the URL.
* {@link https://app.withpersona.com/dashboard/inquiries}
*
* @param inquiryId inquiry ID from your server
* @return builder for the Inquiry flow
*/
static fromInquiry(inquiryId) {
return new InquiryBuilder(makeInquiryId(inquiryId));
}
/**
* Launch the Persona Inquiry.
*/
start() {
this.onCompleteListener = eventEmitter.addListener('onComplete', event => {
if (this.onComplete) {
var _event$collectedData;
let fields = {};
for (let key of Object.keys(event.fields || {})) {
let field = event.fields[key];
if (field == undefined) {
fields[key] = new _fields.InquiryField.Unknown('null');
continue;
}
switch (field.type) {
case 'integer':
fields[key] = new _fields.InquiryField.Integer(Number.parseInt(field.value));
break;
case 'boolean':
fields[key] = new _fields.InquiryField.Boolean(field.value);
break;
case 'string':
fields[key] = new _fields.InquiryField.String(field.value);
break;
default:
fields[key] = new _fields.InquiryField.Unknown(field.type);
break;
}
}
let collectedData = null;
let stepData = (_event$collectedData = event.collectedData) === null || _event$collectedData === void 0 ? void 0 : _event$collectedData.stepData;
if (stepData != null) {
// Translate the step data from JSON to actual class objects
let translatedStepData = [];
for (let stepDatum of stepData) {
switch (stepDatum.type) {
case 'DocumentStepData':
translatedStepData.push(Object.assign(new DocumentStepData(), stepDatum));
break;
case 'GovernmentIdStepData':
translatedStepData.push(Object.assign(new GovernmentIdStepData(), stepDatum));
break;
case 'SelfieStepData':
translatedStepData.push(Object.assign(new SelfieStepData(), stepDatum));
break;
case 'UiStepData':
translatedStepData.push(Object.assign(new UiStepData(), stepDatum));
break;
}
}
collectedData = {
stepData: translatedStepData
};
}
let extraData = {
collectedData: collectedData
};
this.onComplete(event.inquiryId, event.status, fields, extraData);
}
this.clearListeners();
});
this.onCanceledListener = eventEmitter.addListener('onCanceled', event => {
if (this.onCanceled) this.onCanceled(event.inquiryId, event.sessionToken);
this.clearListeners();
});
this.onErrorListener = eventEmitter.addListener('onError', event => {
if (this.onError) this.onError(new Error(event.debugMessage), event.errorCode);
this.clearListeners();
});
PersonaInquiry2.startInquiry({
templateId: this.templateId,
templateVersion: this.templateVersion,
inquiryId: this.inquiryId,
referenceId: this.referenceId,
accountId: this.accountId,
environment: this.environment,
environmentId: this.environmentId,
themeSetId: this.themeSetId,
sessionToken: this.sessionToken,
fields: this.fields,
returnCollectedData: this.returnCollectedData,
themeSource: this.themeSource,
iosTheme: (0, _util.processThemeValues)(this.iosThemeObject || {}),
locale: this.locale
});
}
}
exports.Inquiry = Inquiry;
class InquiryBuilder {
constructor(inquiryId) {
_defineProperty(this, "_inquiryId", void 0);
_defineProperty(this, "_sessionToken", void 0);
// Callbacks
_defineProperty(this, "_onComplete", void 0);
_defineProperty(this, "_onCanceled", void 0);
_defineProperty(this, "_onError", void 0);
_defineProperty(this, "_iosThemeObject", void 0);
_defineProperty(this, "_themeSource", ThemeSource.SERVER);
_defineProperty(this, "_fields", void 0);
_defineProperty(this, "_locale", void 0);
this._inquiryId = inquiryId;
}
sessionToken(sessionToken) {
this._sessionToken = sessionToken;
return this;
}
onComplete(callback) {
this._onComplete = callback;
return this;
}
onCanceled(callback) {
this._onCanceled = callback;
return this;
}
onError(callback) {
this._onError = callback;
return this;
}
/**
* @deprecated Use iosThemeToUse
*/
iosTheme(themeObject) {
this._iosThemeObject = themeObject;
this._themeSource = ThemeSource.CLIENT;
return this;
}
iosThemeToUse(themeObject, themeSource) {
this._iosThemeObject = themeObject;
this._themeSource = themeSource;
return this;
}
locale(locale) {
this._locale = locale;
return this;
}
build() {
return new Inquiry({
inquiryId: this._inquiryId,
sessionToken: this._sessionToken,
onComplete: this._onComplete,
onCanceled: this._onCanceled,
onError: this._onError,
iosThemeObject: this._iosThemeObject,
themeSource: this._themeSource,
fields: this._fields,
locale: this._locale
});
}
}
class TemplateBuilder {
constructor(templateId, templateVersion) {
_defineProperty(this, "_templateId", void 0);
_defineProperty(this, "_templateVersion", void 0);
_defineProperty(this, "_accountId", void 0);
_defineProperty(this, "_referenceId", void 0);
_defineProperty(this, "_environment", void 0);
_defineProperty(this, "_environmentId", void 0);
_defineProperty(this, "_themeSetId", void 0);
_defineProperty(this, "_fields", void 0);
_defineProperty(this, "_sessionToken", void 0);
_defineProperty(this, "_returnCollectedData", void 0);
_defineProperty(this, "_locale", void 0);
// Callbacks
_defineProperty(this, "_onComplete", void 0);
_defineProperty(this, "_onCanceled", void 0);
_defineProperty(this, "_onError", void 0);
// Customization
_defineProperty(this, "_iosThemeObject", void 0);
_defineProperty(this, "_themeSource", ThemeSource.SERVER);
if (templateId != null) {
this._templateId = templateId;
} else if (templateVersion != null) {
this._templateVersion = templateVersion;
} else {
throw new InvalidTemplateId(`Either templateId or templateVersion needs to be set.`);
}
return this;
}
returnCollectedData(returnCollectedData) {
this._returnCollectedData = returnCollectedData;
return this;
}
referenceId(referenceId) {
if (referenceId == null) {
return this;
}
this._accountId = undefined;
this._referenceId = referenceId;
return this;
}
accountId(accountId) {
if (accountId == null) {
return this;
}
this._referenceId = undefined;
this._accountId = makeAccountId(accountId);
return this;
}
environment(environment) {
this._environment = environment;
return this;
}
environmentId(environmentId) {
this._environmentId = environmentId;
return this;
}
themeSetId(themeSetId) {
this._themeSetId = themeSetId;
return this;
}
sessionToken(sessionToken) {
this._sessionToken = sessionToken;
return this;
}
fields(fields) {
this._fields = fields;
return this;
}
locale(locale) {
this._locale = locale;
return this;
}
onComplete(callback) {
this._onComplete = callback;
return this;
}
onCanceled(callback) {
this._onCanceled = callback;
return this;
}
onError(callback) {
this._onError = callback;
return this;
}
/**
* @deprecated Use iosThemeToUse
*/
iosTheme(themeObject) {
this._iosThemeObject = themeObject;
this._themeSource = ThemeSource.CLIENT;
return this;
}
iosThemeToUse(themeObject, themeSource) {
this._iosThemeObject = themeObject;
this._themeSource = themeSource;
return this;
}
build() {
return new Inquiry({
templateId: this._templateId,
templateVersion: this._templateVersion,
accountId: this._accountId,
referenceId: this._referenceId,
environment: this._environment,
environmentId: this._environmentId,
themeSetId: this._themeSetId,
sessionToken: this._sessionToken,
fields: this._fields,
onComplete: this._onComplete,
onCanceled: this._onCanceled,
onError: this._onError,
iosThemeObject: this._iosThemeObject,
themeSource: this._themeSource,
returnCollectedData: this._returnCollectedData,
locale: this._locale
});
}
}
/**
* @deprecated Use the `Inquiry` static methods instead
*/
let InquiryBuilders;
(function (_InquiryBuilders) {
function fromInquiry(inquiryId) {
return Inquiry.fromInquiry(inquiryId);
}
_InquiryBuilders.fromInquiry = fromInquiry;
function fromTemplate(templateId) {
return Inquiry.fromTemplate(templateId);
}
_InquiryBuilders.fromTemplate = fromTemplate;
function fromTemplateVersion(templateVersion) {
return Inquiry.fromTemplateVersion(templateVersion);
}
_InquiryBuilders.fromTemplateVersion = fromTemplateVersion;
})(InquiryBuilders || (InquiryBuilders = {}));
var _default = InquiryBuilders;
exports.default = _default;
//# sourceMappingURL=index.js.map