@umituz/react-native-storage 2.6.22 → 2.6.24

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,128 @@
1
+ # Domain Layer
2
+
3
+ Core business logic, entities, value objects, and error handling for storage operations.
4
+
5
+ ## Overview
6
+
7
+ The domain layer contains the fundamental business logic and types used throughout the storage system. Located at `src/domain/`.
8
+
9
+ ## Architecture
10
+
11
+ ```
12
+ domain/
13
+ ├── constants/ # Time constants and defaults
14
+ ├── entities/ # Domain entities (CachedValue, Result)
15
+ ├── errors/ # Error classes and types
16
+ ├── factories/ # Zustand store factory
17
+ ├── types/ # TypeScript type definitions
18
+ ├── utils/ # Helper functions
19
+ └── value-objects/ # StorageKey value objects
20
+ ```
21
+
22
+ ## Strategies
23
+
24
+ ### Value Objects
25
+ - Use StorageKey for type-safe storage keys
26
+ - Create scoped keys (user, organization, app)
27
+ - Use key factories for consistent key generation
28
+ - Document key structure patterns
29
+
30
+ ### Error Handling
31
+ - Use specific error types for different failure scenarios
32
+ - Apply Result pattern for type-safe error handling
33
+ - Preserve error context (key, operation, cause)
34
+ - Categorize errors by type (read, write, serialization)
35
+
36
+ ### Store Factory
37
+ - Use createStore for Zustand store creation
38
+ - Implement persist for state persistence
39
+ - Use versioning and migration for schema changes
40
+ - Apply partialize to select persisted state
41
+
42
+ ### Key Generation
43
+ - Use generateCacheKey for cache entries
44
+ - Use generateListCacheKey for list queries
45
+ - Document key patterns in code comments
46
+ - Use parseCacheKey for key extraction
47
+
48
+ ## Restrictions
49
+
50
+ ### Value Objects
51
+ - DO NOT create keys without using factory functions
52
+ - DO NOT mix key scopes (user vs app vs org)
53
+ - DO NOT use dynamic strings as storage keys
54
+ - DO NOT create ambiguous key structures
55
+
56
+ ### Error Handling
57
+ - DO NOT throw exceptions from Result types
58
+ - DO NOT use generic Error class
59
+ - DO NOT lose error context when wrapping errors
60
+ - DO NOT ignore error states in Result objects
61
+
62
+ ### Store Factory
63
+ - DO NOT create stores without names
64
+ - DO NOT use persist without version number
65
+ - DO NOT change schema without migration
66
+ - DO NOT persist sensitive data without encryption
67
+
68
+ ### Type Safety
69
+ - DO NOT use `any` for type parameters
70
+ - DO NOT skip type guards for runtime validation
71
+ - DO NOT assume Result is always success
72
+ - DO NOT cast values without validation
73
+
74
+ ## Rules
75
+
76
+ ### Value Objects (StorageKey)
77
+ - MUST use factory functions for key creation
78
+ - MUST use consistent key structure (prefix:params)
79
+ - MUST include scope prefix (app:, user:, org:)
80
+ - MUST document key patterns
81
+
82
+ ### Error Classes
83
+ - MUST extend StorageError base class
84
+ - MUST include error code
85
+ - MUST preserve error context (key, operation)
86
+ - MUST attach cause error when wrapping exceptions
87
+
88
+ ### Result Pattern
89
+ - MUST use success() for successful operations
90
+ - MUST use failure() for errors
91
+ - MUST check success flag before accessing data
92
+ - MUST provide type parameter for data
93
+
94
+ ### Store Factory
95
+ - MUST provide unique store name
96
+ - MUST define initial state
97
+ - MUST specify version when persist is enabled
98
+ - MUST implement migrate function for schema changes
99
+
100
+ ### Type Definitions
101
+ - MUST use generic type parameters
102
+ - MUST provide type inference
103
+ - MUST enforce type safety
104
+ - MUST document complex types
105
+
106
+ ### Constants
107
+ - MUST use TIME_MS for time values
108
+ - MUST use DEFAULT_TTL for cache TTL
109
+ - MUST define constants in separate files
110
+ - MUST export constants for external use
111
+
112
+ ### Utilities
113
+ - MUST validate input parameters
114
+ - MUST handle edge cases gracefully
115
+ - MUST provide type guards for validation
116
+ - MUST log warnings in development
117
+
118
+ ### Testing
119
+ - MUST test error scenarios
120
+ - MUST test Result pattern operations
121
+ - MUST validate key generation
122
+ - MUST test store creation and persistence
123
+
124
+ ### Documentation
125
+ - MUST document complex types
126
+ - MUST specify error codes
127
+ - MUST provide usage examples in comments
128
+ - MUST warn about common mistakes
@@ -0,0 +1,105 @@
1
+ # Domain Constants
2
+
3
+ Time constants and default values for storage and cache operations.
4
+
5
+ ## Overview
6
+
7
+ Constant values for time-based calculations and default configuration. Located at `src/domain/constants/`.
8
+
9
+ ## Strategies
10
+
11
+ ### Time Constants
12
+ - Use TIME_MS for all time-based calculations
13
+ - Provide constants for common time units
14
+ - Enable readable time calculations
15
+ - Support time arithmetic
16
+
17
+ ### Default Values
18
+ - Use DEFAULT_TTL for standard cache lifetime
19
+ - Set sensible defaults for common use cases
20
+ - Document default value rationale
21
+ - Allow override for specific needs
22
+
23
+ ### Version Management
24
+ - Use CACHE_VERSION for cache key versioning
25
+ - Increment version on breaking changes
26
+ - Support migration between versions
27
+ - Include version in cache keys
28
+
29
+ ## Restrictions
30
+
31
+ ### Time Calculations
32
+ - DO NOT use hardcoded millisecond values
33
+ - DO NOT calculate time values inline
34
+ - DO NOT mix time units (seconds vs milliseconds)
35
+ - DO NOT use magic numbers for time
36
+
37
+ ### Constants
38
+ - DO NOT redefine TIME_MS values elsewhere
39
+ - DO NOT modify constants at runtime
40
+ - DO NOT use deprecated constants
41
+ - DO NOT create duplicate constants
42
+
43
+ ### Version Management
44
+ - DO NOT skip version numbers
45
+ - DO NOT reuse old versions
46
+ - DO NOT forget to update CACHE_VERSION
47
+ - DO NOT change version format
48
+
49
+ ## Rules
50
+
51
+ ### TIME_MS Constants
52
+ - MUST provide SECOND (1000ms)
53
+ - MUST provide MINUTE (60000ms)
54
+ - MUST provide HOUR (3600000ms)
55
+ - MUST provide DAY (86400000ms)
56
+ - MUST provide WEEK (604800000ms)
57
+ - MUST be in milliseconds
58
+
59
+ ### DEFAULT_TTL
60
+ - MUST be set to 300000 (5 minutes)
61
+ - MUST be used for cache when no TTL specified
62
+ - MUST balance freshness and performance
63
+ - MUST be documented
64
+
65
+ ### CACHE_VERSION
66
+ - MUST be integer value
67
+ - MUST start at 1
68
+ - MUST increment on breaking changes
69
+ - MUST be included in cache keys
70
+
71
+ ### Constant Usage
72
+ - MUST use TIME_MS for all time calculations
73
+ - MUST use DEFAULT_TTL for default cache lifetime
74
+ - MUST use CACHE_VERSION for versioning
75
+ - MUST not calculate values inline
76
+
77
+ ### Time Arithmetic
78
+ - MUST use TIME_MS constants for calculations
79
+ - MUST support multiplication for extended periods
80
+ - MUST be readable and maintainable
81
+ - MUST avoid magic numbers
82
+
83
+ ### Version Control
84
+ - MUST increment CACHE_VERSION on schema changes
85
+ - MUST implement migration for old versions
86
+ - MUST document breaking changes
87
+ - MUST support backward compatibility where possible
88
+
89
+ ### Export Rules
90
+ - MUST export all constants
91
+ - MUST use const for immutability
92
+ - MUST organize constants by purpose
93
+ - MUST provide TypeScript types
94
+
95
+ ### Documentation
96
+ - MUST document time units clearly
97
+ - MUST specify values in milliseconds
98
+ - MUST explain default value choices
99
+ - MUST warn about common mistakes
100
+
101
+ ### Testing Requirements
102
+ - MUST test time constant values
103
+ - MUST verify version increment logic
104
+ - MUST test migration between versions
105
+ - MUST validate default TTL usage
@@ -0,0 +1,109 @@
1
+ # Domain Entities
2
+
3
+ Core entities for the storage domain including CachedValue and Result pattern.
4
+
5
+ ## Overview
6
+
7
+ Fundamental domain entities used throughout the storage system. Located at `src/domain/entities/`.
8
+
9
+ ## Strategies
10
+
11
+ ### CachedValue Entity
12
+ - Use for cache entries with TTL and metadata
13
+ - Track creation timestamp for expiration
14
+ - Support TTL-based expiration checking
15
+ - Enable age calculation and remaining TTL queries
16
+
17
+ ### Result Pattern
18
+ - Use discriminated union for type-safe error handling
19
+ - Provide success/failure states with type narrowing
20
+ - Include error context in failure state
21
+ - Support functional transformations (map, unwrap)
22
+
23
+ ### Entity Composition
24
+ - Combine CachedValue with cache operations
25
+ - Use Result for all storage operations
26
+ - Enable error propagation without exceptions
27
+ - Support functional error handling patterns
28
+
29
+ ## Restrictions
30
+
31
+ ### CachedValue
32
+ - DO NOT modify timestamp after creation (except for sliding expiration)
33
+ - DO NOT adjust TTL manually to extend lifetime
34
+ - DO NOT create CachedValue without factory function
35
+ - DO NOT use negative or zero TTL values
36
+
37
+ ### Result Pattern
38
+ - DO NOT throw exceptions from Result operations
39
+ - DO NOT access data without checking success flag
40
+ - DO NOT ignore failure states
41
+ - DO NOT mix success and failure in same type
42
+
43
+ ### Type Safety
44
+ - DO NOT use `any` for Result data type
45
+ - DO NOT skip type guards when checking results
46
+ - DO NOT cast results without validation
47
+ - DO NOT create circular type dependencies
48
+
49
+ ## Rules
50
+
51
+ ### CachedValue Entity
52
+ - MUST use generic type parameter <T> for data
53
+ - MUST include timestamp (milliseconds since epoch)
54
+ - MUST include ttl (time to live in milliseconds)
55
+ - MUST provide factory function for creation
56
+ - MUST validate structure on creation
57
+
58
+ ### Result Type
59
+ - MUST use discriminated union with success flag
60
+ - MUST include data in success state
61
+ - MUST include error in failure state
62
+ - MUST provide type parameter for data type
63
+ - MUST enable type narrowing with success flag
64
+
65
+ ### Factory Functions
66
+ - MUST provide createCachedValue(data, ttl)
67
+ - MUST provide success(data) factory
68
+ - MUST provide failure(error) factory
69
+ - MUST validate input parameters
70
+ - MUST return properly typed entities
71
+
72
+ ### Type Guards
73
+ - MUST provide isSuccess() type guard
74
+ - MUST provide isFailure() type guard
75
+ - MUST provide isCacheExpired() validator
76
+ - MUST use predicate return type for type narrowing
77
+ - MUST validate all required fields
78
+
79
+ ### Utility Functions
80
+ - MUST provide getRemainingTTL() for cache entries
81
+ - MUST provide getCacheAge() for cache entries
82
+ - MUST provide unwrap() for Result extraction
83
+ - MUST provide map() for Result transformation
84
+ - MUST handle edge cases (null, undefined)
85
+
86
+ ### Error Handling
87
+ - MUST preserve error context in failures
88
+ - MUST attach cause errors when wrapping
89
+ - MUST include error codes for categorization
90
+ - MUST log errors in development mode
91
+
92
+ ### Serialization
93
+ - MUST support JSON serialization for CachedValue
94
+ - MUST validate structure after deserialization
95
+ - MUST handle circular reference errors
96
+ - MUST provide type guards for validation
97
+
98
+ ### Testing Requirements
99
+ - MUST test entity creation and validation
100
+ - MUST test expiration logic with various TTL values
101
+ - MUST test Result pattern operations
102
+ - MUST test type guard behavior
103
+ - MUST test edge cases
104
+
105
+ ### Documentation
106
+ - MUST document all public methods
107
+ - MUST specify parameter types and return types
108
+ - MUST provide usage guidance without code examples
109
+ - MUST warn about common mistakes
@@ -0,0 +1,126 @@
1
+ # Storage Errors
2
+
3
+ Error classes and error handling strategies for storage operations.
4
+
5
+ ## Overview
6
+
7
+ Hierarchical error classes for storage operations with context preservation. Located at `src/domain/errors/`.
8
+
9
+ ## Strategies
10
+
11
+ ### Error Hierarchy
12
+ - Use StorageError as base class for all storage errors
13
+ - Create specific error types for different operations
14
+ - Preserve error context (key, operation, cause)
15
+ - Enable error type checking with instanceof
16
+
17
+ ### Error Categories
18
+ - StorageReadError for read failures
19
+ - StorageWriteError for write failures
20
+ - StorageDeleteError for delete failures
21
+ - StorageSerializationError for serialization failures
22
+ - StorageDeserializationError for parsing failures
23
+
24
+ ### Error Handling Strategy
25
+ - Use Result pattern instead of throwing exceptions
26
+ - Preserve original error as cause property
27
+ - Include error codes for programmatic handling
28
+ - Log errors with full context
29
+
30
+ ### Error Recovery
31
+ - Implement retry logic for transient failures
32
+ - Provide fallback values for graceful degradation
33
+ - Clear corrupted data when appropriate
34
+ - Alert monitoring systems for critical errors
35
+
36
+ ## Restrictions
37
+
38
+ ### Error Creation
39
+ - DO NOT throw generic Error class
40
+ - DO NOT lose error context when wrapping
41
+ - DO NOT create error types without clear purpose
42
+ - DO NOT use strings for error codes
43
+
44
+ ### Error Handling
45
+ - DO NOT catch and ignore errors silently
46
+ - DO NOT suppress error logging in production
47
+ - DO NOT retry indefinitely on persistent failures
48
+ - DO NOT expose sensitive data in error messages
49
+
50
+ ### Error Types
51
+ - DO NOT mix different error categories
52
+ - DO NOT create circular error hierarchies
53
+ - DO NOT omit error code from custom errors
54
+ - DO NOT cause infinite error loops
55
+
56
+ ## Rules
57
+
58
+ ### Error Base Class
59
+ - MUST extend Error class
60
+ - MUST include code property (string)
61
+ - MUST preserve stack trace
62
+ - MUST accept cause parameter for wrapping
63
+ - MUST include message parameter
64
+
65
+ ### Specific Error Types
66
+ - MUST extend StorageError base class
67
+ - MUST include key property for storage errors
68
+ - MUST include cause property when wrapping
69
+ - MUST use specific error codes
70
+ - MUST set appropriate error name
71
+
72
+ ### Error Codes
73
+ - MUST use format: CATEGORY_ERROR (e.g., STORAGE_READ_ERROR)
74
+ - MUST be unique across error types
75
+ - MUST be descriptive and searchable
76
+ - MUST follow naming convention
77
+ - MUST be documented
78
+
79
+ ### Error Context
80
+ - MUST include storage key in storage errors
81
+ - MUST include original operation type
82
+ - MUST attach cause error when wrapping
83
+ - MUST preserve stack trace
84
+ - MUST include timestamp for debugging
85
+
86
+ ### Error Handling Patterns
87
+ - MUST use try-catch for all storage operations
88
+ - MUST check error types with instanceof
89
+ - MUST log errors with full context
90
+ - MUST provide error recovery where possible
91
+ - MUST not expose sensitive data
92
+
93
+ ### Custom Error Creation
94
+ - MUST extend StorageError for custom errors
95
+ - MUST provide unique error code
96
+ - MUST include all required context
97
+ - MUST document error purpose
98
+ - MUST follow naming conventions
99
+
100
+ ### Error Logging
101
+ - MUST log error code and message
102
+ - MUST log error context (key, operation)
103
+ - MUST log cause error if present
104
+ - MUST log stack trace in development
105
+ - MUST not log sensitive data
106
+
107
+ ### Error Testing
108
+ - MUST test error creation
109
+ - MUST test error type checking
110
+ - MUST test error context preservation
111
+ - MUST test error logging
112
+ - MUST test error recovery logic
113
+
114
+ ### Type Safety
115
+ - MUST use instanceof for type checking
116
+ - MUST use type guards where appropriate
117
+ - MUST enforce error codes as literal types
118
+ - MUST provide TypeScript types
119
+ - MUST not use `any` for error types
120
+
121
+ ### Error Recovery
122
+ - MUST implement retry limits
123
+ - MUST provide fallback strategies
124
+ - MUST clear corrupted data when detected
125
+ - MUST alert on critical failures
126
+ - MUST document recovery behavior
@@ -0,0 +1,138 @@
1
+ # Domain Factories
2
+
3
+ Factory functions for creating Zustand stores with persistence.
4
+
5
+ ## Overview
6
+
7
+ Factory functions for creating Zustand stores with AsyncStorage persistence. Located at `src/domain/factories/`.
8
+
9
+ ## Strategies
10
+
11
+ ### Store Creation
12
+ - Use createStore() factory for all Zustand stores
13
+ - Provide type inference for state and actions
14
+ - Support optional persistence
15
+ - Enable store composition
16
+
17
+ ### Persistence Strategy
18
+ - Use persist middleware for AsyncStorage integration
19
+ - Configure storage adapter (StorageService)
20
+ - Support selective persistence (partialize)
21
+ - Enable versioning and migration
22
+
23
+ ### Store Configuration
24
+ - Require unique store name
25
+ - Define initial state clearly
26
+ - Create actions with set/get helpers
27
+ - Support middleware configuration
28
+
29
+ ### Type Safety
30
+ - Infer state types from initial state
31
+ - Separate state and actions types
32
+ - Enable type-safe selectors
33
+ - Support generic store types
34
+
35
+ ## Restrictions
36
+
37
+ ### Store Creation
38
+ - DO NOT create stores without factory
39
+ - DO NOT duplicate store names
40
+ - DO NOT mix state and actions unnecessarily
41
+ - DO NOT create stores without clear purpose
42
+
43
+ ### Persistence
44
+ - DO NOT enable persist without unique name
45
+ - DO NOT persist sensitive data without encryption
46
+ - DO NOT forget version when schema changes
47
+ - DO NOT omit migration for breaking changes
48
+
49
+ ### Type Safety
50
+ - DO NOT use `any` for state type
51
+ - DO NOT bypass type inference
52
+ - DO NOT cast state to wrong type
53
+ - DO NOT create circular type dependencies
54
+
55
+ ## Rules
56
+
57
+ ### Factory Function
58
+ - MUST provide createStore(config) function
59
+ - MUST accept name parameter (required)
60
+ - MUST accept initialState parameter (required)
61
+ - MUST accept actions parameter (optional)
62
+ - MUST accept persist parameter (optional)
63
+
64
+ ### Store Configuration
65
+ - MUST require unique store name
66
+ - MUST define complete initial state
67
+ - MUST provide type inference
68
+ - MUST validate configuration
69
+ - MUST return configured store
70
+
71
+ ### Persistence Setup
72
+ - MUST use zustand persist middleware
73
+ - MUST use storageService adapter
74
+ - MUST include store name in config
75
+ - MUST support version parameter
76
+ - MUST support partialize function
77
+
78
+ ### Actions Creation
79
+ - MUST provide set helper for updates
80
+ - MUST provide get helper for reads
81
+ - MUST support both object and function updates
82
+ - MUST enable action composition
83
+ - MUST preserve type safety
84
+
85
+ ### Type Inference
86
+ - MUST infer state type from initialState
87
+ - MUST infer actions type from actions return
88
+ - MUST support ReturnType for external use
89
+ - MUST enable Pick for action subsets
90
+
91
+ ### Version Management
92
+ - MUST increment version on schema changes
93
+ - MUST provide migrate function
94
+ - MUST handle old persisted states
95
+ - MUST document breaking changes
96
+ - MUST support multiple version migrations
97
+
98
+ ### Partialize Function
99
+ - MUST accept full state parameter
100
+ - MUST return partial state for persistence
101
+ - MUST exclude transient state
102
+ - MUST include all persistent state
103
+ - MUST enable optimization
104
+
105
+ ### Migration Function
106
+ - MUST accept persistedState parameter
107
+ - MUST accept version parameter
108
+ - MUST return migrated state
109
+ - MUST handle all old versions
110
+ - MUST default to identity for current version
111
+
112
+ ### OnRehydrate Callback
113
+ - MUST accept hydrated state parameter
114
+ - MUST execute after rehydration
115
+ - MUST enable initialization logic
116
+ - MUST not block hydration
117
+ - MUST support async operations
118
+
119
+ ### Error Handling
120
+ - MUST validate store configuration
121
+ - MUST throw descriptive errors for invalid config
122
+ - MUST handle persistence errors gracefully
123
+ - MUST log errors in development
124
+ - MUST provide fallback when possible
125
+
126
+ ### Testing Support
127
+ - MUST enable store reset in tests
128
+ - MUST support mocking persistence
129
+ - MUST provide test utilities
130
+ - MUST clear state between tests
131
+ - MUST not have hidden dependencies
132
+
133
+ ### Documentation
134
+ - MUST document store purpose
135
+ - MUST specify state structure
136
+ - MUST document all actions
137
+ - MUST explain persistence strategy
138
+ - MUST warn about common mistakes