import NativeVouch, { type VouchSuccess, type VouchError, type VouchHeadlessProgress, } from './NativeVouch'; import { NativeEventEmitter } from 'react-native'; const HEADLESS_PROGRESS_EVENT = 'VouchHeadlessProgress'; const eventEmitter = new NativeEventEmitter(NativeVouch); export interface VouchSDKConfig { customerId: string; apiKey: string; languageCodeOverride?: string; } export interface VouchStartParams { dataSourceId: string; webhookUrl: string; inputs?: Object; metadata?: string; } export type { VouchSuccess, VouchError, VouchHeadlessProgress }; class VouchSDK { /** * Initialize the Vouch SDK * @param config - Configuration object with customerId, apiKey, and optional language override */ initialize(config: VouchSDKConfig): Promise { return NativeVouch.initialize( config.customerId, config.apiKey, config.languageCodeOverride ?? null ).then(() => undefined); } /** * Check if the SDK is initialized * @returns true if initialized, false otherwise */ isInitialized(): boolean { return NativeVouch.isInitialized(); } /** * Destroy the Vouch SDK and clear initialization state */ destroy(): Promise { return NativeVouch.destroy().then(() => undefined); } /** * Start a proof request flow * Presents the native Vouch UI modally * @param params - Start parameters including dataSourceId, webhookUrl, and optional inputs * @returns Promise that resolves with VouchSuccess or rejects with VouchError */ start(params: VouchStartParams): Promise { return new Promise((resolve, reject) => { NativeVouch.start( params.dataSourceId, params.webhookUrl, params.inputs ?? null, params.metadata ?? null, (result: VouchSuccess) => resolve(result), (error: VouchError) => reject(error) ); }); } startHeadless( params: VouchStartParams, onProgress?: (progress: VouchHeadlessProgress) => void ): Promise { const subscription = onProgress ? eventEmitter.addListener(HEADLESS_PROGRESS_EVENT, onProgress) : null; return NativeVouch.startHeadless( params.dataSourceId, params.webhookUrl, params.inputs ?? null, params.metadata ?? null, () => undefined ).then( (result) => { subscription?.remove(); return result; }, (error) => { subscription?.remove(); throw error; } ); } } export default new VouchSDK();