@incodetech/core 2.0.0-alpha.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.
Files changed (84) hide show
  1. package/package.json +51 -0
  2. package/src/camera/cameraActor.ts +21 -0
  3. package/src/camera/cameraService.test.ts +437 -0
  4. package/src/camera/cameraService.ts +165 -0
  5. package/src/camera/cameraServices.test.ts +66 -0
  6. package/src/camera/cameraServices.ts +26 -0
  7. package/src/camera/cameraStateMachine.test.ts +602 -0
  8. package/src/camera/cameraStateMachine.ts +264 -0
  9. package/src/camera/index.ts +5 -0
  10. package/src/camera/types.ts +17 -0
  11. package/src/device/getBrowser.ts +31 -0
  12. package/src/device/getDeviceClass.ts +29 -0
  13. package/src/device/index.ts +2 -0
  14. package/src/email/__mocks__/emailMocks.ts +59 -0
  15. package/src/email/emailActor.ts +15 -0
  16. package/src/email/emailManager.test.ts +573 -0
  17. package/src/email/emailManager.ts +427 -0
  18. package/src/email/emailServices.ts +66 -0
  19. package/src/email/emailStateMachine.test.ts +741 -0
  20. package/src/email/emailStateMachine.ts +367 -0
  21. package/src/email/index.ts +39 -0
  22. package/src/email/types.ts +60 -0
  23. package/src/events/addEvent.ts +20 -0
  24. package/src/events/types.ts +7 -0
  25. package/src/flow/__mocks__/flowMocks.ts +84 -0
  26. package/src/flow/flowActor.ts +13 -0
  27. package/src/flow/flowAnalyzer.test.ts +266 -0
  28. package/src/flow/flowAnalyzer.ts +37 -0
  29. package/src/flow/flowCompletionService.ts +21 -0
  30. package/src/flow/flowManager.test.ts +560 -0
  31. package/src/flow/flowManager.ts +235 -0
  32. package/src/flow/flowServices.test.ts +109 -0
  33. package/src/flow/flowServices.ts +13 -0
  34. package/src/flow/flowStateMachine.test.ts +334 -0
  35. package/src/flow/flowStateMachine.ts +182 -0
  36. package/src/flow/index.ts +21 -0
  37. package/src/flow/moduleLoader.test.ts +136 -0
  38. package/src/flow/moduleLoader.ts +73 -0
  39. package/src/flow/orchestratedFlowManager.test.ts +240 -0
  40. package/src/flow/orchestratedFlowManager.ts +231 -0
  41. package/src/flow/orchestratedFlowStateMachine.test.ts +199 -0
  42. package/src/flow/orchestratedFlowStateMachine.ts +325 -0
  43. package/src/flow/types.ts +434 -0
  44. package/src/http/__mocks__/api.ts +88 -0
  45. package/src/http/api.test.ts +231 -0
  46. package/src/http/api.ts +90 -0
  47. package/src/http/endpoints.ts +17 -0
  48. package/src/index.ts +33 -0
  49. package/src/permissions/index.ts +2 -0
  50. package/src/permissions/permissionServices.ts +31 -0
  51. package/src/permissions/types.ts +3 -0
  52. package/src/phone/__mocks__/phoneMocks.ts +71 -0
  53. package/src/phone/index.ts +39 -0
  54. package/src/phone/phoneActor.ts +15 -0
  55. package/src/phone/phoneManager.test.ts +393 -0
  56. package/src/phone/phoneManager.ts +458 -0
  57. package/src/phone/phoneServices.ts +98 -0
  58. package/src/phone/phoneStateMachine.test.ts +918 -0
  59. package/src/phone/phoneStateMachine.ts +422 -0
  60. package/src/phone/types.ts +83 -0
  61. package/src/recordings/recordingsRepository.test.ts +87 -0
  62. package/src/recordings/recordingsRepository.ts +48 -0
  63. package/src/recordings/streamingEvents.ts +10 -0
  64. package/src/selfie/__mocks__/selfieMocks.ts +26 -0
  65. package/src/selfie/index.ts +14 -0
  66. package/src/selfie/selfieActor.ts +17 -0
  67. package/src/selfie/selfieErrorUtils.test.ts +116 -0
  68. package/src/selfie/selfieErrorUtils.ts +66 -0
  69. package/src/selfie/selfieManager.test.ts +297 -0
  70. package/src/selfie/selfieManager.ts +301 -0
  71. package/src/selfie/selfieServices.ts +362 -0
  72. package/src/selfie/selfieStateMachine.test.ts +283 -0
  73. package/src/selfie/selfieStateMachine.ts +804 -0
  74. package/src/selfie/selfieUploadService.test.ts +90 -0
  75. package/src/selfie/selfieUploadService.ts +81 -0
  76. package/src/selfie/types.ts +103 -0
  77. package/src/session/index.ts +5 -0
  78. package/src/session/sessionService.ts +78 -0
  79. package/src/setup.test.ts +61 -0
  80. package/src/setup.ts +171 -0
  81. package/tsconfig.json +13 -0
  82. package/tsdown.config.ts +22 -0
  83. package/vitest.config.ts +37 -0
  84. package/vitest.setup.ts +135 -0
