@plyaz/types 1.22.0 → 1.22.2

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 +133 -3
  13. package/dist/core/services/index.d.ts +1 -0
  14. package/dist/core/services/keys.d.ts +40 -0
  15. package/dist/errors/codes.d.ts +11 -0
  16. package/dist/errors/index.cjs +13 -1
  17. package/dist/errors/index.cjs.map +1 -1
  18. package/dist/errors/index.js +13 -1
  19. package/dist/errors/index.js.map +1 -1
  20. package/dist/errors/middleware.d.ts +23 -4
  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,9 +6,9 @@
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
- import type { GlobalErrorHandlerConfig } from '../../errors/middleware';
11
+ import type { GlobalErrorHandlerConfig, ErrorHandlerLogger } from '../../errors/middleware';
12
12
  import type { CoreAppEnvironment, CoreAppContext, CoreRuntimeEnvironment } from '../modules';
13
13
  import type { CoreDbServiceConfig } from '../services';
14
14
  /**
@@ -148,6 +148,42 @@ export interface CoreServiceInitConfig {
148
148
  * ```
149
149
  */
150
150
  dedicatedDb?: boolean | Partial<CoreDbServiceConfig>;
151
+ /**
152
+ * Create a dedicated cache instance for this service.
153
+ *
154
+ * By default, services share the global cache instance. Use this when
155
+ * a service needs its own isolated cache (e.g., different TTL, separate namespace).
156
+ *
157
+ * - `false` or `undefined`: Use global shared cache (default)
158
+ * - `true`: Create dedicated instance using global config
159
+ * - `CoreCacheConfig`: Create dedicated instance with custom config
160
+ *
161
+ * For Redis strategy, services using the same URL will share connections.
162
+ * For memory strategy, each service gets its own isolated cache.
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * // Use global shared cache (default)
167
+ * { service: UserService, config: { enabled: true } }
168
+ *
169
+ * // Create dedicated cache with global config
170
+ * { service: SessionService, config: { dedicatedCache: true } }
171
+ *
172
+ * // Create dedicated cache with custom config
173
+ * {
174
+ * service: RateLimitService,
175
+ * config: {
176
+ * dedicatedCache: {
177
+ * strategy: 'redis',
178
+ * isEnabled: true,
179
+ * ttl: 60, // 1 minute TTL for rate limits
180
+ * prefix: 'ratelimit',
181
+ * },
182
+ * },
183
+ * }
184
+ * ```
185
+ */
186
+ dedicatedCache?: boolean | CoreCacheConfig;
151
187
  }
152
188
  /**
153
189
  * Options passed to the service factory method
@@ -168,6 +204,11 @@ export interface CoreServiceCreateOptions {
168
204
  * Used to create dedicated instance or configure global.
169
205
  */
170
206
  options?: ApiClientOptions;
207
+ /**
208
+ * Actual API client instance to use.
209
+ * Injected by ServiceRegistry, not created by service.
210
+ */
211
+ instance?: unknown;
171
212
  };
172
213
  /**
173
214
  * Database configuration and mode.
@@ -184,6 +225,32 @@ export interface CoreServiceCreateOptions {
184
225
  * Used to create dedicated instance or configure global.
185
226
  */
186
227
  config?: Partial<CoreDbServiceConfig>;
228
+ /**
229
+ * Actual database service instance to use.
230
+ * Injected by ServiceRegistry, not created by service.
231
+ */
232
+ instance?: unknown;
233
+ };
234
+ /**
235
+ * Cache configuration and mode.
236
+ */
237
+ cache?: {
238
+ /**
239
+ * Whether to use a dedicated (isolated) cache instance.
240
+ * - `false`: Use global shared instance (default)
241
+ * - `true`: Create dedicated instance
242
+ */
243
+ dedicated: boolean;
244
+ /**
245
+ * Cache config (merged global + per-service config).
246
+ * Used to create dedicated instance or configure global.
247
+ */
248
+ config?: CoreCacheConfig;
249
+ /**
250
+ * Actual CacheManager instance to use.
251
+ * Injected by ServiceRegistry, not created by service.
252
+ */
253
+ instance?: unknown;
187
254
  };
188
255
  /** Logger prefix */
189
256
  loggerPrefix?: string;
