UNPKG

react-native-persona

Version:

Launch a mobile native implementation of the Persona inquiry flow from React Native.

583 lines (566 loc) 19.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Document", { enumerable: true, get: function () { return _StepData.Document; } }); Object.defineProperty(exports, "DocumentStepData", { enumerable: true, get: function () { return _StepData.DocumentStepData; } }); exports.Environment = void 0; Object.defineProperty(exports, "Fields", { enumerable: true, get: function () { return _fields.Fields; } }); Object.defineProperty(exports, "GovernmentIdCapture", { enumerable: true, get: function () { return _StepData.GovernmentIdCapture; } }); Object.defineProperty(exports, "GovernmentIdCaptureFrames", { enumerable: true, get: function () { return _StepData.GovernmentIdCaptureFrames; } }); Object.defineProperty(exports, "GovernmentIdCaptureMethod", { enumerable: true, get: function () { return _StepData.GovernmentIdCaptureMethod; } }); Object.defineProperty(exports, "GovernmentIdCaptureSide", { enumerable: true, get: function () { return _StepData.GovernmentIdCaptureSide; } }); Object.defineProperty(exports, "GovernmentIdStepData", { enumerable: true, get: function () { return _StepData.GovernmentIdStepData; } }); exports.InvalidTemplateVersion = exports.InvalidTemplateId = exports.InvalidInquiryId = exports.InvalidAccountId = exports.Inquiry = void 0; Object.defineProperty(exports, "PersonaInquiryView", { enumerable: true, get: function () { return _PersonaInquiryView.default; } }); Object.defineProperty(exports, "SelfieCapture", { enumerable: true, get: function () { return _StepData.SelfieCapture; } }); Object.defineProperty(exports, "SelfieCaptureMethod", { enumerable: true, get: function () { return _StepData.SelfieCaptureMethod; } }); Object.defineProperty(exports, "SelfieStepData", { enumerable: true, get: function () { return _StepData.SelfieStepData; } }); Object.defineProperty(exports, "StepData", { enumerable: true, get: function () { return _StepData.StepData; } }); exports.ThemeSource = exports.StyleVariant = void 0; Object.defineProperty(exports, "UiStepData", { enumerable: true, get: function () { return _StepData.UiStepData; } }); 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 _InquiryEvent = require("./InquiryEvent"); var _PersonaInquiryView = _interopRequireWildcard(require("./PersonaInquiryView")); var _versions = require("./versions"); var _StepData = require("./StepData"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } 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 = exports.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. */ let ThemeSource = exports.ThemeSource = /*#__PURE__*/function (ThemeSource) { ThemeSource["SERVER"] = "server"; /** * @deprecated Client side theming is deprecated, please configure your theme inside * the Persona Dashboard and use SERVER as the theme source. */ ThemeSource["CLIENT"] = "client"; return ThemeSource; }({}); /** * String enum for style variants. */ let StyleVariant = exports.StyleVariant = /*#__PURE__*/function (StyleVariant) { StyleVariant["LIGHT"] = "light"; StyleVariant["DARK"] = "dark"; return StyleVariant; }({}); const eventEmitter = new _reactNative.NativeEventEmitter(PersonaInquiry2); let onEventCallback = null; eventEmitter.addListener('onEvent', rawEvent => { let event = _InquiryEvent.InquiryEvent.fromJson(rawEvent.event); if (event != null) { var _onEventCallback; (_onEventCallback = onEventCallback) === null || _onEventCallback === void 0 || _onEventCallback(event); (0, _PersonaInquiryView.onPersonaInquiryViewEvent)(event); } }); 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, "styleVariant", void 0); _defineProperty(this, "disablePresentationAnimation", 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; this.styleVariant = options.styleVariant; this.disablePresentationAnimation = options.disablePresentationAnimation; // 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)); } static onEvent(callback) { onEventCallback = callback; } toOptionsJson() { return { 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, styleVariant: this.styleVariant, disablePresentationAnimation: this.disablePresentationAnimation }; } /** * Launch the Persona Inquiry. */ start() { this.onCompleteListener = eventEmitter.addListener('onComplete', event => { (0, _util.callOnCompleteCallback)(event, this.onComplete); this.clearListeners(); }); this.onCanceledListener = eventEmitter.addListener('onCanceled', event => { (0, _util.callOnCanceledCallback)(event, this.onCanceled); this.clearListeners(); }); this.onErrorListener = eventEmitter.addListener('onError', event => { (0, _util.callOnErrorCallback)(event, this.onError); this.clearListeners(); }); PersonaInquiry2.startInquiry(this.toOptionsJson()); } } 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); _defineProperty(this, "_styleVariant", void 0); _defineProperty(this, "_disablePresentationAnimation", 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; } styleVariant(styleVariant) { this._styleVariant = styleVariant; return this; } disablePresentationAnimation(disablePresentationAnimation) { this._disablePresentationAnimation = disablePresentationAnimation; 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, styleVariant: this._styleVariant, disablePresentationAnimation: this._disablePresentationAnimation }); } } 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); _defineProperty(this, "_styleVariant", void 0); _defineProperty(this, "_disablePresentationAnimation", 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; } styleVariant(styleVariant) { this._styleVariant = styleVariant; 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; } disablePresentationAnimation(disablePresentationAnimation) { this._disablePresentationAnimation = disablePresentationAnimation; 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, styleVariant: this._styleVariant, disablePresentationAnimation: this._disablePresentationAnimation }); } } /** * @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 = exports.default = InquiryBuilders; //# sourceMappingURL=index.js.map