@plyaz/types 1.22.1 → 1.22.3

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 (38) hide show
  1. package/dist/api/index.cjs.map +1 -1
  2. package/dist/api/index.js.map +1 -1
  3. package/dist/core/domain/crud.d.ts +27 -0
  4. package/dist/core/domain/index.d.ts +2 -1
  5. package/dist/core/domain/types.d.ts +34 -0
  6. package/dist/core/events/payloads.d.ts +1 -0
  7. package/dist/core/frontend/featureFlags.d.ts +3 -3
  8. package/dist/core/frontend/index.d.ts +1 -1
  9. package/dist/core/frontend/types.d.ts +544 -8
  10. package/dist/core/index.d.ts +1 -0
  11. package/dist/core/init/index.d.ts +1 -1
  12. package/dist/core/init/types.d.ts +192 -4
  13. package/dist/core/modules.d.ts +7 -2
  14. package/dist/core/services/index.d.ts +1 -0
  15. package/dist/core/services/keys.d.ts +40 -0
  16. package/dist/errors/codes.d.ts +11 -0
  17. package/dist/errors/index.cjs +13 -1
  18. package/dist/errors/index.cjs.map +1 -1
  19. package/dist/errors/index.js +13 -1
  20. package/dist/errors/index.js.map +1 -1
  21. package/dist/examples/types.d.ts +9 -5
  22. package/dist/features/cache/index.cjs +6 -0
  23. package/dist/features/cache/index.cjs.map +1 -1
  24. package/dist/features/cache/index.d.ts +1 -0
  25. package/dist/features/cache/index.js +5 -0
  26. package/dist/features/cache/index.js.map +1 -1
  27. package/dist/features/cache/types.d.ts +9 -1
  28. package/dist/features/index.cjs +6 -0
  29. package/dist/features/index.cjs.map +1 -1
  30. package/dist/features/index.d.ts +1 -0
  31. package/dist/features/index.js +5 -0
  32. package/dist/features/index.js.map +1 -1
  33. package/dist/index.cjs +30 -1
  34. package/dist/index.cjs.map +1 -1
  35. package/dist/index.d.ts +1 -0
  36. package/dist/index.js +28 -2
  37. package/dist/index.js.map +1 -1
  38. package/package.json +1 -1
@@ -6,11 +6,12 @@
6
6
  */
7
7
  import type { ApiClientOptions } from '../../api';
8
8
  import type { StoreKey } from '../../store';
9
- import type { FeatureFlagValue, FeatureFlagPollingConfig } from '../../features';
9
+ import type { FeatureFlagValue, FeatureFlagPollingConfig, CacheStrategyType } from '../../features';
10
10
  import type { PackageErrorLike } from '../../errors';
11
11
  import type { GlobalErrorHandlerConfig, ErrorHandlerLogger } from '../../errors/middleware';
12
- import type { CoreAppEnvironment, CoreAppContext, CoreRuntimeEnvironment } from '../modules';
12
+ import type { CoreAppEnvironment, CoreAppContext, CoreRuntimeEnvironment, CoreServiceRuntime } from '../modules';
13
13
  import type { CoreDbServiceConfig } from '../services';
14
+ import type { CoreInjectedServices } from '../domain';
14
15
  /**
15
16
  * Base interface that all domain service instances must implement.
16
17
  * This allows the registry to manage services generically.
@@ -148,11 +149,93 @@ export interface CoreServiceInitConfig {
148
149
  * ```
149
150
  */
150
151
  dedicatedDb?: boolean | Partial<CoreDbServiceConfig>;