@@ -258,10 +325,16 @@ export interface CoreServiceEntry<TConfig extends CoreServiceInitConfig = CoreSe
258
325
  * ```
259
326
  */
260
327
  export interface CoreServiceRegistryConfig {
328
+ /** Global environment (production, staging, development, test) */
329
+ environment?: CoreAppEnvironment;
330
+ /** Runtime environment (node, browser, react-native, etc.) */
331
+ runtime?: CoreRuntimeEnvironment;
261
332
  /** Global API client options (shared by all services unless overridden) */
262
333
  apiClient?: ApiClientOptions;
263
334
  /** Global database config (shared by all services unless overridden) */
264
335
  db?: Partial<CoreDbServiceConfig>;
336
+ /** Global cache config (shared by all services unless overridden) */
337
+ cache?: CoreCacheConfig;
265
338
  /** Services to register and initialize */
266
339
  services: CoreServiceEntry[];
267
340
  }
@@ -299,6 +372,57 @@ export interface CoreErrorHandlerInitConfig extends Omit<GlobalErrorHandlerConfi
299
372
  * Set to false to use your own error handling.
300
373
  */
301
374
  enabled?: boolean;
375
+ /**
376
+ * Whether to auto-create HTTP error handler based on runtime (default: true).
377
+ * When enabled, Core will create the appropriate middleware for:
378
+ * - Express: createHttpErrorHandler
379
+ * - NestJS: createNestJsExceptionFilter
380
+ * - Next.js: withErrorHandler / createNextApiErrorHandler
381
+ * - Node.js: handleNodeError
382
+ *
383
+ * Access via Core.httpErrorHandler or Core.getHttpErrorHandler()
384
+ */
385
+ httpHandler?: boolean;
386
+ /**
387
+ * Include stack traces in HTTP error responses (default: false in production)
388
+ */
389
+ includeStack?: boolean;
390
+ /**
391
+ * Custom logger for error handlers.
392
+ * If not provided, uses default console logger.
393
+ * Set to false to disable logging.
394
+ */
395
+ logger?: ErrorHandlerLogger | boolean;
396
+ }
397
+ /**
398
+ * Cache initialization configuration
399
+ */
400
+ export interface CoreCacheConfig {
401
+ /** Cache strategy to use */
402
+ strategy: CacheStrategyType;
403
+ /** Whether caching is enabled (default: true) */
404
+ isEnabled: boolean;
405
+ /** Default TTL in seconds (default: 300 - 5 minutes) */
406
+ ttl?: number;
407
+ /** Key prefix for all cache entries (default: 'app') */
408
+ prefix?: string;
409
+ /** Redis-specific configuration (required when strategy is 'redis') */
410
+ redis?: {
411
+ /** Redis connection URL (e.g., redis://localhost:6379) */
412
+ url?: string;
413
+ /** Redis host (alternative to url) */
414
+ host?: string;
415
+ /** Redis port (default: 6379) */
416
+ port?: number;
417
+ /** Redis password */
418
+ password?: string;
419
+ /** Redis database number (default: 0) */
420
+ db?: number;
421
+ /** Connection timeout in milliseconds */
422
+ connectTimeout?: number;
423
+ /** Key prefix for Redis keys */
424
+ keyPrefix?: string;
425
+ };
302
426
  }
303
427
  /**
304
428
  * Base core initialization options
@@ -317,10 +441,14 @@ export interface CoreInitOptionsBase<TDb = unknown, TApi = unknown> {
317
441
  db?: TDb;
318
442
  /** API client configuration */
319
443
  api?: TApi;
444
+ /** Cache configuration */
445
+ cache?: CoreCacheConfig;
320
446
  /** Skip database initialization */
321
447
  skipDb?: boolean;
322
448
  /** Skip API client initialization */
323
449
  skipApi?: boolean;
450
+ /** Skip cache initialization */
451
+ skipCache?: boolean;
324
452
  /** Enable verbose logging */
325
453
  verbose?: boolean;
326
454
  /** Error handler configuration */
@@ -333,11 +461,13 @@ export interface CoreInitOptionsBase<TDb = unknown, TApi = unknown> {
333
461
  /**
334
462
  * Core services result from initialization
335
463
  */
336
- export interface CoreServicesResultBase<TDb = unknown, TApi = unknown> {
464
+ export interface CoreServicesResultBase<TDb = unknown, TApi = unknown, TCache = unknown> {
337
465
  /** Database service instance (null if skipped or on frontend) */
338
466
  db: TDb | null;
339
467
  /** API client service class */
340
468
  api: TApi | null;
469
+ /** Cache service instance (null if skipped or on frontend) */
470
+ cache: TCache | null;
341
471
  /** Loaded environment variables */
342
472
  env: Record<string, string | undefined>;
343
473
  /** Detected runtime environment */
@@ -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