react-native-okhi 1.2.32-beta.6 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.tsx CHANGED
@@ -1,12 +1,82 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * React Native OkHi SDK
4
+ *
5
+ * A comprehensive React Native library for address verification using OkHi's
6
+ * digital and physical verification methods.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import * as OkHi from 'react-native-okhi';
11
+ *
12
+ * // 1. Login
13
+ * await OkHi.login({
14
+ * auth: { branchId: 'xxx', clientKey: 'xxx' },
15
+ * user: { firstName: 'John', lastName: 'Doe', phone: '+254...', email: '...' },
16
+ * });
17
+ *
18
+ * // 2. Start verification
19
+ * const result = await OkHi.startDigitalAddressVerification();
20
+ * console.log('Verified address:', result.location.formattedAddress);
21
+ * ```
22
+ */
23
+
1
24
  import { Platform } from 'react-native';
2
25
  import Okhi from './NativeOkhi';
3
26
  import type { OkCollect, OkHiLogin, OkHiSuccessResponse } from './types';
4
- export type * from './types';
5
-
6
- export function multiply(a: number, b: number): number {
7
- return Okhi.multiply(a, b);
8
- }
27
+ import { OkHiException } from './types';
28
+ export * from './types';
9
29
 
30
+ /**
31
+ * Authenticates a user with the OkHi platform.
32
+ *
33
+ * @remarks
34
+ * This must be called before any verification functions. It establishes
35
+ * the user session and validates your API credentials.
36
+ *
37
+ * **When to call login:** The login function should be called once you have
38
+ * an authenticated user in your app. A common place to call login is immediately
39
+ * after the app dashboard is rendered, for example in a banking app after a user
40
+ * successfully signs in.
41
+ *
42
+ * It initializes OkHi and enables your users to resume verification if they
43
+ * switch devices, as well as enables re-verification of previously unknown addresses.
44
+ *
45
+ * The login persists for the duration of the app session.
46
+ *
47
+ * @param credentials - The login configuration containing auth credentials and user info
48
+ * @returns A promise that resolves with an array of permission strings that were granted,
49
+ * or `null` if `withPermissionsRequest` was not enabled
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * import * as OkHi from 'react-native-okhi';
54
+ * import type { OkHiLogin } from 'react-native-okhi';
55
+ *
56
+ * const credentials: OkHiLogin = {
57
+ * auth: {
58
+ * branchId: 'your_branch_id',
59
+ * clientKey: 'your_client_key',
60
+ * },
61
+ * user: {
62
+ * firstName: 'John',
63
+ * lastName: 'Doe',
64
+ * phone: '+254712345678',
65
+ * email: 'john.doe@example.com',
66
+ * },
67
+ * };
68
+ *
69
+ * try {
70
+ * await OkHi.login(credentials);
71
+ * console.log('Login successful');
72
+ * } catch (error) {
73
+ * console.error('Login failed:', error);
74
+ * }
75
+ * ```
76
+ *
77
+ * @see {@link OkHiLogin} - Configuration type
78
+ * @see {@link startDigitalAddressVerification} - Call after login to verify addresses
79
+ */
10
80
  export function login(credentials: OkHiLogin): Promise<string[] | null> {
11
81
  return new Promise((resolve) => {
12
82
  Okhi.login(credentials, (results) => {
@@ -39,7 +109,7 @@ function processVerificationResponse(
39
109
  response: unknown,
40
110
  error: unknown,
41
111
  resolve: (value: OkHiSuccessResponse) => void,
42
- reject: (reason: { code: string; message: string }) => void
112
+ reject: (reason: OkHiException) => void
43
113
  ) {
44
114
  try {
45
115
  const res = response as { user: string; location: string };
@@ -49,25 +119,75 @@ function processVerificationResponse(
49
119
  location: JSON.parse(res.location),
50
120
  });
51
121
  } else if (error != null) {
52
- const err = error as { code: string; message: string };
53
- reject({
54
- code: err.code,
55
- message: err.message,
56
- });
122
+ const err = error as { code?: string; message?: string };
123
+ reject(OkHiException.fromNativeError(err));
57
124
  } else {
58
- reject({
59
- code: 'unknown',
60
- message: 'unable to complete operation - unknown response',
61
- });
125
+ reject(
126
+ new OkHiException(OkHiException.UNKNOWN, 'An unknown error occurred')
127
+ );
62
128
  }
63
129
  } catch {
64
- reject({
65
- code: 'unknown',
66
- message: 'unable to complete operation - unknown error',
67
- });
130
+ reject(
131
+ new OkHiException(OkHiException.UNKNOWN, 'An unknown error occurred')
132
+ );
68
133
  }
69
134
  }
70
135
 
136
+ /**
137
+ * @remarks
138
+ * Starts the digital address verification flow.
139
+ *
140
+ * **Prerequisites:**
141
+ * - Must call {@link login} first
142
+ *
143
+ * @param okcollect - Optional configuration for styling and behavior
144
+ * @returns A promise that resolves with the user and location data
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * import * as OkHi from 'react-native-okhi';
149
+ *
150
+ * // Basic usage with defaults
151
+ * const result = await OkHi.startDigitalAddressVerification();
152
+ * console.log('Address:', result.location.formattedAddress);
153
+ * console.log('Location ID:', result.location.id);
154
+ * ```
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * import * as OkHi from 'react-native-okhi';
159
+ * import type { OkCollect } from 'react-native-okhi';
160
+ *
161
+ * // With custom styling
162
+ * const config: OkCollect = {
163
+ * style: {
164
+ * color: '#FF5722',
165
+ * logo: 'https://example.com/logo.png',
166
+ * },
167
+ * configuration: {
168
+ * streetView: true,
169
+ * },
170
+ * };
171
+ *
172
+ * const result = await OkHi.startDigitalAddressVerification(config);
173
+ * ```
174
+ *
175
+ * @example
176
+ * ```typescript
177
+ * import * as OkHi from 'react-native-okhi';
178
+ *
179
+ * // Start verification on previously created address
180
+ * const locationId: string = await fetchLocationIDFromMyDB()
181
+ * const result = await OkHi.startDigitalAddressVerification({
182
+ * locationId: locationId,
183
+ * });
184
+ * ```
185
+ *
186
+ * @see {@link OkCollect} - Configuration options
187
+ * @see {@link OkHiSuccessResponse} - Return type
188
+ * @see {@link startPhysicalAddressVerification} - For physical verification
189
+ * @see {@link startDigitalAndPhysicalAddressVerification} - For combined verification
190
+ */
71
191
  export function startDigitalAddressVerification(
72
192
  okcollect?: OkCollect
73
193
  ): Promise<OkHiSuccessResponse> {
@@ -79,6 +199,43 @@ export function startDigitalAddressVerification(
79
199
  });
80
200
  }
81
201
 
202
+ /**
203
+ * Starts the physical address verification flow.
204
+ *
205
+ * @remarks
206
+ * Physical verification requires an agent to visit the user's location
207
+ * in person.
208
+ *
209
+ * **Prerequisites:**
210
+ * - Must call {@link login} first
211
+ *
212
+ * @param okcollect - Optional configuration for styling and behavior
213
+ * @returns A promise that resolves with the user and location data
214
+ *
215
+ * @example
216
+ * ```typescript
217
+ * import * as OkHi from 'react-native-okhi';
218
+ *
219
+ * const result = await OkHi.startPhysicalAddressVerification();
220
+ * console.log('Verification requested for:', result.location.formattedAddress);
221
+ * console.log('Location ID for tracking:', result.location.id);
222
+ * ```
223
+ *
224
+ * @example
225
+ * ```typescript
226
+ * import * as OkHi from 'react-native-okhi';
227
+ *
228
+ * // With custom configuration
229
+ * const result = await OkHi.startPhysicalAddressVerification({
230
+ * style: { color: '#2196F3' },
231
+ * });
232
+ * ```
233
+ *
234
+ * @see {@link OkCollect} - Configuration options
235
+ * @see {@link OkHiSuccessResponse} - Return type
236
+ * @see {@link startDigitalAddressVerification} - For instant digital verification
237
+ * @see {@link startDigitalAndPhysicalAddressVerification} - For combined verification
238
+ */
82
239
  export function startPhysicalAddressVerification(
83
240
  okcollect?: OkCollect
84
241
  ): Promise<OkHiSuccessResponse> {
@@ -90,6 +247,47 @@ export function startPhysicalAddressVerification(
90
247
  });
91
248
  }
92
249
 
250
+ /**
251
+ * Starts both digital and physical address verification flows.
252
+ *
253
+ * @remarks
254
+ * This combines both verification methods for maximum confidence.
255
+ *
256
+ * **Prerequisites:**
257
+ * - Must call {@link login} first
258
+ *
259
+ * @param okcollect - Optional configuration for styling and behavior
260
+ * @returns A promise that resolves with the user and location data
261
+ *
262
+ * @example
263
+ * ```typescript
264
+ * import * as OkHi from 'react-native-okhi';
265
+ *
266
+ * const result = await OkHi.startDigitalAndPhysicalAddressVerification();
267
+ * console.log('Physical + Digital Verification started for:', result.location.id);
268
+ * ```
269
+ *
270
+ * @example
271
+ * ```typescript
272
+ * import * as OkHi from 'react-native-okhi';
273
+ *
274
+ * // With full customization
275
+ * const result = await OkHi.startDigitalAndPhysicalAddressVerification({
276
+ * style: {
277
+ * color: '#4CAF50',
278
+ * logo: 'https://example.com/logo.png',
279
+ * },
280
+ * configuration: {
281
+ * streetView: true,
282
+ * },
283
+ * });
284
+ * ```
285
+ *
286
+ * @see {@link OkCollect} - Configuration options
287
+ * @see {@link OkHiSuccessResponse} - Return type
288
+ * @see {@link startDigitalAddressVerification} - For digital-only verification
289
+ * @see {@link startPhysicalAddressVerification} - For physical-only verification
290
+ */
93
291
  export function startDigitalAndPhysicalAddressVerification(
94
292
  okcollect?: OkCollect
95
293
  ): Promise<OkHiSuccessResponse> {
@@ -104,6 +302,48 @@ export function startDigitalAndPhysicalAddressVerification(
104
302
  });
105
303
  }