152
+ /**
153
+ * Create a dedicated cache instance for this service.
154
+ *
155
+ * By default, services share the global cache instance. Use this when
156
+ * a service needs its own isolated cache (e.g., different TTL, separate namespace).
157
+ *
158
+ * - `false` or `undefined`: Use global shared cache (default)
159
+ * - `true`: Create dedicated instance using global config
160
+ * - `CoreCacheConfig`: Create dedicated instance with custom config
161
+ *
162
+ * For Redis strategy, services using the same URL will share connections.
163
+ * For memory strategy, each service gets its own isolated cache.
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * // Use global shared cache (default)
168
+ * { service: UserService, config: { enabled: true } }
169
+ *
170
+ * // Create dedicated cache with global config
171
+ * { service: SessionService, config: { dedicatedCache: true } }
172
+ *
173
+ * // Create dedicated cache with custom config
174
+ * {
175
+ * service: RateLimitService,
176
+ * config: {
177
+ * dedicatedCache: {
178
+ * strategy: 'redis',
179
+ * isEnabled: true,
180
+ * ttl: 60, // 1 minute TTL for rate limits
181
+ * prefix: 'ratelimit',
182
+ * },
183
+ * },
184
+ * }
185
+ * ```
186
+ */
187
+ dedicatedCache?: boolean | CoreCacheConfig;
188
+ /**
189
+ * Service-level cache configuration.
190
+ * Controls caching behavior for this specific service's operations.
191
+ *
192
+ * @example
193
+ * ```typescript
194
+ * {
195
+ * service: ExampleService,
196
+ * config: {
197
+ * cache: {
198
+ * enabled: true,
199
+ * defaultTtl: 300, // 5 minutes
200
+ * },
201
+ * },
202
+ * }
203
+ * ```
204
+ */
205
+ cache?: {
206
+ /** Enable cache for this service (default: true) */
207
+ enabled?: boolean;
208
+ /** Default TTL in seconds for cached operations */
209
+ defaultTtl?: number;
210
+ };
151
211
  }
152
212
  /**
153
213
  * Options passed to the service factory method
154
214
  */
