@rilaykit/workflow 7.0.0 → 7.2.0

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/dist/index.d.mts CHANGED
@@ -5,6 +5,202 @@ export { form } from '@rilaykit/forms';
5
5
  import * as react_jsx_runtime from 'react/jsx-runtime';
6
6
  import React$1 from 'react';
7
7
 
8
+ interface WorkflowState {
9
+ currentStepIndex: number;
10
+ allData: Record<string, any>;
11
+ stepData: Record<string, any>;
12
+ visitedSteps: Set<string>;
13
+ isSubmitting: boolean;
14
+ isTransitioning: boolean;
15
+ isInitializing: boolean;
16
+ }
17
+ type WorkflowAction = {
18
+ type: 'SET_CURRENT_STEP';
19
+ stepIndex: number;
20
+ } | {
21
+ type: 'SET_STEP_DATA';
22
+ data: Record<string, any>;
23
+ stepId: string;
24
+ } | {
25
+ type: 'SET_ALL_DATA';
26
+ data: Record<string, any>;
27
+ } | {
28
+ type: 'SET_FIELD_VALUE';
29
+ fieldId: string;
30
+ value: any;
31
+ stepId: string;
32
+ } | {
33
+ type: 'SET_SUBMITTING';
34
+ isSubmitting: boolean;
35
+ } | {
36
+ type: 'SET_TRANSITIONING';
37
+ isTransitioning: boolean;
38
+ } | {
39
+ type: 'MARK_STEP_VISITED';
40
+ stepIndex: number;
41
+ stepId: string;
42
+ } | {
43
+ type: 'RESET_WORKFLOW';
44
+ } | {
45
+ type: 'LOAD_PERSISTED_STATE';
46
+ state: Partial<WorkflowState>;
47
+ } | {
48
+ type: 'SET_INITIALIZATION_COMPLETE';
49
+ };
50
+ interface UseWorkflowStateProps {
51
+ defaultValues?: Record<string, any>;
52
+ persistence?: {
53
+ workflowId: string;
54
+ adapter?: WorkflowPersistenceAdapter;
55
+ options?: PersistenceOptions;
56
+ userId?: string;
57
+ autoLoad?: boolean;
58
+ };
59
+ }
60
+ declare function useWorkflowState({ defaultValues, persistence }: UseWorkflowStateProps): {
61
+ workflowState: WorkflowState;
62
+ setCurrentStep: (stepIndex: number) => void;
63
+ setStepData: (data: Record<string, any>, stepId: string) => void;
64
+ setFieldValue: (fieldId: string, value: any, stepId: string) => void;
65
+ setSubmitting: (isSubmitting: boolean) => void;
66
+ setTransitioning: (isTransitioning: boolean) => void;
67
+ markStepVisited: (stepIndex: number, stepId: string) => void;
68
+ resetWorkflow: () => void;
69
+ loadPersistedState: () => Promise<boolean>;
70
+ persistence: {
71
+ isPersisting: boolean;
72
+ persistenceError: WorkflowPersistenceError | null;
73
+ persistNow: () => Promise<void>;
74
+ clearPersistedData: () => Promise<void>;
75
+ hasPersistedData: () => Promise<boolean>;
76
+ } | null;
77
+ };
78
+
79
+ /**
80
+ * @fileoverview Persistence system types for Rilay workflows
81
+ *
82
+ * This module defines the core interfaces and types for the workflow persistence system.
83
+ * It provides a flexible adapter pattern allowing different persistence strategies:
84
+ * - localStorage persistence
85
+ * - API-based persistence
86
+ * - Custom persistence implementations
87
+ *
88
+ * The system is designed to be type-safe and extensible while maintaining
89
+ * a simple API for common use cases.
90
+ */
91
+
92
+ /**
93
+ * Persistent workflow data that gets saved/loaded
94
+ */
95
+ interface PersistedWorkflowData {
96
+ /** Unique identifier for the workflow */
97
+ workflowId: string;
98
+ /** Current step index in the workflow */
99
+ currentStepIndex: number;
100
+ /** All collected data across steps */
101
+ allData: Record<string, any>;
102
+ /** Currently active step data */
103
+ stepData: Record<string, any>;
104
+ /** Set of visited step IDs */
105
+ visitedSteps: string[];
106
+ /** When this data was last saved */
107
+ lastSaved: number;
108
+ /** Optional metadata for custom persistence needs */
109
+ metadata?: Record<string, any>;
110
+ }
111
+ /**
112
+ * Options for persistence operations
113
+ */
114
+ interface PersistenceOptions {
115
+ /** Whether to automatically persist on state changes */
116
+ autoPersist?: boolean;
117
+ /** Debounce delay in ms for auto-persistence (default: 500ms) */
118
+ debounceMs?: number;
119
+ /** Custom key for storage (overrides default) */
120
+ storageKey?: string;
121
+ /** Additional metadata to include in persisted data */
122
+ metadata?: Record<string, any>;
123
+ }
124
+ /**
125
+ * Core persistence adapter interface
126
+ *
127
+ * All persistence implementations must implement this interface.
128
+ * It provides a consistent API regardless of the underlying storage mechanism.
129
+ */
130
+ interface WorkflowPersistenceAdapter {
131
+ /**
132
+ * Save workflow data
133
+ * @param key - Unique identifier for the stored data
134
+ * @param data - Workflow data to persist
135
+ * @returns Promise that resolves when save is complete
136
+ */
137
+ save(key: string, data: PersistedWorkflowData): Promise<void>;
138
+ /**
139
+ * Load workflow data
140
+ * @param key - Unique identifier for the stored data
141
+ * @returns Promise that resolves to persisted data or null if not found
142
+ */
143
+ load(key: string): Promise<PersistedWorkflowData | null>;
144
+ /**
145
+ * Remove persisted workflow data
146
+ * @param key - Unique identifier for the stored data
147
+ * @returns Promise that resolves when deletion is complete
148
+ */
149
+ remove(key: string): Promise<void>;
150
+ /**
151
+ * Check if data exists for the given key
152
+ * @param key - Unique identifier to check
153
+ * @returns Promise that resolves to true if data exists
154
+ */
155
+ exists(key: string): Promise<boolean>;
156
+ /**
157
+ * List all available keys (optional, for debugging/admin)
158
+ * @returns Promise that resolves to array of available keys
159
+ */
160
+ listKeys?(): Promise<string[]>;
161
+ /**
162
+ * Clear all persisted data (optional, for cleanup)
163
+ * @returns Promise that resolves when all data is cleared
164
+ */
165
+ clear?(): Promise<void>;
166
+ }
167
+ /**
168
+ * Configuration for localStorage persistence adapter
169
+ */
170
+ interface LocalStorageAdapterConfig {
171
+ /** Prefix for localStorage keys (default: 'rilay_workflow_') */
172
+ keyPrefix?: string;
173
+ /** Whether to compress data before storage */
174
+ compress?: boolean;
175
+ /** Maximum age in ms before data expires (optional) */
176
+ maxAge?: number;
177
+ }
178
+ /**
179
+ * Error types for persistence operations
180
+ */
181
+ declare class WorkflowPersistenceError extends Error {
182
+ readonly code: string;
183
+ readonly cause?: Error | undefined;
184
+ constructor(message: string, code: string, cause?: Error | undefined);
185
+ }
186
+ /**
187
+ * Persistence hook return type
188
+ */
189
+ interface UsePersistenceReturn {
190
+ /** Whether persistence is currently saving */
191
+ isPersisting: boolean;
192
+ /** Last persistence error (if any) */
193
+ persistenceError: WorkflowPersistenceError | null;
194
+ /** Manually trigger a save operation */
195
+ persistNow: () => Promise<void>;
196
+ /** Load data from persistence */
197
+ loadPersistedData: () => Promise<PersistedWorkflowData | null>;
198
+ /** Clear persisted data */
199
+ clearPersistedData: () => Promise<void>;
200
+ /** Whether persisted data exists */
201
+ hasPersistedData: () => Promise<boolean>;
202
+ }
203
+
8
204
  /**
9
205
  * Enhanced step configuration interface for better type safety and simplicity
10
206
  *
@@ -97,6 +293,12 @@ interface StepDefinition {
97
293
  interface WorkflowOptions {
98
294
  /** Analytics and tracking configuration */
