UNPKG

react-native-persona

Version:

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

460 lines (443 loc) 15.8 kB
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); } import { NativeEventEmitter, NativeModules } from 'react-native'; import { callOnCanceledCallback, callOnCompleteCallback, callOnErrorCallback, processThemeValues } from './util'; import { Fields } from './fields'; import { InquiryEvent } from './InquiryEvent'; import PersonaInquiryView, { onPersonaInquiryViewEvent } from './PersonaInquiryView'; import { Versions } from './versions'; import { StepData, Document, UiStepData, SelfieCaptureMethod, SelfieCapture, SelfieStepData, GovernmentIdCaptureFrames, GovernmentIdCaptureSide, GovernmentIdCaptureMethod, GovernmentIdCapture, GovernmentIdStepData, DocumentStepData } from './StepData'; export { Fields }; export { PersonaInquiryView }; export { Versions }; export { StepData, Document, UiStepData, SelfieCaptureMethod, SelfieCapture, SelfieStepData, GovernmentIdCaptureFrames, GovernmentIdCaptureSide, GovernmentIdCaptureMethod, GovernmentIdCapture, GovernmentIdStepData, DocumentStepData }; const { PersonaInquiry2 } = NativeModules; // Using Opaque types + Smart Constructor enforces validation at // instantiation time for IDS export class InvalidTemplateId extends Error {} export class InvalidTemplateVersion extends Error {} export class InvalidInquiryId extends Error {} export class InvalidAccountId extends Error {} /** * Run validations that the string is in proper Inquiry token format * and do a type conversion to InquiryId. * * @param candidate */ 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. */ export 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. */ export let 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; }({}); const eventEmitter = new NativeEventEmitter(PersonaInquiry2); let onEventCallback = null; eventEmitter.addListener('onEvent', rawEvent => { let event = InquiryEvent.fromJson(rawEvent.event); if (event != null) { var _onEventCallback; (_onEventCallback = onEventCallback) === null || _onEventCallback === void 0 || _onEventCallback(event); onPersonaInquiryViewEvent(event); } }); export 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, "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.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: processThemeValues(this.iosThemeObject || {}), locale: this.locale, disablePresentationAnimation: this.disablePresentationAnimation }; } /** * Launch the Persona Inquiry. */ start() { this.onCompleteListener = eventEmitter.addListener('onComplete', event => { callOnCompleteCallback(event, this.onComplete); this.clearListeners(); }); this.onCanceledListener = eventEmitter.addListener('onCanceled', event => { callOnCanceledCallback(event, this.onCanceled); this.clearListeners(); }); this.onErrorListener = eventEmitter.addListener('onError', event => { callOnErrorCallback(event, this.onError); this.clearListeners(); }); PersonaInquiry2.startInquiry(this.toOptionsJson()); } } 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, "_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; } 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, 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, "_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; } 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, 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 = {})); export default InquiryBuilders; //# sourceMappingURL=index.js.map