155
- export interface CoreServiceCreateOptions {
215
+ export interface CoreServiceCreateOptions<TConfig = unknown, TMapper = unknown, TValidator = unknown> {
216
+ /**
217
+ * The service-specific configuration object.
218
+ * Optional when passed from ServiceRegistry (services build their own).
219
+ */
220
+ serviceConfig?: TConfig;
221
+ /**
222
+ * Mapper class to use for DTO transformations.
223
+ */
224
+ MapperClass?: new () => TMapper;
225
+ /**
226
+ * Validator class to use for validation.
227
+ */
228
+ ValidatorClass?: new () => TValidator;
229
+ /**
230
+ * Service name for logging and identification.
231
+ * Optional when passed from ServiceRegistry (services provide their own).
232
+ */
233
+ serviceName?: string;
234
+ /**
235
+ * Supported runtime environments.
236
+ * Optional when passed from ServiceRegistry (services provide their own).
237
+ */
238
+ supportedRuntimes?: readonly CoreServiceRuntime[];
156
239
  /**
157
240
  * API client configuration and mode.
158
241
  */
@@ -168,6 +251,11 @@ export interface CoreServiceCreateOptions {
168
251
  * Used to create dedicated instance or configure global.
169
252
  */
170
253
  options?: ApiClientOptions;
254
+ /**
255
+ * Actual API client instance to use.
256
+ * Injected by ServiceRegistry, not created by service.
257
+ */
258
+ instance?: unknown;
171
259
  };
172
260
  /**
173
261
  * Database configuration and mode.
@@ -184,6 +272,32 @@ export interface CoreServiceCreateOptions {
184
272
  * Used to create dedicated instance or configure global.
185
273
  */
186
274
  config?: Partial<CoreDbServiceConfig>;
275
+ /**
276
+ * Actual database service instance to use.
277
+ * Injected by ServiceRegistry, not created by service.
278
+ */
279
+ instance?: unknown;
280
+ };
281
+ /**
282
+ * Cache configuration and mode.
283
+ */
284
+ cache?: {
285
+ /**
286
+ * Whether to use a dedicated (isolated) cache instance.
287
+ * - `false`: Use global shared instance (default)
288
+ * - `true`: Create dedicated instance
289
+ */
290
+ dedicated: boolean;
291
+ /**
292
+ * Cache config (merged global + per-service config).
293
+ * Used to create dedicated instance or configure global.
294
+ */
295
+ config?: CoreCacheConfig;
296
+ /**
297
+ * Actual CacheManager instance to use.
298
+ * Injected by ServiceRegistry, not created by service.
299
+ */
300
+ instance?: unknown;
187
301
  };
188
302
  /** Logger prefix */
189
303
  loggerPrefix?: string;
@@ -194,6 +308,11 @@ export interface CoreServiceCreateOptions {
194
308
  * Passed from the service config to the create method.
195
309
  */
196
310
  storeKeys?: StoreKey[];
311
+ /**
312
+ * Injected service instances (cache, db, api).
313
+ * Services built by ServiceRegistry will receive these automatically.
314
+ */
315
+ injected?: CoreInjectedServices;
197
316
  }
198
317
  /**
199
318
  * Static interface that service classes must implement.
@@ -258,10 +377,16 @@ export interface CoreServiceEntry<TConfig extends CoreServiceInitConfig = CoreSe
258
377
  * ```
259
378
  */
260
379
  export interface CoreServiceRegistryConfig {
380
+ /** Global environment (production, staging, development, test) */
381
+ environment?: CoreAppEnvironment;
382
+ /** Runtime environment (node, browser, react-native, etc.) */
383
+ runtime?: CoreRuntimeEnvironment;
261
384
  /** Global API client options (shared by all services unless overridden) */
262
385
  apiClient?: ApiClientOptions;
263
386
  /** Global database config (shared by all services unless overridden) */
264
387
  db?: Partial<CoreDbServiceConfig>;
388
+ /** Global cache config (shared by all services unless overridden) */
389
+ cache?: CoreCacheConfig;
265
390
  /** Services to register and initialize */
266
391
  services: CoreServiceEntry[];
267
392
  }
@@ -320,6 +445,63 @@ export interface CoreErrorHandlerInitConfig extends Omit<GlobalErrorHandlerConfi
320
445
  * Set to false to disable logging.
321
446
  */
322
447
  logger?: ErrorHandlerLogger | boolean;
448
+ /**
449
+ * Localization configuration for error messages.
450
+ * Configure how error messages are translated and which locales are available.
451
+ *
452
+ * @example
453
+ * ```typescript
454
+ * errorHandler: {
455
+ * localization: {
456
+ * defaultLocale: 'es',
457
+ * fallbackLocale: 'en',
458
+ * additionalCatalogs: {
459
+ * es: { errors: { custom_error: 'Error personalizado' } }
460
+ * }
461
+ * }
462
+ * }
463
+ * ```
464
+ */
465
+ localization?: {
466
+ /** Default locale to use for error messages (default: 'en') */
467
+ defaultLocale?: string;
468
+ /** Fallback locale if translation not found (default: 'en') */
469
+ fallbackLocale?: string;
470
+ /** Additional message catalogs to merge with built-in messages */
471
+ additionalCatalogs?: Record<string, unknown>;
472
+ /** Replace built-in catalogs entirely (default: false) */
473
+ replaceBuiltIn?: boolean;
474
+ };
475
+ }
476
+ /**
477
+ * Cache initialization configuration
478
+ */
479
+ export interface CoreCacheConfig {
480
+ /** Cache strategy to use */
481
+ strategy: CacheStrategyType;
482
+ /** Whether caching is enabled (default: true) */
483
+ isEnabled: boolean;
484
+ /** Default TTL in seconds (default: 300 - 5 minutes) */
485
+ ttl?: number;
486
+ /** Key prefix for all cache entries (default: 'app') */
487
+ prefix?: string;
488
+ /** Redis-specific configuration (required when strategy is 'redis') */
489
+ redis?: {
490
+ /** Redis connection URL (e.g., redis://localhost:6379) */
491
+ url?: string;
492
+ /** Redis host (alternative to url) */
493
+ host?: string;
494
+ /** Redis port (default: 6379) */
495
+ port?: number;
496
+ /** Redis password */
497
+ password?: string;
498
+ /** Redis database number (default: 0) */
499
+ db?: number;
500
+ /** Connection timeout in milliseconds */
501
+ connectTimeout?: number;
502
+ /** Key prefix for Redis keys */
503
+ keyPrefix?: string;
504
+ };
323
505
  }
324
506
  /**
325
507
  * Base core initialization options
@@ -338,10 +520,14 @@ export interface CoreInitOptionsBase<TDb = unknown, TApi = unknown> {
338
520
  db?: TDb;
339
521
  /** API client configuration */
340
522
  api?: TApi;
