@hkdigital/lib-sveltekit 0.1.70 → 0.1.72

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 (31) hide show
  1. package/dist/classes/cache/IndexedDbCache.d.ts +212 -0
  2. package/dist/classes/cache/IndexedDbCache.js +673 -0
  3. package/dist/classes/cache/MemoryResponseCache.d.ts +101 -14
  4. package/dist/classes/cache/MemoryResponseCache.js +97 -12
  5. package/dist/classes/cache/index.d.ts +1 -1
  6. package/dist/classes/cache/index.js +2 -1
  7. package/dist/classes/events/EventEmitter.d.ts +142 -0
  8. package/dist/classes/events/EventEmitter.js +275 -0
  9. package/dist/classes/events/index.d.ts +1 -0
  10. package/dist/classes/events/index.js +2 -0
  11. package/dist/classes/logging/Logger.d.ts +74 -0
  12. package/dist/classes/logging/Logger.js +158 -0
  13. package/dist/classes/logging/constants.d.ts +14 -0
  14. package/dist/classes/logging/constants.js +18 -0
  15. package/dist/classes/logging/index.d.ts +2 -0
  16. package/dist/classes/logging/index.js +4 -0
  17. package/dist/classes/services/ServiceBase.d.ts +153 -0
  18. package/dist/classes/services/ServiceBase.js +409 -0
  19. package/dist/classes/services/ServiceManager.d.ts +350 -0
  20. package/dist/classes/services/ServiceManager.js +1114 -0
  21. package/dist/classes/services/constants.d.ts +11 -0
  22. package/dist/classes/services/constants.js +12 -0
  23. package/dist/classes/services/index.d.ts +3 -0
  24. package/dist/classes/services/index.js +5 -0
  25. package/dist/util/env/index.d.ts +1 -0
  26. package/dist/util/env/index.js +9 -0
  27. package/dist/util/http/caching.js +24 -12
  28. package/dist/util/http/http-request.js +12 -7
  29. package/package.json +2 -1
  30. package/dist/classes/cache/PersistentResponseCache.d.ts +0 -46
  31. /package/dist/classes/cache/{PersistentResponseCache.js → PersistentResponseCache.js__} +0 -0