@@ -0,0 +1,367 @@
1
+ import { assign, fromCallback, fromPromise, setup } from '@incodetech/infra';
2
+ import { addEvent } from '../events/addEvent';
3
+ import {
4
+ addEmail,
5
+ fetchEmail,
6
+ sendEmailOtp,
7
+ verifyEmailOtp,
8
+ } from './emailServices';
9
+ import type { EmailConfig } from './types';
10
+
11
+ export type EmailContext = {
12
+ config: EmailConfig;
13
+ email: string;
14
+ isValid: boolean;
15
+ emailError: string | undefined;
16
+ prefilledEmail: string | undefined;
17
+ error: string | undefined;
18
+ otpCode: string;
19
+ otpError: string | undefined;
20
+ attemptsRemaining: number;
21
+ resendTimer: number;
22
+ resendTimerActive: boolean;
23
+ };
24
+
25
+ export type EmailEvent =
26
+ | { type: 'LOAD' }
27
+ | { type: 'EMAIL_CHANGED'; email: string; isValid: boolean }
28
+ | { type: 'SUBMIT' }
29
+ | { type: 'OTP_CHANGED'; code: string }
30
+ | { type: 'VERIFY_OTP' }
31
+ | { type: 'RESEND_OTP' }
32
+ | { type: 'BACK' }
33
+ | { type: 'RESET' }
34
+ | { type: 'TICK' };
35
+
36
+ export type EmailInput = {
37
+ config: EmailConfig;
38
+ };
39
+
40
+ const RESEND_TIMER_SECONDS = 30;
41
+
42
+ export const emailMachine = setup({
43
+ types: {
44
+ context: {} as EmailContext,
45
+ events: {} as EmailEvent,
46
+ input: {} as EmailInput,
47
+ },
48
+ actors: {
49
+ fetchEmail: fromPromise<{ email: string }, void>(async ({ signal }) => {
50
+ return fetchEmail(signal);
51
+ }),
52
+ submitEmail: fromPromise<{ success: boolean }, { email: string }>(
53
+ async ({ input, signal }) => {
54
+ return addEmail({ email: input.email }, signal);
55
+ },
56
+ ),
57
+ sendOtp: fromPromise<void, void>(async ({ signal }) => {
58
+ return sendEmailOtp(signal);
59
+ }),
60
+ verifyOtp: fromPromise<{ success: boolean }, { code: string }>(
61
+ async ({ input, signal }) => {
62
+ return verifyEmailOtp(input.code, signal);
63
+ },
64
+ ),
65
+ resendTimer: fromCallback(({ sendBack }) => {
66
+ let seconds = RESEND_TIMER_SECONDS;
67
+ const interval = setInterval(() => {
68
+ seconds -= 1;
69
+ sendBack({ type: 'TICK' });
70
+ if (seconds <= 0) {
71
+ clearInterval(interval);
72
+ }
73
+ }, 1000);
74
+ return () => clearInterval(interval);
75
+ }),
76
+ },
77
+ actions: {
78
+ setPrefilledEmail: assign(({ event }) => {
79
+ const email = (event as unknown as { output: { email: string } }).output
80
+ .email;
81
+ return {
82
+ prefilledEmail: email,
83
+ email,
84
+ };
85
+ }),
86
+ setEmail: assign(({ event }) => {
87
+ const e = event as {
88
+ type: 'EMAIL_CHANGED';
89
+ email: string;
90
+ isValid: boolean;
91
+ };
92
+ return {
93
+ email: e.email,
94
+ isValid: e.isValid,
95
+ emailError: e.isValid ? undefined : 'Invalid email address',
96
+ };
97
+ }),
98
+ setEmailError: assign(({ event }) => ({
99
+ emailError: String((event as unknown as { error: unknown }).error),
100
+ })),
101
+ setError: assign(({ event }) => ({
102
+ error: String((event as unknown as { error: unknown }).error),
103
+ })),
104
+ clearError: assign({
105
+ error: () => undefined,
106
+ }),
107
+ clearEmailError: assign({
108
+ emailError: () => undefined,
109
+ }),
110
+ setOtpCode: assign(({ event }) => ({
111
+ otpCode: (event as { type: 'OTP_CHANGED'; code: string }).code,
112
+ otpError: undefined,
113
+ })),
114
+ setOtpError: assign(({ context, event }) => ({
115
+ otpError: String((event as unknown as { error: unknown }).error),
116
+ attemptsRemaining: context.attemptsRemaining - 1,
117
+ })),
118
+ clearOtpError: assign({
119
+ otpError: () => undefined,
120
+ otpCode: () => '',
121
+ }),
122
+ startResendTimer: assign({
123
+ resendTimer: () => RESEND_TIMER_SECONDS,
124
+ resendTimerActive: () => true,
125
+ }),
126
+ tickResendTimer: assign(({ context }) => {
127
+ const newTimer = Math.max(0, context.resendTimer - 1);
128
+ return {
129
+ resendTimer: newTimer,
130
+ resendTimerActive: newTimer > 0,
131
+ };
132
+ }),
133
+ stopResendTimer: assign({
134
+ resendTimerActive: () => false,
135
+ }),
136
+ resetContext: assign(({ context }) => ({
137
+ config: context.config,
138
+ email: '',
139
+ isValid: false,
140
+ emailError: undefined,
141
+ prefilledEmail: undefined,
142
+ error: undefined,
143
+ otpCode: '',
144
+ otpError: undefined,
145
+ attemptsRemaining: context.config.maxOtpAttempts ?? 3,
146
+ resendTimer: 0,
147
+ resendTimerActive: false,
148
+ })),
149
+ sendEmailSubmitEvent: () => {
150
+ addEvent({
151
+ code: 'continue',
152
+ module: 'email',
153
+ screen: 'emailInput',
154
+ });
155
+ },
156
+ },
157
+ guards: {
158
+ hasPrefill: ({ context }): boolean => context.config.prefill,
159
+ hasOtpVerification: ({ context }): boolean =>
160
+ context.config.otpVerification,
161
+ isValidEmail: ({ context }): boolean => context.isValid,
162
+ hasAttemptsRemaining: ({ context }): boolean =>
163
+ context.attemptsRemaining > 0,
164
+ canResend: ({ context }): boolean => !context.resendTimerActive,
165
+ },
166
+ }).createMachine({
167
+ id: 'email',
168
+ initial: 'idle',
169
+ context: ({ input }) => ({
170
+ config: input.config,
171
+ email: '',
172
+ isValid: false,
173
+ emailError: undefined,
174
+ prefilledEmail: undefined,
175
+ error: undefined,
176
+ otpCode: '',
177
+ otpError: undefined,
178
+ attemptsRemaining: input.config.maxOtpAttempts ?? 3,
179
+ resendTimer: 0,
180
+ resendTimerActive: false,
181
+ }),
182
+ states: {
183
+ idle: {
184
+ on: {
185
+ LOAD: [
186
+ {
187
+ target: 'loadingPrefill',
188
+ guard: 'hasPrefill',
189
+ },
190
+ {
191
+ target: 'inputting',
192
+ },
193
+ ],
194
+ },
195
+ },
196
+
197
+ loadingPrefill: {
198
+ invoke: {
199
+ id: 'fetchEmail',
200
+ src: 'fetchEmail',
201
+ onDone: {
202
+ target: 'inputting',
203
+ actions: 'setPrefilledEmail',
204
+ },
205
+ onError: {
206
+ target: 'inputting',
207
+ },
208
+ },
209
+ },
210
+
211
+ inputting: {
212
+ entry: 'clearEmailError',
213
+ on: {
214
+ EMAIL_CHANGED: {
215
+ actions: 'setEmail',
216
+ },
217
+ SUBMIT: {
218
+ target: 'submitting',
219
+ guard: 'isValidEmail',
220
+ },
221
+ },
222
+ },
223
+
224
+ submitting: {
225
+ invoke: {
226
+ id: 'submitEmail',
227
+ src: 'submitEmail',
228
+ input: ({ context }) => ({
229
+ email: context.email,
230
+ }),
231
+ onDone: [
232
+ {
233
+ target: 'sendingOtp',
234
+ guard: 'hasOtpVerification',
235
+ actions: 'sendEmailSubmitEvent',
236
+ },
237
+ {
238
+ target: 'success',
239
+ actions: 'sendEmailSubmitEvent',
240
+ },
241
+ ],
242
+ onError: {
243
+ target: 'inputting',
244
+ actions: 'setEmailError',
245
+ },
246
+ },
247
+ },
248
+
249
+ sendingOtp: {
250
+ invoke: {
251
+ id: 'sendOtp',
252
+ src: 'sendOtp',
253
+ onDone: {
254
+ target: 'awaitingOtp',
255
+ },
256
+ onError: {
257
+ target: 'awaitingOtp',
258
+ actions: 'setError',
259
+ },
260
+ },
261
+ },
262
+
263
+ awaitingOtp: {
264
+ entry: 'startResendTimer',
265
+ invoke: {
266
+ id: 'resendTimer',
267
+ src: 'resendTimer',
268
+ },
269
+ on: {
270
+ TICK: {
271
+ actions: 'tickResendTimer',
272
+ },
273
+ OTP_CHANGED: {
274
+ actions: 'setOtpCode',
275
+ },
276
+ VERIFY_OTP: {
277
+ target: 'verifyingOtp',
278
+ },
279
+ RESEND_OTP: {
280
+ target: 'sendingOtp',
281
+ guard: 'canResend',
282
+ },
283
+ BACK: {
284
+ target: 'inputting',
285
+ },
286
+ },
287
+ },
288
+
289
+ verifyingOtp: {
290
+ invoke: {
291
+ id: 'verifyOtp',
292
+ src: 'verifyOtp',
293
+ input: ({ context }) => ({
294
+ code: context.otpCode,
295
+ }),
296
+ onDone: [
297
+ {
298
+ target: 'success',
299
+ guard: ({ event }) => event.output.success === true,
300
+ },
301
+ {
302
+ target: 'otpError',
303
+ guard: 'hasAttemptsRemaining',
304
+ actions: assign(({ context }) => ({
305
+ otpError: 'Invalid OTP code',
306
+ attemptsRemaining: context.attemptsRemaining - 1,
307
+ })),
308
+ },
309
+ {
310
+ target: 'error',
311
+ actions: assign({
312
+ error: () => 'Maximum OTP attempts exceeded',
313
+ }),
314
+ },
315
+ ],
316
+ onError: [
317
+ {
318
+ target: 'otpError',
319
+ guard: 'hasAttemptsRemaining',
320
+ actions: 'setOtpError',
321
+ },
322
+ {
323
+ target: 'error',
324
+ actions: 'setError',
325
+ },
326
+ ],
327
+ },
328
+ },
329
+
330
+ otpError: {
331
+ on: {
332
+ OTP_CHANGED: {
333
+ target: 'awaitingOtp',
334
+ actions: 'setOtpCode',
335
+ },
336
+ RESEND_OTP: {
337
+ target: 'sendingOtp',
338
+ guard: 'canResend',
339
+ },
340
+ BACK: {
341
+ target: 'inputting',
342
+ },
343
+ },
344
+ },
345
+
346
+ success: {
347
+ on: {
348
+ RESET: {
349
+ target: 'idle',
350
+ actions: 'resetContext',
351
+ },
352
+ },
353
+ },
354
+
355
+ error: {
356
+ on: {
357
+ RESET: {
358
+ target: 'idle',
359
+ actions: 'resetContext',
360
+ },
361
+ },
362
+ },
363
+ },
364
+ // biome-ignore lint/suspicious/noExplicitAny: XState type inference issue with guards in declaration files
365
+ }) as any;
366
+
367
+ export type EmailMachine = typeof emailMachine;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Email verification module - supports headless and UI-driven usage.
3
+ *
4
+ * ## Headless Usage (Programmatic)
5
+ *
6
+ * Use `createEmailManager` to control email verification without any UI.
7
+ * Perfect for custom UI implementations, backend integrations, or automated testing.
8
+ *
9
+ * ```typescript
10
+ * import { createEmailManager } from '@incodetech/core/email';
11
+ *
12
+ * const manager = createEmailManager({
13
+ * config: { otpVerification: true, otpExpirationInMinutes: 5, prefill: false },
14
+ * });
15
+ *
16
+ * manager.subscribe((state) => console.log(state.status));
17
+ * manager.load();
18
+ * manager.setEmail('user@example.com', true);
19
+ * manager.submit();
20
+ * manager.submitOtp('ABC123');
21
+ * manager.stop();
22
+ * ```
23
+ *
24
+ * ## UI Usage
25
+ *
26
+ * For a ready-to-use UI, import the Email component from `@incodetech/ui/email`.
27
+ *
28
+ * @module @incodetech/core/email
29
+ * @see {@link createEmailManager} for headless API documentation
30
+ * @see {@link EmailConfig} for configuration options
31
+ * @see {@link EmailState} for state machine states
32
+ */
33
+ export {
34
+ createEmailManager,
35
+ type EmailManager,
36
+ type EmailState,
37
+ } from './emailManager';
38
+ export { emailMachine } from './emailStateMachine';
39
+ export type { EmailConfig } from './types';
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Configuration options for email verification.
3
+ *
4
+ * @example Standard OTP verification
5
+ * ```typescript
6
+ * const config: EmailConfig = {
7
+ * otpVerification: true,
8
+ * otpExpirationInMinutes: 5,
9
+ * prefill: false,
10
+ * };
11
+ * ```
12
+ *
13
+ * @example With all options
14
+ * ```typescript
15
+ * const config: EmailConfig = {
16
+ * otpVerification: true,
17
+ * otpExpirationInMinutes: 10,
18
+ * prefill: true,
19
+ * maxOtpAttempts: 3,
20
+ * };
21
+ * ```
22
+ */
23
+ export type EmailConfig = {
24
+ /**
25
+ * Whether to require OTP (email code) verification.
26
+ * If false, email is verified immediately after submission.
27
+ */
28
+ otpVerification: boolean;
29
+
30
+ /**
31
+ * How long the OTP code remains valid, in minutes.
32
+ * After expiration, user must request a new code.
33
+ */
34
+ otpExpirationInMinutes: number;
35
+
36
+ /**
37
+ * Whether to pre-populate with user's previously stored email address.
38
+ * Useful for returning users.
39
+ */
40
+ prefill: boolean;
41
+
42
+ /**
43
+ * Maximum number of OTP verification attempts before lockout.
44
+ * After exhausting attempts, user sees an error state.
45
+ * @default 3
46
+ */
47
+ maxOtpAttempts?: number;
48
+ };
49
+
50
+ export type AddEmailResponse = {
51
+ success: boolean;
52
+ };
53
+
54
+ export type VerifyOtpResponse = {
55
+ success: boolean;
56
+ };
57
+
58
+ export type AddEmailParams = {
59
+ email: string;
60
+ };
@@ -0,0 +1,20 @@
1
+ import { api } from '../http/api';
2
+ import { endpoints } from '../http/endpoints';
3
+ import type { Event } from './types';
4
+
5
+ export function addEvent(event: Event): Promise<void> {
6
+ const { code, module, screen, clientTimestamp, payload = {} } = event;
7
+
8
+ return api
9
+ .post(endpoints.events, [
10
+ {
11
+ code,
12
+ module,
13
+ screen,
14
+ clientTimestamp: clientTimestamp ?? Date.now(),
15
+ payload,
16
+ },
17
+ ])
18
+ .then(() => undefined)
19
+ .catch(() => undefined);
20
+ }
@@ -0,0 +1,7 @@
1
+ export type Event = {
2
+ code: string;
3
+ module?: string;
4
+ screen?: string;
5
+ payload?: Record<string, unknown>;
6
+ clientTimestamp?: number;
7
+ };
@@ -0,0 +1,84 @@
1
+ import { vi } from 'vitest';
2
+ import type { Flow, FlowModule } from '../types';
3
+
4
+ export const mockTutorialIdModule: FlowModule = {
5
+ key: 'TUTORIAL_ID',
6
+ configuration: {
7
+ showTutorial: true,
8
+ enableId: true,
9
+ enablePassport: false,
10
+ onlyFront: false,
11
+ onlyBack: false,
12
+ barcodeCapture: false,
13
+ fetchAdditionalPage: false,
14
+ secondId: false,
15
+ thirdId: false,
16
+ autoCaptureTimeout: 30,
17
+ deviceIdleTimeout: 60,
18
+ captureAttempts: 3,
19
+ manualUploadIdCapture: false,
20
+ digitalIdsUpload: false,
21
+ showDocumentChooserScreen: false,
22
+ enableIdRecording: false,
23
+ usSmartCapture: false,
24
+ perCountryPerDocOverrides: {},
25
+ },
26
+ };
27
+
28
+ export const mockSelfieModule: FlowModule = {
29
+ key: 'SELFIE',
30
+ configuration: {
31
+ showTutorial: false,
32
+ showPreview: true,
33
+ assistedOnboarding: false,
34
+ enableFaceRecording: false,
35
+ autoCaptureTimeout: 30,
36
+ captureAttempts: 3,
37
+ validateLenses: false,
38
+ validateFaceMask: false,
39
+ validateHeadCover: false,
40
+ validateClosedEyes: false,
41
+ validateBrightness: false,
42
+ deepsightLiveness: 'SINGLE_FRAME',
43
+ },
44
+ };
45
+
46
+ export const mockFaceMatchModule: FlowModule = {
47
+ key: 'FACE_MATCH',
48
+ configuration: {
49
+ matchingType: 'selfieVsId',
50
+ disableFaceMatchAnimation: false,
51
+ },
52
+ };
53
+
54
+ export const mockModules: FlowModule[] = [
55
+ mockTutorialIdModule,
56
+ mockSelfieModule,
57
+ mockFaceMatchModule,
58
+ ];
59
+
60
+ export function createMockFlow(modules: FlowModule[] = mockModules): Flow {
61
+ return {
62
+ flowId: 'test-flow-id',
63
+ name: 'Test Flow',
64
+ flowModules: modules,
65
+ };
66
+ }
67
+
68
+ export const mockFlow = createMockFlow();
69
+
70
+ export function createMockGetFlow(flow: Flow = mockFlow) {
71
+ return vi.fn().mockResolvedValue(flow);
72
+ }
73
+
74
+ export function createFailingGetFlow(error: string) {
75
+ return vi.fn().mockRejectedValue(new Error(error));
76
+ }
77
+
78
+ export function createDelayedGetFlow(flow: Flow = mockFlow, delayMs = 1000) {
79
+ return vi
80
+ .fn()
81
+ .mockImplementation(
82
+ () => new Promise((resolve) => setTimeout(() => resolve(flow), delayMs)),
83
+ );
84
+ }
@@ -0,0 +1,13 @@
1
+ import { createActor } from '@incodetech/infra';
2
+ import { getFlow as defaultGetFlow, type GetFlow } from './flowServices';
3
+ import { flowMachine } from './flowStateMachine';
4
+
5
+ export type CreateFlowActorOptions = {
6
+ getFlow?: GetFlow;
7
+ };
8
+
9
+ export function createFlowActor(options?: CreateFlowActorOptions) {
10
+ const getFlow = options?.getFlow ?? defaultGetFlow;
11
+
12
+ return createActor(flowMachine, { input: { getFlow } }).start();
13
+ }