@plyaz/types 1.1.3 → 1.1.4

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.
@@ -6,7 +6,7 @@ import { z } from 'zod';
6
6
  * ```ts
7
7
  * LoginCredentialsSchema.parse({
8
8
  * email: "user@example.com",
9
- * password: "securePassword123"
9
+ * password: ""
10
10
  * });
11
11
  * ```
12
12
  */
@@ -0,0 +1 @@
1
+ export type * from './types';
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Cache Types and Interfaces
3
+ *
4
+ * Type definitions for the caching layer.
5
+ * This will be moved to @plyaz/core when the package structure is finalized.
6
+ *
7
+ * @fileoverview Cache type definitions
8
+ * @version 1.0.0
9
+ */
10
+ /**
11
+ * Cache entry structure that wraps cached data with metadata.
12
+ */
13
+ export interface CacheEntry<T = unknown> {
14
+ /** The cached data */
15
+ data: T;
16
+ /** When the entry expires (timestamp in milliseconds) */
17
+ expiresAt: number;
18
+ /** When the entry was created (timestamp in milliseconds) */
19
+ createdAt: number;
20
+ /** Optional metadata for the cache entry */
21
+ meta?: Record<string, unknown>;
22
+ }
23
+ /**
24
+ * Cache statistics for monitoring and debugging.
25
+ */
26
+ export interface CacheStats {
27
+ /** Total number of cache hits */
28
+ hits: number;
29
+ /** Total number of cache misses */
30
+ misses: number;
31
+ /** Total number of set operations */
32
+ sets: number;
33
+ /** Total number of delete operations */
34
+ deletes: number;
35
+ /** Number of entries currently in cache */
36
+ size: number;
37
+ /** Cache hit ratio (hits / (hits + misses)) */
38
+ hitRatio?: number;
39
+ }
40
+ /**
41
+ * Configuration for cache strategies.
42
+ */
43
+ export interface CacheConfig {
44
+ /** Whether caching is enabled */
45
+ isEnabled: boolean;
46
+ /** Default TTL in seconds */
47
+ ttl: number;
48
+ /** Cache strategy to use */
49
+ strategy: 'memory' | 'redis';
50
+ /** Redis-specific configuration */
51
+ redisConfig?: RedisCacheConfig;
52
+ /** Memory cache specific configuration */
53
+ memoryConfig?: {
54
+ /** Maximum number of entries to store */
55
+ maxSize?: number;
56
+ /** Maximum number of entries to store (alias for maxSize) */
57
+ maxEntries?: number;
58
+ /** Check for expired entries every N milliseconds */
59
+ cleanupInterval?: number;
60
+ };
61
+ }
62
+ /**
63
+ * Configuration for memory cache strategy.
64
+ */
65
+ export interface MemoryCacheConfig {
66
+ /** Maximum number of entries to store (default: 1000) */
67
+ maxSize?: number;
68
+ /** Maximum number of entries to store (alias for maxSize) */
69
+ maxEntries?: number;
70
+ /** Check for expired entries every N milliseconds (default: 60000) */
71
+ cleanupInterval?: number;
72
+ /** Callback when entry is evicted */
73
+ onEvict?: (key: string, entry: CacheEntry) => void;
74
+ }
75
+ /**
76
+ * Configuration for Redis cache strategy.
77
+ */
78
+ export interface RedisCacheConfig {
79
+ /** Redis connection URL */
80
+ url: string;
81
+ /** Redis password for authentication */
82
+ password?: string;
83
+ /** Redis database number */
84
+ db?: number;
85
+ /** Key prefix for Redis keys */
86
+ keyPrefix?: string;
87
+ /** Connection timeout in milliseconds */
88
+ connectTimeout?: number;
89
+ /** Command timeout in milliseconds */
90
+ commandTimeout?: number;
91
+ }
92
+ /**
93
+ * Cache manager statistics interface.
94
+ */
95
+ export interface CacheManagerStats extends CacheStats {
96
+ }
97
+ /**
98
+ * Interface that all cache strategies must implement.
99
+ */
100
+ export interface CacheStrategy {
101
+ /**
102
+ * Stores a cache entry.
103
+ *
104
+ * @param key - Cache key
105
+ * @param entry - Cache entry to store
106
+ * @returns Promise that resolves when entry is stored
107
+ */
108
+ set<T>(key: string, entry: CacheEntry<T>): Promise<void>;
109
+ /**
110
+ * Retrieves a cache entry.
111
+ *
112
+ * @param key - Cache key
113
+ * @returns Promise that resolves to cache entry or null if not found
114
+ */
115
+ get<T>(key: string): Promise<CacheEntry<T> | null>;
116
+ /**
117
+ * Removes a cache entry.
118
+ *
119
+ * @param key - Cache key to remove
120
+ * @returns Promise that resolves when entry is removed
121
+ */
122
+ delete(key: string): Promise<void>;
123
+ /**
124
+ * Clears all cache entries.
125
+ *
126
+ * @returns Promise that resolves when cache is cleared
127
+ */
128
+ clear(): Promise<void>;
129
+ /**
130
+ * Gets cache statistics.
131
+ *
132
+ * @returns Promise that resolves to cache statistics
133
+ */
134
+ getStats(): Promise<CacheStats>;
135
+ /**
136
+ * Disposes of the cache strategy and cleans up resources.
137
+ * Optional method for cleanup.
138
+ *
139
+ * @returns Promise that resolves when cleanup is complete
140
+ */
141
+ dispose?(): Promise<void>;
142
+ }
@@ -0,0 +1 @@
1
+ export type * from './types';
@@ -0,0 +1,302 @@
1
+ /**
2
+ * Feature Flags Domain Types
3
+ *
4
+ * Core type definitions for the feature flags domain.
5
+ * These types will be moved to @plyaz/types when the package structure is finalized.
6
+ *
7
+ * @fileoverview Feature flags domain type definitions
8
+ * @version 1.0.0
9
+ */
10
+ import type * as React from 'react';
11
+ /**
12
+ * Possible values that a feature flag can hold.
13
+ * Supports primitive types and JSON objects for complex configurations.
14
+ */
15
+ export type FeatureFlagValue = boolean | string | number | Record<string, unknown>;
16
+ /**
17
+ * Core feature flag definition interface.
18
+ * Represents a complete feature flag with all its metadata and configuration.
19
+ */
20
+ export interface FeatureFlag<FeatureFlagKey> {
21
+ /** The unique identifier for this flag */
22
+ key: FeatureFlagKey;
23
+ /** Human-readable name for the flag */
24
+ name: string;
25
+ /** Detailed description of what this flag controls */
26
+ description: string;
27
+ /** Whether this flag is currently active */
28
+ isEnabled: boolean;
29
+ /** The value returned when this flag is evaluated */
30
+ value: FeatureFlagValue;
31
+ /** The data type of the flag value */
32
+ type: 'boolean' | 'string' | 'number' | 'json';
33
+ /** Which environment(s) this flag applies to */
34
+ environment: 'development' | 'staging' | 'production' | 'all';
35
+ /** Percentage of users who should receive this flag (0-100) */
36
+ rolloutPercentage?: number;
37
+ /** Timestamp when this flag was created */
38
+ createdAt: Date;
39
+ /** Timestamp when this flag was last updated */
40
+ updatedAt: Date;
41
+ /** Email or ID of the user who created this flag */
42
+ createdBy: string;
43
+ /** Email or ID of the user who last updated this flag */
44
+ updatedBy: string;
45
+ }
46
+ /**
47
+ * Feature flag rule for advanced targeting and conditional logic.
48
+ */
49
+ export interface FeatureFlagRule<FeatureFlagKey> {
50
+ /** Unique identifier for this rule */
51
+ id: string;
52
+ /** The feature flag this rule applies to */
53
+ flagKey: FeatureFlagKey;
54
+ /** Human-readable name for this rule */
55
+ name: string;
56
+ /** Array of conditions that must all be met for this rule to apply */
57
+ conditions: FeatureFlagCondition[];
58
+ /** The value to return if this rule matches */
59
+ value: FeatureFlagValue;
60
+ /** Percentage of matching users who should receive this value (0-100) */
61
+ rolloutPercentage?: number;
62
+ /** Priority for rule evaluation (higher numbers = higher priority) */
63
+ priority: number;
64
+ /** Whether this rule is currently active */
65
+ isEnabled: boolean;
66
+ }
67
+ /**
68
+ * Individual condition for feature flag rule evaluation.
69
+ */
70
+ export interface FeatureFlagCondition {
71
+ /** The context field to evaluate */
72
+ field: 'userId' | 'userEmail' | 'userRole' | 'country' | 'platform' | 'version' | 'environment' | 'custom';
73
+ /** The comparison operator to use */
74
+ operator: 'equals' | 'not_equals' | 'contains' | 'not_contains' | 'in' | 'not_in' | 'greater_than' | 'less_than';
75
+ /** The value(s) to compare against */
76
+ value: string | number | string[] | number[];
77
+ }
78
+ /**
79
+ * Context information used for feature flag evaluation.
80
+ */
81
+ export interface FeatureFlagContext {
82
+ /** Unique identifier for the user */
83
+ userId?: string;
84
+ /** User's email address */
85
+ userEmail?: string;
86
+ /** User's role or permission level */
87
+ userRole?: string;
88
+ /** User's country code (ISO 3166-1 alpha-2) */
89
+ country?: string;
90
+ /** Platform the user is accessing from */
91
+ platform?: 'web' | 'mobile' | 'desktop';
92
+ /** Application version */
93
+ version?: string;
94
+ /** Custom context data for advanced targeting */
95
+ custom?: Record<string, unknown>;
96
+ /** Current environment */
97
+ environment: 'development' | 'staging' | 'production';
98
+ }
99
+ /**
100
+ * Result of a feature flag evaluation.
101
+ */
102
+ export interface FeatureFlagEvaluation<FeatureFlagKey> {
103
+ /** The feature flag key that was evaluated */
104
+ flagKey: FeatureFlagKey;
105
+ /** The resolved value for this flag */
106
+ value: FeatureFlagValue;
107
+ /** Whether the flag is considered enabled (truthy value) */
108
+ isEnabled: boolean;
109
+ /** Explanation of how this value was determined */
110
+ reason: 'default' | 'rule_match' | 'rollout' | 'override' | 'disabled';
111
+ /** Source of the evaluation result */
112
+ source?: 'default' | 'override' | 'rule' | 'rollout';
113
+ /** ID of the rule that matched (if reason is 'rule_match') */
114
+ ruleId?: string;
115
+ /** Timestamp when this evaluation occurred */
116
+ evaluatedAt: Date;
117
+ }
118
+ /**
119
+ * Configuration options for the feature flag system.
120
+ */
121
+ export interface FeatureFlagConfig<FeatureFlagKey> {
122
+ /** The storage provider to use for flag data */
123
+ provider: 'database' | 'redis' | 'memory' | 'api' | 'file';
124
+ /** Whether to enable caching of flag evaluations */
125
+ isCacheEnabled: boolean;
126
+ /** Cache time-to-live in seconds */
127
+ cacheTtl: number;
128
+ /** How often to refresh flag data in seconds (0 = no auto-refresh) */
129
+ refreshInterval: number;
130
+ /** Whether to fall back to default values on errors */
131
+ shouldFallbackToDefaults: boolean;
132
+ /** Whether to enable debug logging */
133
+ isLoggingEnabled: boolean;
134
+ /** API endpoint URL (required if provider is 'api') */
135
+ apiEndpoint?: string;
136
+ /** API authentication key (required if provider is 'api') */
137
+ apiKey?: string;
138
+ /** Database configuration (required if provider is 'database') */
139
+ databaseConfig?: {
140
+ connectionString: string;
141
+ tableName: string;
142
+ };
143
+ /** Redis configuration (required if provider is 'redis') */
144
+ redisConfig?: {
145
+ url: string;
146
+ keyPrefix: string;
147
+ };
148
+ /** File configuration (required if provider is 'file') */
149
+ fileConfig?: {
150
+ filePath: string;
151
+ format: 'json' | 'yaml';
152
+ shouldWatchForChanges: boolean;
153
+ };
154
+ /** Memory provider rules (optional for memory provider) */
155
+ memoryRules?: FeatureFlagRule<FeatureFlagKey>[];
156
+ }
157
+ /**
158
+ * React Hook Options for Feature Flags
159
+ */
160
+ export interface UseFeatureFlagOptions {
161
+ /** Evaluation context for targeting */
162
+ context?: FeatureFlagContext;
163
+ /** Whether to automatically refresh on provider updates */
164
+ isAutoRefresh?: boolean;
165
+ /** Default value to use while loading or on error */
166
+ defaultValue?: FeatureFlagValue;
167
+ /** Whether to suspend on initial load (for Suspense) */
168
+ isSuspense?: boolean;
169
+ }
170
+ /**
171
+ * Internal state for feature flag evaluation in React hooks.
172
+ */
173
+ export interface FeatureFlagState<FeatureFlagKey, T extends FeatureFlagValue = FeatureFlagValue> {
174
+ value: T;
175
+ isLoading: boolean;
176
+ error: Error | null;
177
+ evaluation: FeatureFlagEvaluation<FeatureFlagKey> | null;
178
+ }
179
+ /**
180
+ * Context value interface for React feature flag provider.
181
+ */
182
+ export interface FeatureFlagContextValue<FeatureFlagKey> {
183
+ /** The feature flag provider instance */
184
+ provider: FeatureFlagProvider<FeatureFlagKey>;
185
+ /** Whether the provider is initialized */
186
+ isInitialized: boolean;
187
+ /** Whether the provider is currently loading */
188
+ isLoading: boolean;
189
+ /** Any error that occurred during initialization */
190
+ error: Error | null;
191
+ /** When the provider was last updated */
192
+ lastUpdated: Date | null;
193
+ /** Manual refresh function */
194
+ refresh: () => Promise<void>;
195
+ }
196
+ /**
197
+ * Props for the React FeatureFlagProvider component.
198
+ */
199
+ export interface FeatureFlagProviderProps<FeatureFlagKey> {
200
+ /** Provider configuration */
201
+ config: FeatureFlagConfig<FeatureFlagKey>;
202
+ /** Default context for flag evaluation */
203
+ defaultContext?: FeatureFlagContext;
204
+ /** Children components */
205
+ children: React.ReactNode;
206
+ /** Callback when provider is ready */
207
+ onReady?: (provider: FeatureFlagProvider<FeatureFlagKey>) => void;
208
+ /** Callback when an error occurs */
209
+ onError?: (error: Error) => void;
210
+ /** Whether to show loading state while initializing */
211
+ isShowLoading?: boolean;
212
+ /** Custom loading component */
213
+ loadingComponent?: React.ComponentType;
214
+ /** Custom error component */
215
+ errorComponent?: React.ComponentType<{
216
+ error: Error;
217
+ retry: () => void;
218
+ }>;
219
+ }
220
+ /**
221
+ * Backend Request DTO for flag creation.
222
+ */
223
+ export interface CreateFlagRequest<FeatureFlagKey> {
224
+ key: FeatureFlagKey;
225
+ name: string;
226
+ description?: string;
227
+ value: FeatureFlagValue;
228
+ isEnabled?: boolean;
229
+ environment?: 'all' | 'development' | 'staging' | 'production';
230
+ rolloutPercentage?: number;
231
+ }
232
+ /**
233
+ * Backend Provider health status information.
234
+ */
235
+ export interface ProviderHealthStatus {
236
+ isInitialized: boolean;
237
+ provider: string;
238
+ isCacheEnabled: boolean;
239
+ }
240
+ /**
241
+ * Main interface for feature flag providers.
242
+ */
243
+ export interface FeatureFlagProvider<FeatureFlagKey> {
244
+ /** Initializes the provider by loading initial data */
245
+ initialize(): Promise<void>;
246
+ /** Evaluates a feature flag and returns full evaluation details */
247
+ getFlag(key: FeatureFlagKey, context?: FeatureFlagContext): Promise<FeatureFlagEvaluation<FeatureFlagKey>>;
248
+ /** Evaluates all feature flags for the given context */
249
+ getAllFlags(context?: FeatureFlagContext): Promise<Record<string, FeatureFlagEvaluation<FeatureFlagKey>>>;
250
+ /** Checks if a feature flag is enabled */
251
+ isEnabled(key: FeatureFlagKey, context?: FeatureFlagContext): Promise<boolean>;
252
+ /** Gets the value of a feature flag with optional type casting */
253
+ getValue<T = FeatureFlagValue>(key: FeatureFlagKey, context?: FeatureFlagContext): Promise<T>;
254
+ /** Refreshes the flag data from the underlying storage */
255
+ refresh(): Promise<void>;
256
+ /** Subscribes to flag changes and updates */
257
+ subscribe(callback: () => void): () => void;
258
+ /** Sets an override for a specific flag key */
259
+ setOverride(key: FeatureFlagKey, value: FeatureFlagValue): void;
260
+ /** Removes an override for a specific flag key */
261
+ removeOverride(key: FeatureFlagKey): void;
262
+ /** Clears all overrides */
263
+ clearOverrides(): void;
264
+ /** Disposes of the provider, cleaning up resources */
265
+ dispose(): void;
266
+ }
267
+ /**
268
+ * Partial mapping for flag overrides.
269
+ */
270
+ export type FeatureFlagOverrides<FeatureFlagKey extends string = string> = Partial<{
271
+ [K in FeatureFlagKey]: FeatureFlagValue;
272
+ }>;
273
+ /**
274
+ * Hook-like interface for reactive feature flag usage.
275
+ */
276
+ export type FeatureFlagHook<T = boolean> = {
277
+ /** The current value of the flag */
278
+ value: T;
279
+ /** Whether the flag is currently being loaded */
280
+ isLoading: boolean;
281
+ /** Any error that occurred during flag evaluation */
282
+ error: Error | null;
283
+ /** Function to manually refresh the flag value */
284
+ refresh: () => Promise<void>;
285
+ };
286
+ /**
287
+ * Helper functions for managing feature flags.
288
+ */
289
+ export interface FeatureFlagHelpers<FeatureFlagKey extends string = string> {
290
+ setOverride: (key: FeatureFlagKey, value: FeatureFlagValue) => void;
291
+ removeOverride: (key: FeatureFlagKey) => void;
292
+ clearOverrides: () => void;
293
+ refresh: () => Promise<void>;
294
+ getMultipleFlags: (keys: FeatureFlagKey[], context?: FeatureFlagContext) => Promise<Record<FeatureFlagKey, FeatureFlagValue>>;
295
+ isAnyEnabled: (keys: FeatureFlagKey[], context?: FeatureFlagContext) => Promise<boolean>;
296
+ isAllEnabled: (keys: FeatureFlagKey[], context?: FeatureFlagContext) => Promise<boolean>;
297
+ whenEnabled: <T>(key: FeatureFlagKey, callback: () => T | Promise<T>, fallback?: () => T | Promise<T>, context?: FeatureFlagContext) => Promise<T | undefined>;
298
+ }
299
+ export interface FetchFeatureFlagDataResponse<FeatureFlagKey> {
300
+ flags: FeatureFlag<FeatureFlagKey>[];
301
+ rules: FeatureFlagRule<FeatureFlagKey>[];
302
+ }
@@ -0,0 +1,2 @@
1
+ export type * from './cache';
2
+ export type * from './feature-flag';
package/dist/index.d.ts CHANGED
@@ -8,3 +8,4 @@ export type * from './store';
8
8
  export type * from './ui';
9
9
  export * from './web3';
10
10
  export type * from './translations';
11
+ export type * from './features';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plyaz/types",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "author": "Redeemer Pace",
5
5
  "license": "ISC",
6
6
  "description": "Provides shared TypeScript types and schema utilities for validation and parsing in the @playz ecosystem.",