@incodetech/core 0.0.0-dev-20260126-4504c5b

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.
@@ -0,0 +1,478 @@
1
+ import { d as addEvent, n as eventModuleNames, r as eventScreenNames } from "./events-B8ZkhAZo.esm.js";
2
+ import { C as createManager, d as BrowserTimerProvider } from "./src-DYtpbFY5.esm.js";
3
+ import { a as fromPromise, i as fromCallback, n as setup, o as createActor, r as assign, t as endpoints } from "./endpoints-BUsSVoJV.esm.js";
4
+ import { t as api } from "./api-DfRLAneb.esm.js";
5
+
6
+ //#region src/modules/email/emailServices.ts
7
+ async function fetchEmail(signal) {
8
+ const res = await api.get(endpoints.getEmail, { signal });
9
+ if (!res.ok) throw new Error(`GET ${endpoints.getEmail} failed: ${res.status} ${res.statusText}`);
10
+ return res.data;
11
+ }
12
+ async function addEmail(params, signal) {
13
+ const res = await api.post(endpoints.email, { email: params.email }, { signal });
14
+ if (!res.ok) throw new Error(`POST ${endpoints.email} failed: ${res.status} ${res.statusText}`);
15
+ return res.data;
16
+ }
17
+ async function sendEmailOtp(signal) {
18
+ const res = await api.get(`${endpoints.sendSmsOtp}?communicationchannel=EMAIL`, { signal });
19
+ if (!res.ok) throw new Error(`GET ${endpoints.sendSmsOtp} failed: ${res.status} ${res.statusText}`);
20
+ }
21
+ async function verifyEmailOtp(code, signal) {
22
+ const res = await api.get(`${endpoints.compareOtp}?code=${code}&channel=EMAIL`, { signal });
23
+ if (!res.ok) throw new Error(`GET ${endpoints.compareOtp} failed: ${res.status} ${res.statusText}`);
24
+ return res.data;
25
+ }
26
+
27
+ //#endregion
28
+ //#region src/modules/email/emailStateMachine.ts
29
+ const RESEND_TIMER_SECONDS = 30;
30
+ const emailMachine = setup({
31
+ types: {
32
+ context: {},
33
+ events: {},
34
+ input: {}
35
+ },
36
+ actors: {
37
+ fetchEmail: fromPromise(async ({ signal }) => {
38
+ return fetchEmail(signal);
39
+ }),
40
+ submitEmail: fromPromise(async ({ input, signal }) => {
41
+ return addEmail({ email: input.email }, signal);
42
+ }),
43
+ sendOtp: fromPromise(async ({ signal }) => {
44
+ return sendEmailOtp(signal);
45
+ }),
46
+ verifyOtp: fromPromise(async ({ input, signal }) => {
47
+ return verifyEmailOtp(input.code, signal);
48
+ }),
49
+ resendTimer: fromCallback(({ sendBack }) => {
50
+ const timer = BrowserTimerProvider.getInstance();
51
+ let seconds = RESEND_TIMER_SECONDS;
52
+ const interval = timer.setInterval(() => {
53
+ seconds -= 1;
54
+ sendBack({ type: "TICK" });
55
+ if (seconds <= 0) timer.clearInterval(interval);
56
+ }, 1e3);
57
+ return () => timer.clearInterval(interval);
58
+ })
59
+ },
60
+ actions: {
61
+ setPrefilledEmail: assign(({ event }) => {
62
+ const email = event.output.email;
63
+ return {
64
+ prefilledEmail: email,
65
+ email
66
+ };
67
+ }),
68
+ setEmail: assign(({ event }) => {
69
+ const e = event;
70
+ return {
71
+ email: e.email,
72
+ isValid: e.isValid,
73
+ emailError: e.isValid ? void 0 : "Invalid email address"
74
+ };
75
+ }),
76
+ setEmailError: assign(({ event }) => ({ emailError: String(event.error) })),
77
+ setError: assign(({ event }) => ({ error: String(event.error) })),
78
+ clearError: assign({ error: () => void 0 }),
79
+ clearEmailError: assign({ emailError: () => void 0 }),
80
+ setOtpCode: assign(({ event }) => ({
81
+ otpCode: event.code,
82
+ otpError: void 0
83
+ })),
84
+ setOtpError: assign(({ context, event }) => ({
85
+ otpError: String(event.error),
86
+ attemptsRemaining: context.attemptsRemaining - 1
87
+ })),
88
+ clearOtpError: assign({
89
+ otpError: () => void 0,
90
+ otpCode: () => ""
91
+ }),
92
+ startResendTimer: assign({
93
+ resendTimer: () => RESEND_TIMER_SECONDS,
94
+ resendTimerActive: () => true
95
+ }),
96
+ tickResendTimer: assign(({ context }) => {
97
+ const newTimer = Math.max(0, context.resendTimer - 1);
98
+ return {
99
+ resendTimer: newTimer,
100
+ resendTimerActive: newTimer > 0
101
+ };
102
+ }),
103
+ stopResendTimer: assign({ resendTimerActive: () => false }),
104
+ resetContext: assign(({ context }) => ({
105
+ config: context.config,
106
+ email: "",
107
+ isValid: false,
108
+ emailError: void 0,
109
+ prefilledEmail: void 0,
110
+ error: void 0,
111
+ otpCode: "",
112
+ otpError: void 0,
113
+ attemptsRemaining: context.config.maxOtpAttempts ?? 3,
114
+ resendTimer: 0,
115
+ resendTimerActive: false
116
+ })),
117
+ sendEmailSubmitEvent: () => {
118
+ addEvent({
119
+ code: "continue",
120
+ module: eventModuleNames.email,
121
+ screen: eventScreenNames.emailInput
122
+ });
123
+ }
124
+ },
125
+ guards: {
126
+ hasPrefill: ({ context }) => context.config.prefill,
127
+ hasOtpVerification: ({ context }) => context.config.otpVerification,
128
+ isValidEmail: ({ context }) => context.isValid,
129
+ hasAttemptsRemaining: ({ context }) => context.attemptsRemaining > 0,
130
+ canResend: ({ context }) => !context.resendTimerActive
131
+ }
132
+ }).createMachine({
133
+ id: "email",
134
+ initial: "idle",
135
+ context: ({ input }) => ({
136
+ config: input.config,
137
+ email: "",
138
+ isValid: false,
139
+ emailError: void 0,
140
+ prefilledEmail: void 0,
141
+ error: void 0,
142
+ otpCode: "",
143
+ otpError: void 0,
144
+ attemptsRemaining: input.config.maxOtpAttempts ?? 3,
145
+ resendTimer: 0,
146
+ resendTimerActive: false
147
+ }),
148
+ states: {
149
+ idle: { on: { LOAD: [{
150
+ target: "loadingPrefill",
151
+ guard: "hasPrefill"
152
+ }, { target: "inputting" }] } },
153
+ loadingPrefill: { invoke: {
154
+ id: "fetchEmail",
155
+ src: "fetchEmail",
156
+ onDone: {
157
+ target: "inputting",
158
+ actions: "setPrefilledEmail"
159
+ },
160
+ onError: { target: "inputting" }
161
+ } },
162
+ inputting: {
163
+ entry: "clearEmailError",
164
+ on: {
165
+ EMAIL_CHANGED: { actions: "setEmail" },
166
+ SUBMIT: {
167
+ target: "submitting",
168
+ guard: "isValidEmail"
169
+ }
170
+ }
171
+ },
172
+ submitting: { invoke: {
173
+ id: "submitEmail",
174
+ src: "submitEmail",
175
+ input: ({ context }) => ({ email: context.email }),
176
+ onDone: [{
177
+ target: "sendingOtp",
178
+ guard: "hasOtpVerification",
179
+ actions: "sendEmailSubmitEvent"
180
+ }, {
181
+ target: "finished",
182
+ actions: "sendEmailSubmitEvent"
183
+ }],
184
+ onError: {
185
+ target: "inputting",
186
+ actions: "setEmailError"
187
+ }
188
+ } },
189
+ sendingOtp: { invoke: {
190
+ id: "sendOtp",
191
+ src: "sendOtp",
192
+ onDone: { target: "awaitingOtp" },
193
+ onError: {
194
+ target: "awaitingOtp",
195
+ actions: "setError"
196
+ }
197
+ } },
198
+ awaitingOtp: {
199
+ entry: "startResendTimer",
200
+ invoke: {
201
+ id: "resendTimer",
202
+ src: "resendTimer"
203
+ },
204
+ on: {
205
+ TICK: { actions: "tickResendTimer" },
206
+ OTP_CHANGED: { actions: "setOtpCode" },
207
+ VERIFY_OTP: { target: "verifyingOtp" },
208
+ RESEND_OTP: {
209
+ target: "sendingOtp",
210
+ guard: "canResend"
211
+ },
212
+ BACK: { target: "inputting" }
213
+ }
214
+ },
215
+ verifyingOtp: { invoke: {
216
+ id: "verifyOtp",
217
+ src: "verifyOtp",
218
+ input: ({ context }) => ({ code: context.otpCode }),
219
+ onDone: [
220
+ {
221
+ target: "finished",
222
+ guard: ({ event }) => event.output.success === true
223
+ },
224
+ {
225
+ target: "otpError",
226
+ guard: "hasAttemptsRemaining",
227
+ actions: assign(({ context }) => ({
228
+ otpError: "Invalid OTP code",
229
+ attemptsRemaining: context.attemptsRemaining - 1
230
+ }))
231
+ },
232
+ {
233
+ target: "error",
234
+ actions: assign({ error: () => "Maximum OTP attempts exceeded" })
235
+ }
236
+ ],
237
+ onError: [{
238
+ target: "otpError",
239
+ guard: "hasAttemptsRemaining",
240
+ actions: "setOtpError"
241
+ }, {
242
+ target: "error",
243
+ actions: "setError"
244
+ }]
245
+ } },
246
+ otpError: { on: {
247
+ OTP_CHANGED: {
248
+ target: "awaitingOtp",
249
+ actions: "setOtpCode"
250
+ },
251
+ RESEND_OTP: {
252
+ target: "sendingOtp",
253
+ guard: "canResend"
254
+ },
255
+ BACK: { target: "inputting" }
256
+ } },
257
+ finished: { type: "final" },
258
+ error: { on: { RESET: {
259
+ target: "idle",
260
+ actions: "resetContext"
261
+ } } }
262
+ }
263
+ });
264
+
265
+ //#endregion
266
+ //#region src/modules/email/emailActor.ts
267
+ function createEmailActor(options) {
268
+ return createActor(emailMachine, { input: { config: options.config } }).start();
269
+ }
270
+
271
+ //#endregion
272
+ //#region src/modules/email/emailManager.ts
273
+ /**
274
+ * @module @incodetech/core/email
275
+ *
276
+ * Email verification module for the Incode Web SDK.
277
+ * Supports both headless (programmatic) and UI-driven usage patterns.
278
+ *
279
+ * ## Headless Usage
280
+ *
281
+ * The email manager can be used entirely without UI for backend integrations,
282
+ * custom UI implementations, or automated workflows.
283
+ *
284
+ * @example Basic headless email verification with OTP
285
+ * ```typescript
286
+ * import { createEmailManager } from '@incodetech/core/email';
287
+ * import { setup } from '@incodetech/core';
288
+ *
289
+ * // 1. Configure the SDK (required before using any module)
290
+ * setup({ apiURL: 'https://api.example.com', token: 'your-token' });
291
+ *
292
+ * // 2. Create the email manager
293
+ * const emailManager = createEmailManager({
294
+ * config: {
295
+ * otpVerification: true,
296
+ * otpExpirationInMinutes: 5,
297
+ * prefill: false,
298
+ * },
299
+ * });
300
+ *
301
+ * // 3. Subscribe to state changes (optional but recommended)
302
+ * emailManager.subscribe((state) => {
303
+ * console.log('Email state:', state.status);
304
+ * if (state.status === 'finished') {
305
+ * console.log('Email verified successfully!');
306
+ * }
307
+ * if (state.status === 'error') {
308
+ * console.error('Error:', state.error);
309
+ * }
310
+ * });
311
+ *
312
+ * // 4. Start the flow
313
+ * emailManager.load();
314
+ *
315
+ * // 5. When state is 'inputting', set the email address
316
+ * emailManager.setEmail('user@example.com', true);
317
+ *
318
+ * // 6. Submit the email address
319
+ * emailManager.submit();
320
+ *
321
+ * // 7. When state is 'awaitingOtp', submit the OTP code
322
+ * emailManager.submitOtp('ABC123');
323
+ *
324
+ * // 8. Clean up when done
325
+ * emailManager.stop();
326
+ * ```
327
+ *
328
+ * @example Polling-based headless usage
329
+ * ```typescript
330
+ * const emailManager = createEmailManager({ config });
331
+ *
332
+ * emailManager.load();
333
+ *
334
+ * // Poll for state changes
335
+ * const interval = setInterval(() => {
336
+ * const state = emailManager.getState();
337
+ *
338
+ * switch (state.status) {
339
+ * case 'inputting':
340
+ * emailManager.setEmail('user@example.com', true);
341
+ * emailManager.submit();
342
+ * break;
343
+ * case 'awaitingOtp':
344
+ * // Get OTP from user or external source
345
+ * emailManager.submitOtp(otpCode);
346
+ * break;
347
+ * case 'finished':
348
+ * clearInterval(interval);
349
+ * emailManager.stop();
350
+ * break;
351
+ * case 'error':
352
+ * clearInterval(interval);
353
+ * console.error(state.error);
354
+ * emailManager.stop();
355
+ * break;
356
+ * }
357
+ * }, 100);
358
+ * ```
359
+ */
360
+ function mapState(snapshot) {
361
+ const typedSnapshot = snapshot;
362
+ const { context } = typedSnapshot;
363
+ if (typedSnapshot.matches("idle")) return { status: "idle" };
364
+ if (typedSnapshot.matches("loadingPrefill")) return { status: "loadingPrefill" };
365
+ if (typedSnapshot.matches("inputting")) return {
366
+ status: "inputting",
367
+ prefilledEmail: context.prefilledEmail,
368
+ emailError: context.emailError
369
+ };
370
+ if (typedSnapshot.matches("submitting")) return { status: "submitting" };
371
+ if (typedSnapshot.matches("sendingOtp")) return { status: "sendingOtp" };
372
+ if (typedSnapshot.matches("awaitingOtp")) return {
373
+ status: "awaitingOtp",
374
+ resendTimer: context.resendTimer,
375
+ canResend: !context.resendTimerActive,
376
+ attemptsRemaining: context.attemptsRemaining
377
+ };
378
+ if (typedSnapshot.matches("verifyingOtp")) return { status: "verifyingOtp" };
379
+ if (typedSnapshot.matches("otpError")) return {
380
+ status: "otpError",
381
+ error: context.otpError ?? "Invalid OTP code",
382
+ attemptsRemaining: context.attemptsRemaining
383
+ };
384
+ if (typedSnapshot.matches("finished")) return { status: "finished" };
385
+ if (typedSnapshot.matches("error")) return {
386
+ status: "error",
387
+ error: context.error ?? "An error occurred"
388
+ };
389
+ return { status: "idle" };
390
+ }
391
+ function createApi({ actor }) {
392
+ return {
393
+ load() {
394
+ actor.send({ type: "LOAD" });
395
+ },
396
+ setEmail(email, isValid) {
397
+ actor.send({
398
+ type: "EMAIL_CHANGED",
399
+ email,
400
+ isValid
401
+ });
402
+ },
403
+ submit() {
404
+ actor.send({ type: "SUBMIT" });
405
+ },
406
+ setOtpCode(code) {
407
+ actor.send({
408
+ type: "OTP_CHANGED",
409
+ code
410
+ });
411
+ },
412
+ submitOtp(code) {
413
+ actor.send({
414
+ type: "OTP_CHANGED",
415
+ code
416
+ });
417
+ actor.send({ type: "VERIFY_OTP" });
418
+ },
419
+ resendOtp() {
420
+ actor.send({ type: "RESEND_OTP" });
421
+ },
422
+ back() {
423
+ actor.send({ type: "BACK" });
424
+ },
425
+ reset() {
426
+ actor.send({ type: "RESET" });
427
+ }
428
+ };
429
+ }
430
+ /**
431
+ * Creates an email verification manager for headless or UI-driven usage.
432
+ *
433
+ * The manager provides a state machine-based API for email address verification
434
+ * with optional OTP (one-time password) verification.
435
+ *
436
+ * @param options - Configuration options
437
+ * @param options.config - Email verification configuration
438
+ * @param options.config.otpVerification - Whether to require OTP verification
439
+ * @param options.config.otpExpirationInMinutes - How long the OTP is valid
440
+ * @param options.config.prefill - Whether to fetch a pre-filled email address
441
+ * @param options.config.maxOtpAttempts - Maximum OTP verification attempts (default: 3)
442
+ *
443
+ * @returns Email manager with state, API methods, and subscription
444
+ *
445
+ * @example Headless usage
446
+ * ```typescript
447
+ * const manager = createEmailManager({
448
+ * config: { otpVerification: true, otpExpirationInMinutes: 5, prefill: false },
449
+ * });
450
+ *
451
+ * manager.subscribe((state) => console.log(state.status));
452
+ * manager.load();
453
+ * manager.setEmail('user@example.com', true);
454
+ * manager.submit();
455
+ * // ... wait for 'awaitingOtp' state ...
456
+ * manager.submitOtp('ABC123');
457
+ * manager.stop();
458
+ * ```
459
+ *
460
+ * @example With React/Preact UI hook
461
+ * ```tsx
462
+ * const [state, manager] = useManager(() => createEmailManager({ config }));
463
+ *
464
+ * if (state.status === 'inputting') {
465
+ * return <input onChange={(e) => manager.setEmail(e.target.value, true)} />;
466
+ * }
467
+ * ```
468
+ */
469
+ function createEmailManager(options) {
470
+ return createManager({
471
+ actor: createEmailActor(options),
472
+ mapState,
473
+ createApi
474
+ });
475
+ }
476
+
477
+ //#endregion
478
+ export { createEmailManager, emailMachine };