@@ -0,0 +1,350 @@
1
+ /**
2
+ * @typedef {Object} ServiceEntry
3
+ * @property {Object} instance - Service instance
4
+ * @property {Object} config - Service configuration
5
+ * @property {string[]} dependencies - List of service dependencies
6
+ * @property {Function} stateChangedUnsubscribe - Unsubscribe function for state events
7
+ * @property {Function} errorUnsubscribe - Unsubscribe function for error events
8
+ */
9
+ /**
10
+ * Manager for coordinating services lifecycle
11
+ */
12
+ export default class ServiceManager {
13
+ /**
14
+ * Create a new service manager
15
+ *
16
+ * @param {Object} [options] - Manager options
17
+ * @param {string} [options.logLevel=INFO] - Log level for the manager
18
+ */
19
+ constructor(options?: {
20
+ logLevel?: string;
21
+ });
22
+ /**
23
+ * Map of registered services
24
+ * @type {Map<string, ServiceEntry>}
25
+ * @private
26
+ */
27
+ private services;
28
+ /**
29
+ * Event emitter for service events
30
+ * @type {EventEmitter}
31
+ */
32
+ events: EventEmitter;
33
+ /**
34
+ * Service manager logger
35
+ * @type {Logger}
36
+ */
37
+ logger: Logger;
38
+ /**
39
+ * Service dependency graph
40
+ * @type {Map<string, Set<string>>}
41
+ * @private
42
+ */
43
+ private dependencyGraph;
44
+ /**
45
+ * Register an event handler
46
+ *
47
+ * @param {string} eventName - Event name
48
+ * @param {Function} handler - Event handler
49
+ * @returns {Function} Unsubscribe function
50
+ */
51
+ on(eventName: string, handler: Function): Function;
52
+ /**
53
+ * Remove an event handler
54
+ *
55
+ * @param {string} eventName - Event name
56
+ * @param {Function} handler - Event handler
57
+ * @returns {boolean} True if handler was removed
58
+ */
59
+ off(eventName: string, handler: Function): boolean;
60
+ /**
61
+ * Emit an event
62
+ *
63
+ * @param {string} eventName - Event name
64
+ * @param {*} data - Event data
65
+ * @returns {boolean} True if event had listeners
66
+ * @private
67
+ */
68
+ private _emit;
69
+ /**
70
+ * Register a service
71
+ *
72
+ * @param {string} name - Service name
73
+ * @param {Object} instance - Service instance
74
+ * @param {Object} [options] - Registration options
75
+ * @param {string[]} [options.dependencies=[]] - Service dependencies
76
+ * @param {Object} [options.config={}] - Service configuration
77
+ * @returns {boolean} True if registration was successful
78
+ *
79
+ * @example
80
+ * manager.register('database', new DatabaseService());
81
+ * manager.register('api', new ApiService(), {
82
+ * dependencies: ['database'],
83
+ * config: { port: 3000 }
84
+ * });
85
+ */
86
+ register(name: string, instance: any, options?: {
87
+ dependencies?: string[];
88
+ config?: any;
89
+ }): boolean;
90
+ /**
91
+ * Unregister a service
92
+ *
93
+ * @param {string} name - Service name
94
+ * @returns {boolean} True if unregistration was successful
95
+ *
96
+ * @example
97
+ * manager.unregister('api');
98
+ */
99
+ unregister(name: string): boolean;
100
+ /**
101
+ * Get a service instance
102
+ *
103
+ * @param {string} name - Service name
104
+ * @returns {Object|null} Service instance or null if not found
105
+ *
106
+ * @example
107
+ * const db = manager.getService('database');
108
+ * await db.query('SELECT * FROM users');
109
+ */
110
+ getService(name: string): any | null;
111
+ /**
112
+ * Initialize a specific service
113
+ *
114
+ * @param {string} name - Service name
115
+ * @param {Object} [config] - Service configuration (overrides config from
116
+ * registration)
117
+ * @returns {Promise<boolean>} True if initialization was successful
118
+ *
119
+ * @example
120
+ * await manager.initializeService('database', {
121
+ * connectionString: 'mongodb://localhost:27017'
122
+ * });
123
+ */
124
+ initializeService(name: string, config?: any): Promise<boolean>;
125
+ /**
126
+ * Initialize all registered services
127
+ *
128
+ * @param {Object} [configs] - Configuration map for services
129
+ * @returns {Promise<boolean>} True if all services initialized successfully
130
+ *
131
+ * @example
132
+ * await manager.initializeAll({
133
+ * database: { connectionString: 'mongodb://localhost:27017' },
134
+ * api: { port: 3000 }
135
+ * });
136
+ */
137
+ initializeAll(configs?: any): Promise<boolean>;
138
+ /**
139
+ * Start a specific service and its dependencies
140
+ *
141
+ * @param {string} name - Service name
142
+ * @returns {Promise<boolean>} True if start was successful
143
+ *
144
+ * @example
145
+ * await manager.startService('api');
146
+ */
147
+ startService(name: string): Promise<boolean>;
148
+ /**
149
+ * Start all services in dependency order
150
+ *
151
+ * @returns {Promise<boolean>} True if all services started successfully
152
+ *
153
+ * @example
154
+ * await manager.startAll();
155
+ */
156
+ startAll(): Promise<boolean>;
157
+ /**
158
+ * Stop a specific service and services that depend on it
159
+ *
160
+ * @param {string} name - Service name
161
+ * @param {Object} [options] - Stop options
162
+ * @param {boolean} [options.force=false] - Force stop even with dependents
163
+ * @returns {Promise<boolean>} True if stop was successful
164
+ *
165
+ * @example
166
+ * await manager.stopService('database');
167
+ */
168
+ stopService(name: string, options?: {
169
+ force?: boolean;
170
+ }): Promise<boolean>;
171
+ /**
172
+ * Stop all services in reverse dependency order
173
+ *
174
+ * @param {Object} [options] - Stop options
175
+ * @param {boolean} [options.force=false] - Force stop even with errors
176
+ * @returns {Promise<boolean>} True if all services stopped successfully
177
+ *
178
+ * @example
179
+ * await manager.stopAll();
180
+ */
181
+ stopAll(options?: {
182
+ force?: boolean;
183
+ }): Promise<boolean>;
184
+ /**
185
+ * Destroy a service and remove it from the manager
186
+ *
187
+ * @param {string} name - Service name
188
+ * @param {Object} [options] - Destroy options
189
+ * @param {boolean} [options.force=false] - Force destroy even with dependents
190
+ * @returns {Promise<boolean>} True if service was destroyed
191
+ *
192
+ * @example
193
+ * await manager.destroyService('api');
194
+ */
195
+ destroyService(name: string, options?: {
196
+ force?: boolean;
197
+ }): Promise<boolean>;
198
+ /**
199
+ * Destroy all services and shutdown the manager
200
+ *
201
+ * @param {Object} [options] - Destroy options
202
+ * @param {boolean} [options.force=false] - Force destroy even with errors
203
+ * @returns {Promise<boolean>} True if all services were destroyed
204
+ *
205
+ * @example
206
+ * await manager.destroyAll();
207
+ */
208
+ destroyAll(options?: {
209
+ force?: boolean;
210
+ }): Promise<boolean>;
211
+ /**
212
+ * Recover a service and its dependencies from error state
213
+ *
214
+ * @param {string} name - Service name
215
+ * @param {Object} [options] - Recovery options
216
+ * @param {boolean} [options.recursive=true] - Recursively recover dependencies
217
+ * @param {boolean} [options.autoStart=true] - Auto-start service after recovery
218
+ * @returns {Promise<boolean>} True if recovery was successful
219
+ *
220
+ * @example
221
+ * // Recover a service and its dependencies
222
+ * await manager.recoverService('api');
223
+ *
224
+ * // Recover just this service without auto-starting
225
+ * await manager.recoverService('database', {
226
+ * recursive: false,
227
+ * autoStart: false
228
+ * });
229
+ */
230
+ recoverService(name: string, options?: {
231
+ recursive?: boolean;
232
+ autoStart?: boolean;
233
+ }): Promise<boolean>;
234
+ /**
235
+ * Recover all services in dependency order
236
+ *
237
+ * @param {Object} [options] - Recovery options
238
+ * @param {boolean} [options.autoStart=true] - Auto-start services after recovery
239
+ * @returns {Promise<boolean>} True if all recoveries were successful
240
+ *
241
+ * @example
242
+ * // Recover all services and auto-start them
243
+ * await manager.recoverAll();
244
+ *
245
+ * // Recover all services without auto-starting
246
+ * await manager.recoverAll({ autoStart: false });
247
+ */
248
+ recoverAll(options?: {
249
+ autoStart?: boolean;
250
+ }): Promise<boolean>;
251
+ /**
252
+ * Set log level for a specific service or all services
253
+ *
254
+ * @param {string} level - New log level
255
+ * @param {string} [serviceName] - Service to set level for, or all if omitted
256
+ * @returns {boolean} True if level was set successfully
257
+ *
258
+ * @example
259
+ * // Set level for specific service
260
+ * manager.setLogLevel(DEBUG, 'database');
261
+ *
262
+ * // Set level for all services including manager
263
+ * manager.setLogLevel(INFO);
264
+ */
265
+ setLogLevel(level: string, serviceName?: string): boolean;
266
+ /**
267
+ * Get the names of all registered services
268
+ *
269
+ * @returns {string[]} List of service names
270
+ *
271
+ * @example
272
+ * const services = manager.getServiceNames();
273
+ * console.log(`Registered services: ${services.join(', ')}`);
274
+ */
275
+ getServiceNames(): string[];
276
+ /**
277
+ * Get service status information
278
+ *
279
+ * @param {string} [name] - Service name, or all if omitted
280
+ * @returns {Object|Array|null} Service status or null if not found
281
+ *
282
+ * @example
283
+ * // Get status for all services
284
+ * const allStatus = manager.getServiceStatus();
285
+ *
286
+ * // Get status for specific service
287
+ * const dbStatus = manager.getServiceStatus('database');
288
+ * console.log(`Database state: ${dbStatus.state}`);
289
+ */
290
+ getServiceStatus(name?: string): any | any[] | null;
291
+ /**
292
+ * Handle state change events from services
293
+ *
294
+ * @private
295
+ * @param {string} serviceName - Service name
296
+ * @param {Object} event - State change event
297
+ */
298
+ private _handleStateChanged;
299
+ /**
300
+ * Handle error events from services
301
+ *
302
+ * @private
303
+ * @param {string} serviceName - Service name
304
+ * @param {Object} event - Error event
305
+ */
306
+ private _handleError;
307
+ /**
308
+ * Update the dependency graph
309
+ *
310
+ * @private
311
+ */
312
+ private _updateDependencyGraph;
313
+ /**
314
+ * Check for circular dependencies in the graph
315
+ *
316
+ * @private
317
+ */
318
+ private _checkForCircularDependencies;
319
+ /**
320
+ * Get optimal service start order based on dependencies
321
+ *
322
+ * @private
323
+ * @returns {string[]} Services in dependency order
324
+ */
325
+ private _getStartOrder;
326
+ }
327
+ export type ServiceEntry = {
328
+ /**
329
+ * - Service instance
330
+ */
331
+ instance: any;
332
+ /**
333
+ * - Service configuration
334
+ */
335
+ config: any;
336
+ /**
337
+ * - List of service dependencies
338
+ */
339
+ dependencies: string[];
340
+ /**
341
+ * - Unsubscribe function for state events
342
+ */
343
+ stateChangedUnsubscribe: Function;
344
+ /**
345
+ * - Unsubscribe function for error events
346
+ */
347
+ errorUnsubscribe: Function;
348
+ };
349
+ import { EventEmitter } from '../events';
350
+ import { Logger } from '../logging';