523
+ /** Cache configuration */
524
+ cache?: CoreCacheConfig;
341
525
  /** Skip database initialization */
342
526
  skipDb?: boolean;
343
527
  /** Skip API client initialization */
344
528
  skipApi?: boolean;
529
+ /** Skip cache initialization */
530
+ skipCache?: boolean;
345
531
  /** Enable verbose logging */
346
532
  verbose?: boolean;
347
533
  /** Error handler configuration */
@@ -354,11 +540,13 @@ export interface CoreInitOptionsBase<TDb = unknown, TApi = unknown> {
354
540
  /**
355
541
  * Core services result from initialization
356
542
  */
357
- export interface CoreServicesResultBase<TDb = unknown, TApi = unknown> {
543
+ export interface CoreServicesResultBase<TDb = unknown, TApi = unknown, TCache = unknown> {
358
544
  /** Database service instance (null if skipped or on frontend) */
359
545
  db: TDb | null;
360
546
  /** API client service class */
361
547
  api: TApi | null;
548
+ /** Cache service instance (null if skipped or on frontend) */
549
+ cache: TCache | null;
362
550
  /** Loaded environment variables */
363
551
  env: Record<string, string | undefined>;
364
552
  /** Detected runtime environment */
@@ -311,7 +311,7 @@ export interface CoreApiInitOptions {
311
311
  /**
312
312
  * Core initialization options
313
313
  */
314
- export interface CoreInitOptions<TDbConfig = unknown, TApiConfig = CoreApiInitOptions> {
314
+ export interface CoreInitOptions<TDbConfig = unknown, TApiConfig = CoreApiInitOptions, TCacheConfig = unknown> {
315
315
  /** Path to .env file */
316
316
  envPath?: string;
317
317
  /** Global application environment */
@@ -324,19 +324,24 @@ export interface CoreInitOptions<TDbConfig = unknown, TApiConfig = CoreApiInitOp
324
324
  db?: TDbConfig;
325
325
  /** API client configuration */
326
326
  api?: TApiConfig;
327
+ /** Cache configuration */
328
+ cache?: TCacheConfig;
327
329
  /** Skip database initialization */
328
330
  skipDb?: boolean;
329
331
  /** Skip API client initialization */
330
332
  skipApi?: boolean;
333
+ /** Skip cache initialization */
334
+ skipCache?: boolean;
331
335
  /** Enable verbose logging */
332
336
  verbose?: boolean;
333
337
  }
334
338
  /**
335
339
  * Core initialization result
336
340
  */
337
- export interface CoreServicesResult<TDb = unknown, TApi = unknown> {
341
+ export interface CoreServicesResult<TDb = unknown, TApi = unknown, TCache = unknown> {
338
342
  db: TDb | null;
339
343
  api: TApi | null;
344
+ cache: TCache | null;
340
345
  env: CoreEnvVars;
341
346
  runtime: CoreRuntimeEnvironment;
342
347
  appContext: CoreAppContext;
@@ -3,3 +3,4 @@
3
3
  * Type definitions for @plyaz/core services
4
4
  */
5
5
  export type * from './types';
6
+ export { SERVICE_KEYS, ALL_SERVICE_KEYS, type ServiceKey } from './keys';
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Service Keys Constants
3
+ *
4
+ * Centralized service key constants for service registry and dependency injection.
5
+ * Use these constants instead of string literals for type safety and consistency.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { SERVICE_KEYS } from '@plyaz/types/core/services';
10
+ *
11
+ * class MyService {
12
+ * static readonly serviceKey = SERVICE_KEYS.EXAMPLE;
13
+ * }
14
+ *
15
+ * // Use in service registry
16
+ * const service = ServiceRegistry.get(SERVICE_KEYS.EXAMPLE);
17
+ * ```
18
+ */
19
+ /**
20
+ * Keys for all available domain services.
21
+ * These keys are used for service registration and lookup in the ServiceRegistry.
22
+ */
23
+ export declare const SERVICE_KEYS: {
24
+ /** Example domain service (backend) */
25
+ readonly EXAMPLE: "example";
26
+ /** Example domain service (frontend) */
27
+ readonly EXAMPLE_FRONTEND: "example-frontend";
28
+ /** Feature flags service */
29
+ readonly FEATURE_FLAGS: "featureFlags";
30
+ };
31
+ /**
32
+ * Type for service keys.
33
+ * Ensures only valid service keys can be used.
34
+ */
35
+ export type ServiceKey = (typeof SERVICE_KEYS)[keyof typeof SERVICE_KEYS];
36
+ /**
37
+ * Array of all service keys for iteration.
38
+ * Useful for validating service configurations or iterating over all services.
39
+ */
40
+ export declare const ALL_SERVICE_KEYS: ServiceKey[];
@@ -826,6 +826,17 @@ export declare const CORE_ERROR_CODES: {
826
826
  readonly CONFIGURATION_INVALID: "core.configuration_invalid";
827
827
  readonly OPERATION_FAILED: "core.operation_failed";
828
828
  readonly UNKNOWN_ERROR: "core.unknown_error";
829
+ readonly CLIENT_INITIALIZATION_FAILED: "CLIENT_INITIALIZATION_FAILED";
830
+ readonly CLIENT_INVALID_CONFIG: "CLIENT_INVALID_CONFIG";
831
+ readonly CONTEXT_OPERATION_FAILED: "CONTEXT_OPERATION_FAILED";
832
+ readonly FEATURE_NOT_SUPPORTED: "feature.not.supported";
833
+ readonly VALIDATION_ERROR: "VALIDATION_ERROR";
834
+ readonly NETWORK_ERROR: "NETWORK_ERROR";
835
+ readonly RESOURCE_NOT_FOUND: "RESOURCE_NOT_FOUND";
836
+ readonly DB_CONNECTION_FAILED: "db.connection_failed";
837
+ readonly DB_DUPLICATE_ENTRY: "db.duplicate_entry";
838
+ readonly PROVIDER_NOT_IMPLEMENTED: "provider.not.implemented";
839
+ readonly STORAGE_FILE_WRITE_FAILED: "storage.file.write.failed";
829
840
  };
830
841
  /**
831
842
  * PAYMENT-specific error codes
@@ -1324,7 +1324,19 @@ var CORE_ERROR_CODES = {
1324
1324
  INITIALIZATION_FAILED: ERROR_CODES.CORE_INITIALIZATION_FAILED,
1325
1325
  CONFIGURATION_INVALID: ERROR_CODES.CORE_CONFIGURATION_INVALID,
1326
1326
  OPERATION_FAILED: ERROR_CODES.CORE_OPERATION_FAILED,
1327
- UNKNOWN_ERROR: ERROR_CODES.CORE_UNKNOWN_ERROR
1327
+ UNKNOWN_ERROR: ERROR_CODES.CORE_UNKNOWN_ERROR,
1328
+ // Common/Generic Errors (used across core package)
1329
+ CLIENT_INITIALIZATION_FAILED: ERROR_CODES.CLIENT_INITIALIZATION_FAILED,
1330
+ CLIENT_INVALID_CONFIG: ERROR_CODES.CLIENT_INVALID_CONFIG,
1331
+ CONTEXT_OPERATION_FAILED: ERROR_CODES.CONTEXT_OPERATION_FAILED,
1332
+ FEATURE_NOT_SUPPORTED: ERROR_CODES.FEATURE_NOT_SUPPORTED,
1333
+ VALIDATION_ERROR: ERROR_CODES.VALIDATION_ERROR,
1334
+ NETWORK_ERROR: ERROR_CODES.NETWORK_ERROR,
1335
+ RESOURCE_NOT_FOUND: ERROR_CODES.RESOURCE_NOT_FOUND,
1336
+ DB_CONNECTION_FAILED: ERROR_CODES.DB_CONNECTION_FAILED,
1337
+ DB_DUPLICATE_ENTRY: ERROR_CODES.DB_DUPLICATE_ENTRY,
1338
+ PROVIDER_NOT_IMPLEMENTED: ERROR_CODES.PROVIDER_NOT_IMPLEMENTED,
1339
+ STORAGE_FILE_WRITE_FAILED: ERROR_CODES.STORAGE_FILE_WRITE_FAILED
1328
1340
  };
1329
1341
  var PAYMENT_ERROR_CODES = {
1330
1342
  // Timeout & Network