99
295
  analytics?: WorkflowAnalytics;
296
+ /** Persistence configuration */
297
+ persistence?: {
298
+ adapter: WorkflowPersistenceAdapter;
299
+ options?: PersistenceOptions;
300
+ userId?: string;
301
+ };
100
302
  }
101
303
  /**
102
304
  * Workflow builder class for creating complex multi-step workflows
@@ -141,6 +343,7 @@ declare class flow {
141
343
  private workflowDescription?;
142
344
  private steps;
143
345
  private analytics?;
346
+ private persistenceConfig?;
144
347
  private plugins;
145
348
  private idGenerator;
146
349
  /**
@@ -231,17 +434,14 @@ declare class flow {
231
434
  * @example
232
435
  * ```typescript
233
436
  * workflow.configure({
234
- * navigation: {
235
- * allowBackNavigation: true,
236
- * showProgressBar: true
237
- * },
238
- * persistence: {
239
- * saveOnStepComplete: true,
240
- * storageKey: 'my-workflow'
241
- * },
242
437
  * analytics: {
243
438
  * trackStepCompletion: true,
244
439
  * trackFieldInteractions: false
440
+ * },
441
+ * persistence: {
442
+ * adapter: new LocalStorageAdapter(),
443
+ * options: { autoPersist: true, debounceMs: 1000 },
444
+ * userId: 'user123'
245
445
  * }
246
446
  * });
247
447
  * ```
@@ -574,197 +774,50 @@ declare function useMultipleConditionEvaluation(fieldsWithConditions: Record<str
574
774
  declare function useMultipleStepConditionEvaluation(stepsWithConditions: Record<number, ConditionalBehavior | undefined>, workflowData?: Record<string, any>): Record<number, ConditionEvaluationResult>;
575
775
 
576
776
  /**
577
- * @fileoverview Persistence system types for Rilay workflows
578
- *
579
- * This module defines the core interfaces and types for the workflow persistence system.
580
- * It provides a flexible adapter pattern allowing different persistence strategies:
581
- * - localStorage persistence
582
- * - API-based persistence
583
- * - Custom persistence implementations
777
+ * @fileoverview Persistence hook for Rilay workflows
584
778
  *
585
- * The system is designed to be type-safe and extensible while maintaining
586
- * a simple API for common use cases.
779
+ * This hook provides workflow persistence functionality with automatic
780
+ * debounced saving, error handling, and flexible adapter support.
781
+ * It integrates seamlessly with the existing workflow state management.
587
782
  */