106
304
 
305
+ /**
306
+ * Creates an address without starting verification.
307
+ *
308
+ * @remarks
309
+ * Use this when you want to collect and store an address but defer
310
+ * verification to a later time. The address can
311
+ * be verified later using the returned `locationId`.
312
+ *
313
+ * **Prerequisites:**
314
+ * - Must call {@link login} first
315
+ *
316
+ * @param okcollect - Optional configuration for styling and behavior
317
+ * @returns A promise that resolves with the user and location data
318
+ *
319
+ * @example
320
+ * ```typescript
321
+ * import * as OkHi from 'react-native-okhi';
322
+ *
323
+ * // Create address without verification
324
+ * const result = await OkHi.createAddress();
325
+ * console.log('Address created:', result.location.id);
326
+ *
327
+ * // Save the location ID to verify later
328
+ * const locationId = result.location.id;
329
+ * await saveToDatabase({ locationId: locationId });
330
+ * ```
331
+ *
332
+ * @example
333
+ * ```typescript
334
+ * import * as OkHi from 'react-native-okhi';
335
+ *
336
+ * // Later, verify the saved address
337
+ * const savedLocationId = await fetchLocationIdFromMyDB();
338
+ * const result = await OkHi.startDigitalAddressVerification({
339
+ * locationId: savedLocationId,
340
+ * });
341
+ * ```
342
+ *
343
+ * @see {@link OkCollect} - Configuration options
344
+ * @see {@link OkHiSuccessResponse} - Return type
345
+ * @see {@link startDigitalAddressVerification} - To verify an address
346
+ */
107
347
  export function createAddress(
108
348
  okcollect?: OkCollect
109
349
  ): Promise<OkHiSuccessResponse> {
@@ -120,11 +360,11 @@ function processBooleanResponse(
120
360
  result: unknown,
121
361
  error: unknown,
122
362
  resolve: (value: boolean) => void,
123
- reject: (reason: { code: string; message: string }) => void
363
+ reject: (reason: OkHiException) => void
124
364
  ) {
125
365
  if (error != null) {
126
- const err = error as { code: string; message: string };
127
- reject({ code: err.code, message: err.message });
366
+ const err = error as { code?: string; message?: string };
367
+ reject(OkHiException.fromNativeError(err));
128
368
  } else {
129
369
  resolve(result as boolean);
130
370
  }
@@ -135,18 +375,16 @@ function processStringResponse(
135
375
  result: unknown,
136
376
  error: unknown,
137
377
  resolve: (value: string) => void,
138
- reject: (reason: { code: string; message: string }) => void
378
+ reject: (reason: OkHiException) => void
139
379
  ) {
140
380
  if (error != null) {
141
- const err = error as { code: string; message: string };
142
- reject({ code: err.code, message: err.message });
381
+ const err = error as { code?: string; message?: string };
382
+ reject(OkHiException.fromNativeError(err));
143
383
  } else {
144
384
  resolve(result as string);
145
385
  }
146
386
  }
147
387
 
148
- // MARK: - Check Helpers
149
-
150
388
  export function isLocationServicesEnabled(): Promise<boolean> {
151
389
  return new Promise((resolve, reject) => {
152
390
  Okhi.isLocationServicesEnabled((result, error) => {
@@ -218,8 +456,6 @@ export function openProtectedApps(): Promise<void> {
218
456
  });
219
457
  }
220
458
 
221
- // MARK: - Request Helpers
222
-
223
459
  export function requestLocationPermission(): Promise<boolean> {
224
460
  return new Promise((resolve, reject) => {
225
461
  Okhi.requestLocationPermission((result, error) => {
@@ -247,10 +483,12 @@ export function requestEnableLocationServices(): Promise<boolean> {
247
483
  export function requestPostNotificationPermissions(): Promise<boolean> {
248
484
  return new Promise((resolve, reject) => {
249
485
  if (Platform.OS === 'ios') {
250
- reject({
251
- code: 'unsupported_platform',
252
- message: 'operation not supported',
253
- });
486
+ reject(
487
+ new OkHiException(
488
+ OkHiException.UNSUPPORTED_DEVICE,
489
+ 'Notification permission request is not supported on iOS. Use iOS-specific notification APIs.'
490
+ )
491
+ );
254
492
  } else {
255
493
  Okhi.requestPostNotificationPermissions((result, error) => {
256
494
  processBooleanResponse(result, error, resolve, reject);