588
783
 
589
- /**
590
- * Persistent workflow data that gets saved/loaded
591
- */
592
- interface PersistedWorkflowData {
593
- /** Unique identifier for the workflow */
784
+ interface UsePersistenceProps {
785
+ /** Unique workflow identifier */
594
786
  workflowId: string;
595
- /** Current step index in the workflow */
596
- currentStepIndex: number;
597
- /** All collected data across steps */
598
- allData: Record<string, any>;
599
- /** Currently active step data */
600
- stepData: Record<string, any>;
601
- /** Set of visited step IDs */
602
- visitedSteps: string[];
603
- /** When this data was last saved */
604
- lastSaved: number;
605
- /** Optional metadata for custom persistence needs */
606
- metadata?: Record<string, any>;
607
- }
608
- /**
609
- * Options for persistence operations
610
- */
611
- interface PersistenceOptions {
612
- /** Whether to automatically persist on state changes */
613
- autoPersist?: boolean;
614
- /** Debounce delay in ms for auto-persistence (default: 500ms) */
615
- debounceMs?: number;
616
- /** Custom key for storage (overrides default) */
617
- storageKey?: string;
618
- /** Additional metadata to include in persisted data */
619
- metadata?: Record<string, any>;
787
+ /** Current workflow state */
788
+ workflowState: WorkflowState;
789
+ /** Persistence adapter to use */
790
+ adapter: WorkflowPersistenceAdapter;
791
+ /** Persistence options */
792
+ options?: PersistenceOptions;
793
+ /** Optional user ID for multi-user scenarios */
794
+ userId?: string;
620
795
  }
621
796
  /**
622
- * Core persistence adapter interface
797
+ * Hook for managing workflow persistence
623
798
  *
624
- * All persistence implementations must implement this interface.
625
- * It provides a consistent API regardless of the underlying storage mechanism.
626
- */
627
- interface WorkflowPersistenceAdapter {
628
- /**
629
- * Save workflow data
630
- * @param key - Unique identifier for the stored data
631
- * @param data - Workflow data to persist
632
- * @returns Promise that resolves when save is complete
633
- */
634
- save(key: string, data: PersistedWorkflowData): Promise<void>;
635
- /**
636
- * Load workflow data
637
- * @param key - Unique identifier for the stored data
638
- * @returns Promise that resolves to persisted data or null if not found
639
- */
640
- load(key: string): Promise<PersistedWorkflowData | null>;
641
- /**
642
- * Remove persisted workflow data
643
- * @param key - Unique identifier for the stored data
644
- * @returns Promise that resolves when deletion is complete
645
- */
646
- remove(key: string): Promise<void>;
647
- /**
648
- * Check if data exists for the given key
649
- * @param key - Unique identifier to check
650
- * @returns Promise that resolves to true if data exists
651
- */
652
- exists(key: string): Promise<boolean>;
653
- /**
654
- * List all available keys (optional, for debugging/admin)
655
- * @returns Promise that resolves to array of available keys
656
- */
657
- listKeys?(): Promise<string[]>;
658
- /**
659
- * Clear all persisted data (optional, for cleanup)
660
- * @returns Promise that resolves when all data is cleared
661
- */
662
- clear?(): Promise<void>;
663
- }
664
- /**
665
- * Configuration for localStorage persistence adapter
666
- */
667
- interface LocalStorageAdapterConfig {
668
- /** Prefix for localStorage keys (default: 'rilay_workflow_') */
669
- keyPrefix?: string;
670
- /** Whether to compress data before storage */
671
- compress?: boolean;
672
- /** Maximum age in ms before data expires (optional) */
673
- maxAge?: number;
674
- }
675
- /**
676
- * Error types for persistence operations
677
- */
678
- declare class WorkflowPersistenceError extends Error {
679
- readonly code: string;
680
- readonly cause?: Error | undefined;
681
- constructor(message: string, code: string, cause?: Error | undefined);
682
- }
683
- /**
684
- * Persistence hook return type
799
+ * Provides automatic persistence with debouncing, manual save/load operations,
800
+ * and comprehensive error handling. Integrates with any persistence adapter
801
+ * that implements the WorkflowPersistenceAdapter interface.
802
+ *
803
+ * @example
804
+ * ```typescript
805
+ * const persistence = usePersistence({
806
+ * workflowId: 'user-onboarding',
807
+ * workflowState,
808
+ * adapter: new LocalStorageAdapter(),
809
+ * options: {
810
+ * autoPersist: true,
811
+ * debounceMs: 1000
812
+ * }
813
+ * });
814
+ *
815
+ * if (persistence.persistenceError) {
816
+ * console.error('Persistence error:', persistence.persistenceError);
817
+ * }
818
+ * ```
685
819
  */
686
- interface UsePersistenceReturn {
687
- /** Whether persistence is currently saving */
688
- isPersisting: boolean;
689
- /** Last persistence error (if any) */
690
- persistenceError: WorkflowPersistenceError | null;
691
- /** Manually trigger a save operation */
692
- persistNow: () => Promise<void>;
693
- /** Load data from persistence */
694
- loadPersistedData: () => Promise<PersistedWorkflowData | null>;
695
- /** Clear persisted data */
696
- clearPersistedData: () => Promise<void>;
697
- /** Whether persisted data exists */
698
- hasPersistedData: () => Promise<boolean>;
699
- }
700
-
701
- interface WorkflowState {
702
- currentStepIndex: number;
703
- allData: Record<string, any>;
704
- stepData: Record<string, any>;
705
- visitedSteps: Set<string>;
706
- isSubmitting: boolean;
707
- isTransitioning: boolean;
708
- }
709
- type WorkflowAction = {
710
- type: 'SET_CURRENT_STEP';
711
- stepIndex: number;
712
- } | {
713
- type: 'SET_STEP_DATA';
714
- data: Record<string, any>;
715
- stepId: string;
716
- } | {
717
- type: 'SET_ALL_DATA';
718
- data: Record<string, any>;
719
- } | {
720
- type: 'SET_FIELD_VALUE';
721
- fieldId: string;
722
- value: any;
723
- stepId: string;
724
- } | {
725
- type: 'SET_SUBMITTING';
726
- isSubmitting: boolean;
727
- } | {
728
- type: 'SET_TRANSITIONING';
729
- isTransitioning: boolean;
730
- } | {
731
- type: 'MARK_STEP_VISITED';
732
- stepIndex: number;
733
- stepId: string;
734
- } | {
735
- type: 'RESET_WORKFLOW';
736
- } | {
737
- type: 'LOAD_PERSISTED_STATE';
738
- state: Partial<WorkflowState>;
739
- };
740
- interface UseWorkflowStateProps {
741
- defaultValues?: Record<string, any>;
742
- persistence?: {
743
- workflowId: string;
744
- adapter?: WorkflowPersistenceAdapter;
745
- options?: PersistenceOptions;
746
- userId?: string;
747
- autoLoad?: boolean;
748
- };
749
- }
750
- declare function useWorkflowState({ defaultValues, persistence }: UseWorkflowStateProps): {
751
- workflowState: WorkflowState;
752
- setCurrentStep: (stepIndex: number) => void;
753
- setStepData: (data: Record<string, any>, stepId: string) => void;
754
- setFieldValue: (fieldId: string, value: any, stepId: string) => void;
755
- setSubmitting: (isSubmitting: boolean) => void;
756
- setTransitioning: (isTransitioning: boolean) => void;
757
- markStepVisited: (stepIndex: number, stepId: string) => void;
758
- resetWorkflow: () => void;
759
- loadPersistedState: () => Promise<boolean>;
760
- persistence: {
761
- isPersisting: boolean;
762
- persistenceError: WorkflowPersistenceError | null;
763
- persistNow: () => Promise<void>;
764
- clearPersistedData: () => Promise<void>;
765
- hasPersistedData: () => Promise<boolean>;
766
- } | null;
767
- };
820
+ declare function usePersistence({ workflowId, workflowState, adapter, options, userId, }: UsePersistenceProps): UsePersistenceReturn;
768
821
 
769
822
  interface UseWorkflowAnalyticsProps {
770
823
  workflowConfig: WorkflowConfig;
@@ -872,6 +925,9 @@ interface WorkflowContextValue {
872
925
  submitWorkflow: () => Promise<void>;
873
926
  isSubmitting: boolean;
874
927
  canSubmit: boolean;
928
+ persistNow?: () => Promise<void>;
929
+ isPersisting?: boolean;
930
+ persistenceError?: Error | null;
875
931
  }
876
932
  interface WorkflowProviderProps {
877
933
  children: React$1.ReactNode;
@@ -949,6 +1005,7 @@ declare class LocalStorageAdapter implements WorkflowPersistenceAdapter {
949
1005
  private readonly compress;
950
1006
  private readonly maxAge?;
951
1007
  private readonly version;
1008
+ private readonly _isAvailable;
952
1009
  constructor(config?: LocalStorageAdapterConfig);
953
1010
  /**
954
1011
  * Save workflow data to localStorage
@@ -1030,52 +1087,6 @@ declare function debounce<T extends (...args: any[]) => any>(func: T, wait: numb
1030
1087
  */
1031
1088
  declare function mergePersistedState(currentState: WorkflowState, persistedData: PersistedWorkflowData, strategy?: 'persist' | 'current' | 'merge'): Partial<WorkflowState>;
1032
1089
 
1033
- /**
1034
- * @fileoverview Persistence hook for Rilay workflows
1035
- *
1036
- * This hook provides workflow persistence functionality with automatic
1037
- * debounced saving, error handling, and flexible adapter support.
1038
- * It integrates seamlessly with the existing workflow state management.
1039
- */
1040
-
1041
- interface UsePersistenceProps {
1042
- /** Unique workflow identifier */
1043
- workflowId: string;
1044
- /** Current workflow state */
1045
- workflowState: WorkflowState;
1046
- /** Persistence adapter to use */
1047
- adapter: WorkflowPersistenceAdapter;
1048
- /** Persistence options */
1049
- options?: PersistenceOptions;
1050
- /** Optional user ID for multi-user scenarios */
1051
- userId?: string;
1052
- }
1053
- /**
1054
- * Hook for managing workflow persistence
1055
- *
1056
- * Provides automatic persistence with debouncing, manual save/load operations,
1057
- * and comprehensive error handling. Integrates with any persistence adapter
1058
- * that implements the WorkflowPersistenceAdapter interface.
1059
- *
1060
- * @example
1061
- * ```typescript
1062
- * const persistence = usePersistence({
1063
- * workflowId: 'user-onboarding',
1064
- * workflowState,
1065
- * adapter: new LocalStorageAdapter(),
1066
- * options: {
1067
- * autoPersist: true,
1068
- * debounceMs: 1000
1069
- * }
1070
- * });
1071
- *
1072
- * if (persistence.persistenceError) {
1073
- * console.error('Persistence error:', persistence.persistenceError);
1074
- * }
1075
- * ```
1076
- */
1077
- declare function usePersistence({ workflowId, workflowState, adapter, options, userId, }: UsePersistenceProps): UsePersistenceReturn;
1078
-
1079
1090
  interface LicensePayload {
1080
1091
  plan: 'ARCHITECT' | 'FOUNDRY';
1081
1092
  company: string;