@opensourcekd/ng-common-libs 2.0.5 → 2.0.7
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.
- package/README.md +6 -0
- package/dist/index.cjs +273 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +249 -3
- package/dist/index.mjs +273 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,12 @@ interface EventPayload<T = unknown> {
|
|
|
8
8
|
data: T;
|
|
9
9
|
timestamp?: number;
|
|
10
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* EventBus options interface
|
|
13
|
+
*/
|
|
14
|
+
interface EventBusOptions {
|
|
15
|
+
id?: string;
|
|
16
|
+
}
|
|
11
17
|
/**
|
|
12
18
|
* EventBus - A centralized event bus for application-wide communication
|
|
13
19
|
* Framework-agnostic implementation using only RxJS
|
|
@@ -17,6 +23,9 @@ interface EventPayload<T = unknown> {
|
|
|
17
23
|
* // Create an instance
|
|
18
24
|
* const eventBus = new EventBus();
|
|
19
25
|
*
|
|
26
|
+
* // Create an instance with an identifier
|
|
27
|
+
* const eventBus = new EventBus({ id: 'MFE' });
|
|
28
|
+
*
|
|
20
29
|
* // Emit an event
|
|
21
30
|
* eventBus.emit('user:login', { userId: '123', username: 'john' });
|
|
22
31
|
*
|
|
@@ -24,10 +33,24 @@ interface EventPayload<T = unknown> {
|
|
|
24
33
|
* eventBus.on('user:login').subscribe(data => {
|
|
25
34
|
* console.log('User logged in:', data);
|
|
26
35
|
* });
|
|
36
|
+
*
|
|
37
|
+
* // Get the identifier
|
|
38
|
+
* const id = eventBus.getId(); // 'MFE' or undefined
|
|
27
39
|
* ```
|
|
28
40
|
*/
|
|
29
41
|
declare class EventBus {
|
|
30
42
|
private eventSubject;
|
|
43
|
+
private id?;
|
|
44
|
+
/**
|
|
45
|
+
* Create a new EventBus instance
|
|
46
|
+
* @param options - Optional configuration with an id to identify the instance
|
|
47
|
+
*/
|
|
48
|
+
constructor(options?: EventBusOptions);
|
|
49
|
+
/**
|
|
50
|
+
* Get the identifier of this EventBus instance
|
|
51
|
+
* @returns The id if provided during initialization, undefined otherwise
|
|
52
|
+
*/
|
|
53
|
+
getId(): string | undefined;
|
|
31
54
|
/**
|
|
32
55
|
* Emit an event with optional data
|
|
33
56
|
* @param eventType - The type/name of the event
|
|
@@ -243,6 +266,12 @@ interface Auth0Config {
|
|
|
243
266
|
scope: string;
|
|
244
267
|
connection?: string;
|
|
245
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* AuthService options interface
|
|
271
|
+
*/
|
|
272
|
+
interface AuthServiceOptions {
|
|
273
|
+
id?: string;
|
|
274
|
+
}
|
|
246
275
|
/**
|
|
247
276
|
* Storage configuration
|
|
248
277
|
*/
|
|
@@ -280,10 +309,16 @@ interface StorageKeys {
|
|
|
280
309
|
* };
|
|
281
310
|
* const authService = new AuthService(authConfig, eventBus);
|
|
282
311
|
*
|
|
312
|
+
* // Or create with an identifier
|
|
313
|
+
* const authService = new AuthService(authConfig, eventBus, undefined, undefined, { id: 'MFE' });
|
|
314
|
+
*
|
|
283
315
|
* // Use the service
|
|
284
316
|
* await authService.login();
|
|
285
317
|
* const user = authService.getUser();
|
|
286
318
|
* const token = await authService.getToken();
|
|
319
|
+
*
|
|
320
|
+
* // Get the identifier
|
|
321
|
+
* const id = authService.getId(); // 'MFE' or undefined
|
|
287
322
|
* ```
|
|
288
323
|
*/
|
|
289
324
|
declare class AuthService {
|
|
@@ -296,14 +331,21 @@ declare class AuthService {
|
|
|
296
331
|
private storageConfig;
|
|
297
332
|
private storageKeys;
|
|
298
333
|
private eventBus;
|
|
334
|
+
private id?;
|
|
299
335
|
/**
|
|
300
336
|
* Create a new AuthService instance
|
|
301
337
|
* @param config - Auth0 configuration
|
|
302
338
|
* @param eventBus - EventBus instance for emitting auth events
|
|
303
339
|
* @param storageConfig - Storage configuration (optional, defaults to sessionStorage)
|
|
304
340
|
* @param storageKeys - Storage keys (optional, defaults to standard keys)
|
|
341
|
+
* @param options - Optional configuration with an id to identify the instance
|
|
305
342
|
*/
|
|
306
|
-
constructor(config: Auth0Config, eventBus: EventBus, storageConfig?: StorageConfig, storageKeys?: StorageKeys);
|
|
343
|
+
constructor(config: Auth0Config, eventBus: EventBus, storageConfig?: StorageConfig, storageKeys?: StorageKeys, options?: AuthServiceOptions);
|
|
344
|
+
/**
|
|
345
|
+
* Get the identifier of this AuthService instance
|
|
346
|
+
* @returns The id if provided during initialization, undefined otherwise
|
|
347
|
+
*/
|
|
348
|
+
getId(): string | undefined;
|
|
307
349
|
/**
|
|
308
350
|
* Initialize Auth0 client
|
|
309
351
|
*/
|
|
@@ -377,6 +419,11 @@ declare class AuthService {
|
|
|
377
419
|
* Emit authentication event for cross-application communication
|
|
378
420
|
*/
|
|
379
421
|
private emitAuthEvent;
|
|
422
|
+
/**
|
|
423
|
+
* Clean up OAuth callback parameters from URL after successful authentication
|
|
424
|
+
* Removes 'code' and 'state' parameters while preserving other query parameters
|
|
425
|
+
*/
|
|
426
|
+
private cleanupCallbackUrl;
|
|
380
427
|
}
|
|
381
428
|
/**
|
|
382
429
|
* Create AuthService instance using AUTH0_CONFIG
|
|
@@ -405,5 +452,204 @@ declare class AuthService {
|
|
|
405
452
|
*/
|
|
406
453
|
declare function createAuthService(eventBus: EventBus): AuthService;
|
|
407
454
|
|
|
408
|
-
|
|
409
|
-
|
|
455
|
+
/**
|
|
456
|
+
* OpenTelemetry severity levels
|
|
457
|
+
* Based on OpenTelemetry log data model
|
|
458
|
+
* @see https://opentelemetry.io/docs/specs/otel/logs/data-model/
|
|
459
|
+
*/
|
|
460
|
+
declare enum LogSeverity {
|
|
461
|
+
TRACE = 1,
|
|
462
|
+
DEBUG = 5,
|
|
463
|
+
INFO = 9,
|
|
464
|
+
WARN = 13,
|
|
465
|
+
ERROR = 17,
|
|
466
|
+
FATAL = 21
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* OpenTelemetry log record attributes
|
|
470
|
+
* Structured key-value pairs for additional context
|
|
471
|
+
*/
|
|
472
|
+
interface LogAttributes {
|
|
473
|
+
[key: string]: string | number | boolean | undefined | null;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* OpenTelemetry log record
|
|
477
|
+
* Conforms to OTel log data model specification
|
|
478
|
+
*/
|
|
479
|
+
interface LogRecord {
|
|
480
|
+
/** Timestamp in milliseconds since Unix epoch */
|
|
481
|
+
timestamp: number;
|
|
482
|
+
/** Severity level number */
|
|
483
|
+
severityNumber: LogSeverity;
|
|
484
|
+
/** Severity level text */
|
|
485
|
+
severityText: string;
|
|
486
|
+
/** Log message body */
|
|
487
|
+
body: string;
|
|
488
|
+
/** Structured attributes for additional context */
|
|
489
|
+
attributes?: LogAttributes;
|
|
490
|
+
/** Trace ID if available (for correlation) */
|
|
491
|
+
traceId?: string;
|
|
492
|
+
/** Span ID if available (for correlation) */
|
|
493
|
+
spanId?: string;
|
|
494
|
+
/** Resource attributes (e.g., service.name, deployment.environment) */
|
|
495
|
+
resource?: LogAttributes;
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Logger options interface
|
|
499
|
+
*/
|
|
500
|
+
interface LoggerOptions {
|
|
501
|
+
/** Logger identifier (e.g., service name, component name) */
|
|
502
|
+
name?: string;
|
|
503
|
+
/** Minimum severity level to log (logs below this level are ignored) */
|
|
504
|
+
minSeverity?: LogSeverity;
|
|
505
|
+
/** Resource attributes applied to all logs from this logger */
|
|
506
|
+
resource?: LogAttributes;
|
|
507
|
+
/** Whether to output logs to console (default: true) */
|
|
508
|
+
consoleOutput?: boolean;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Logger - OpenTelemetry-compliant structured logging
|
|
512
|
+
* Framework-agnostic implementation using RxJS for observability
|
|
513
|
+
*
|
|
514
|
+
* Conforms to OpenTelemetry log data model:
|
|
515
|
+
* - Structured logging with severity levels
|
|
516
|
+
* - Timestamp in milliseconds
|
|
517
|
+
* - Support for attributes and resource context
|
|
518
|
+
* - Observable log stream for integration with OTel exporters
|
|
519
|
+
*
|
|
520
|
+
* @example
|
|
521
|
+
* ```typescript
|
|
522
|
+
* // Create a logger instance
|
|
523
|
+
* const logger = new Logger({
|
|
524
|
+
* name: 'MyService',
|
|
525
|
+
* minSeverity: LogSeverity.INFO,
|
|
526
|
+
* resource: {
|
|
527
|
+
* 'service.name': 'my-app',
|
|
528
|
+
* 'deployment.environment': 'production'
|
|
529
|
+
* }
|
|
530
|
+
* });
|
|
531
|
+
*
|
|
532
|
+
* // Simple logging
|
|
533
|
+
* logger.info('User logged in');
|
|
534
|
+
* logger.error('Failed to fetch data');
|
|
535
|
+
*
|
|
536
|
+
* // Structured logging with attributes
|
|
537
|
+
* logger.info('User action', {
|
|
538
|
+
* userId: '123',
|
|
539
|
+
* action: 'purchase',
|
|
540
|
+
* amount: 99.99
|
|
541
|
+
* });
|
|
542
|
+
*
|
|
543
|
+
* // Subscribe to log stream (e.g., for sending to OTel collector)
|
|
544
|
+
* logger.getLogStream().subscribe(logRecord => {
|
|
545
|
+
* // Send to OTel exporter, custom backend, etc.
|
|
546
|
+
* console.log(JSON.stringify(logRecord));
|
|
547
|
+
* });
|
|
548
|
+
*
|
|
549
|
+
* // Log with trace correlation
|
|
550
|
+
* logger.info('Processing request',
|
|
551
|
+
* { requestId: 'abc-123' },
|
|
552
|
+
* { traceId: '1234...', spanId: '5678...' }
|
|
553
|
+
* );
|
|
554
|
+
* ```
|
|
555
|
+
*/
|
|
556
|
+
declare class Logger {
|
|
557
|
+
private logSubject;
|
|
558
|
+
private name?;
|
|
559
|
+
private minSeverity;
|
|
560
|
+
private resource?;
|
|
561
|
+
private consoleOutput;
|
|
562
|
+
/**
|
|
563
|
+
* Create a new Logger instance
|
|
564
|
+
* @param options - Optional configuration for the logger
|
|
565
|
+
*/
|
|
566
|
+
constructor(options?: LoggerOptions);
|
|
567
|
+
/**
|
|
568
|
+
* Get the logger name
|
|
569
|
+
* @returns The logger name if provided during initialization
|
|
570
|
+
*/
|
|
571
|
+
getName(): string | undefined;
|
|
572
|
+
/**
|
|
573
|
+
* Get the observable log stream
|
|
574
|
+
* Subscribe to this to receive all log records emitted by this logger
|
|
575
|
+
* @returns Observable that emits LogRecord objects
|
|
576
|
+
*/
|
|
577
|
+
getLogStream(): Observable<LogRecord>;
|
|
578
|
+
/**
|
|
579
|
+
* Emit a log record
|
|
580
|
+
* @param severity - Severity level
|
|
581
|
+
* @param severityText - Severity level text
|
|
582
|
+
* @param body - Log message
|
|
583
|
+
* @param attributes - Optional structured attributes
|
|
584
|
+
* @param context - Optional trace context (traceId, spanId)
|
|
585
|
+
*/
|
|
586
|
+
private log;
|
|
587
|
+
/**
|
|
588
|
+
* Output log to console
|
|
589
|
+
* @param logRecord - The log record to output
|
|
590
|
+
*/
|
|
591
|
+
private outputToConsole;
|
|
592
|
+
/**
|
|
593
|
+
* Log a TRACE level message
|
|
594
|
+
* @param body - Log message
|
|
595
|
+
* @param attributes - Optional structured attributes
|
|
596
|
+
* @param context - Optional trace context
|
|
597
|
+
*/
|
|
598
|
+
trace(body: string, attributes?: LogAttributes, context?: {
|
|
599
|
+
traceId?: string;
|
|
600
|
+
spanId?: string;
|
|
601
|
+
}): void;
|
|
602
|
+
/**
|
|
603
|
+
* Log a DEBUG level message
|
|
604
|
+
* @param body - Log message
|
|
605
|
+
* @param attributes - Optional structured attributes
|
|
606
|
+
* @param context - Optional trace context
|
|
607
|
+
*/
|
|
608
|
+
debug(body: string, attributes?: LogAttributes, context?: {
|
|
609
|
+
traceId?: string;
|
|
610
|
+
spanId?: string;
|
|
611
|
+
}): void;
|
|
612
|
+
/**
|
|
613
|
+
* Log an INFO level message
|
|
614
|
+
* @param body - Log message
|
|
615
|
+
* @param attributes - Optional structured attributes
|
|
616
|
+
* @param context - Optional trace context
|
|
617
|
+
*/
|
|
618
|
+
info(body: string, attributes?: LogAttributes, context?: {
|
|
619
|
+
traceId?: string;
|
|
620
|
+
spanId?: string;
|
|
621
|
+
}): void;
|
|
622
|
+
/**
|
|
623
|
+
* Log a WARN level message
|
|
624
|
+
* @param body - Log message
|
|
625
|
+
* @param attributes - Optional structured attributes
|
|
626
|
+
* @param context - Optional trace context
|
|
627
|
+
*/
|
|
628
|
+
warn(body: string, attributes?: LogAttributes, context?: {
|
|
629
|
+
traceId?: string;
|
|
630
|
+
spanId?: string;
|
|
631
|
+
}): void;
|
|
632
|
+
/**
|
|
633
|
+
* Log an ERROR level message
|
|
634
|
+
* @param body - Log message
|
|
635
|
+
* @param attributes - Optional structured attributes
|
|
636
|
+
* @param context - Optional trace context
|
|
637
|
+
*/
|
|
638
|
+
error(body: string, attributes?: LogAttributes, context?: {
|
|
639
|
+
traceId?: string;
|
|
640
|
+
spanId?: string;
|
|
641
|
+
}): void;
|
|
642
|
+
/**
|
|
643
|
+
* Log a FATAL level message
|
|
644
|
+
* @param body - Log message
|
|
645
|
+
* @param attributes - Optional structured attributes
|
|
646
|
+
* @param context - Optional trace context
|
|
647
|
+
*/
|
|
648
|
+
fatal(body: string, attributes?: LogAttributes, context?: {
|
|
649
|
+
traceId?: string;
|
|
650
|
+
spanId?: string;
|
|
651
|
+
}): void;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
export { APP_CONFIG, AUTH0_CONFIG, AuthService, EventBus, LogSeverity, Logger, STORAGE_CONFIG, STORAGE_KEYS, configureAuth0, createAuthService, getStorageItem, removeStorageItem, resetAuth0Config, setStorageItem };
|
|
655
|
+
export type { AppState, Auth0Config, Auth0ConfigOptions, AuthServiceOptions, AuthorizationParams, CallbackResult, EventBusOptions, EventPayload, LogAttributes, LogRecord, LoggerOptions, StorageConfig, StorageKeys, UserData, UserInfo };
|
package/dist/index.mjs
CHANGED
|
@@ -10,6 +10,9 @@ import { filter, map } from 'rxjs/operators';
|
|
|
10
10
|
* // Create an instance
|
|
11
11
|
* const eventBus = new EventBus();
|
|
12
12
|
*
|
|
13
|
+
* // Create an instance with an identifier
|
|
14
|
+
* const eventBus = new EventBus({ id: 'MFE' });
|
|
15
|
+
*
|
|
13
16
|
* // Emit an event
|
|
14
17
|
* eventBus.emit('user:login', { userId: '123', username: 'john' });
|
|
15
18
|
*
|
|
@@ -17,10 +20,28 @@ import { filter, map } from 'rxjs/operators';
|
|
|
17
20
|
* eventBus.on('user:login').subscribe(data => {
|
|
18
21
|
* console.log('User logged in:', data);
|
|
19
22
|
* });
|
|
23
|
+
*
|
|
24
|
+
* // Get the identifier
|
|
25
|
+
* const id = eventBus.getId(); // 'MFE' or undefined
|
|
20
26
|
* ```
|
|
21
27
|
*/
|
|
22
28
|
class EventBus {
|
|
23
29
|
eventSubject = new Subject();
|
|
30
|
+
id;
|
|
31
|
+
/**
|
|
32
|
+
* Create a new EventBus instance
|
|
33
|
+
* @param options - Optional configuration with an id to identify the instance
|
|
34
|
+
*/
|
|
35
|
+
constructor(options) {
|
|
36
|
+
this.id = options?.id;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get the identifier of this EventBus instance
|
|
40
|
+
* @returns The id if provided during initialization, undefined otherwise
|
|
41
|
+
*/
|
|
42
|
+
getId() {
|
|
43
|
+
return this.id;
|
|
44
|
+
}
|
|
24
45
|
/**
|
|
25
46
|
* Emit an event with optional data
|
|
26
47
|
* @param eventType - The type/name of the event
|
|
@@ -243,10 +264,16 @@ function removeStorageItem(key, storageType = 'sessionStorage') {
|
|
|
243
264
|
* };
|
|
244
265
|
* const authService = new AuthService(authConfig, eventBus);
|
|
245
266
|
*
|
|
267
|
+
* // Or create with an identifier
|
|
268
|
+
* const authService = new AuthService(authConfig, eventBus, undefined, undefined, { id: 'MFE' });
|
|
269
|
+
*
|
|
246
270
|
* // Use the service
|
|
247
271
|
* await authService.login();
|
|
248
272
|
* const user = authService.getUser();
|
|
249
273
|
* const token = await authService.getToken();
|
|
274
|
+
*
|
|
275
|
+
* // Get the identifier
|
|
276
|
+
* const id = authService.getId(); // 'MFE' or undefined
|
|
250
277
|
* ```
|
|
251
278
|
*/
|
|
252
279
|
class AuthService {
|
|
@@ -265,12 +292,14 @@ class AuthService {
|
|
|
265
292
|
storageConfig;
|
|
266
293
|
storageKeys;
|
|
267
294
|
eventBus;
|
|
295
|
+
id;
|
|
268
296
|
/**
|
|
269
297
|
* Create a new AuthService instance
|
|
270
298
|
* @param config - Auth0 configuration
|
|
271
299
|
* @param eventBus - EventBus instance for emitting auth events
|
|
272
300
|
* @param storageConfig - Storage configuration (optional, defaults to sessionStorage)
|
|
273
301
|
* @param storageKeys - Storage keys (optional, defaults to standard keys)
|
|
302
|
+
* @param options - Optional configuration with an id to identify the instance
|
|
274
303
|
*/
|
|
275
304
|
constructor(config, eventBus, storageConfig = {
|
|
276
305
|
TOKEN_STORAGE: 'sessionStorage',
|
|
@@ -278,16 +307,24 @@ class AuthService {
|
|
|
278
307
|
}, storageKeys = {
|
|
279
308
|
ACCESS_TOKEN: 'auth0_access_token',
|
|
280
309
|
USER_INFO: 'auth0_user_info'
|
|
281
|
-
}) {
|
|
310
|
+
}, options) {
|
|
282
311
|
this.config = config;
|
|
283
312
|
this.eventBus = eventBus;
|
|
284
313
|
this.storageConfig = storageConfig;
|
|
285
314
|
this.storageKeys = storageKeys;
|
|
315
|
+
this.id = options?.id;
|
|
286
316
|
this.userSubject = new BehaviorSubject(this.getUserInfoFromStorage());
|
|
287
317
|
this.user$ = this.userSubject.asObservable();
|
|
288
318
|
console.log("[AuthService] AuthService instance created (Auth0 client will be initialized on first use)");
|
|
289
319
|
// Lazy initialization - Auth0 client will be initialized in ensureInitialized() on first use
|
|
290
320
|
}
|
|
321
|
+
/**
|
|
322
|
+
* Get the identifier of this AuthService instance
|
|
323
|
+
* @returns The id if provided during initialization, undefined otherwise
|
|
324
|
+
*/
|
|
325
|
+
getId() {
|
|
326
|
+
return this.id;
|
|
327
|
+
}
|
|
291
328
|
/**
|
|
292
329
|
* Initialize Auth0 client
|
|
293
330
|
*/
|
|
@@ -404,6 +441,8 @@ class AuthService {
|
|
|
404
441
|
}
|
|
405
442
|
const token = await this.auth0Client.getTokenSilently();
|
|
406
443
|
this.setToken(token);
|
|
444
|
+
// Clean up OAuth callback parameters from URL
|
|
445
|
+
this.cleanupCallbackUrl();
|
|
407
446
|
console.log("[AuthService] Authentication successful");
|
|
408
447
|
this.emitAuthEvent('login_success', { user, appState: result.appState });
|
|
409
448
|
return { success: true, appState: result.appState };
|
|
@@ -626,6 +665,34 @@ class AuthService {
|
|
|
626
665
|
this.eventBus.emit(event.type, event);
|
|
627
666
|
console.log('[AuthService] Auth event emitted:', event.type);
|
|
628
667
|
}
|
|
668
|
+
/**
|
|
669
|
+
* Clean up OAuth callback parameters from URL after successful authentication
|
|
670
|
+
* Removes 'code' and 'state' parameters while preserving other query parameters
|
|
671
|
+
*/
|
|
672
|
+
cleanupCallbackUrl() {
|
|
673
|
+
try {
|
|
674
|
+
const url = new URL(window.location.href);
|
|
675
|
+
const params = new URLSearchParams(url.search);
|
|
676
|
+
// Check if OAuth params exist
|
|
677
|
+
const hasCode = params.has('code');
|
|
678
|
+
const hasState = params.has('state');
|
|
679
|
+
if (hasCode || hasState) {
|
|
680
|
+
// Remove OAuth callback parameters
|
|
681
|
+
params.delete('code');
|
|
682
|
+
params.delete('state');
|
|
683
|
+
// Construct new URL without OAuth params
|
|
684
|
+
const newSearch = params.toString();
|
|
685
|
+
const newUrl = `${url.pathname}${newSearch ? '?' + newSearch : ''}${url.hash}`;
|
|
686
|
+
// Replace URL without adding to browser history
|
|
687
|
+
window.history.replaceState({}, '', newUrl);
|
|
688
|
+
console.log('[AuthService] OAuth callback parameters cleaned from URL');
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
catch (error) {
|
|
692
|
+
console.warn('[AuthService] Failed to clean up callback URL:', error);
|
|
693
|
+
// Don't throw - URL cleanup is not critical for auth functionality
|
|
694
|
+
}
|
|
695
|
+
}
|
|
629
696
|
}
|
|
630
697
|
/**
|
|
631
698
|
* Create AuthService instance using AUTH0_CONFIG
|
|
@@ -665,5 +732,209 @@ function createAuthService(eventBus) {
|
|
|
665
732
|
return new AuthService(auth0Config, eventBus, STORAGE_CONFIG, STORAGE_KEYS);
|
|
666
733
|
}
|
|
667
734
|
|
|
668
|
-
|
|
735
|
+
/**
|
|
736
|
+
* OpenTelemetry severity levels
|
|
737
|
+
* Based on OpenTelemetry log data model
|
|
738
|
+
* @see https://opentelemetry.io/docs/specs/otel/logs/data-model/
|
|
739
|
+
*/
|
|
740
|
+
var LogSeverity;
|
|
741
|
+
(function (LogSeverity) {
|
|
742
|
+
LogSeverity[LogSeverity["TRACE"] = 1] = "TRACE";
|
|
743
|
+
LogSeverity[LogSeverity["DEBUG"] = 5] = "DEBUG";
|
|
744
|
+
LogSeverity[LogSeverity["INFO"] = 9] = "INFO";
|
|
745
|
+
LogSeverity[LogSeverity["WARN"] = 13] = "WARN";
|
|
746
|
+
LogSeverity[LogSeverity["ERROR"] = 17] = "ERROR";
|
|
747
|
+
LogSeverity[LogSeverity["FATAL"] = 21] = "FATAL";
|
|
748
|
+
})(LogSeverity || (LogSeverity = {}));
|
|
749
|
+
/**
|
|
750
|
+
* Logger - OpenTelemetry-compliant structured logging
|
|
751
|
+
* Framework-agnostic implementation using RxJS for observability
|
|
752
|
+
*
|
|
753
|
+
* Conforms to OpenTelemetry log data model:
|
|
754
|
+
* - Structured logging with severity levels
|
|
755
|
+
* - Timestamp in milliseconds
|
|
756
|
+
* - Support for attributes and resource context
|
|
757
|
+
* - Observable log stream for integration with OTel exporters
|
|
758
|
+
*
|
|
759
|
+
* @example
|
|
760
|
+
* ```typescript
|
|
761
|
+
* // Create a logger instance
|
|
762
|
+
* const logger = new Logger({
|
|
763
|
+
* name: 'MyService',
|
|
764
|
+
* minSeverity: LogSeverity.INFO,
|
|
765
|
+
* resource: {
|
|
766
|
+
* 'service.name': 'my-app',
|
|
767
|
+
* 'deployment.environment': 'production'
|
|
768
|
+
* }
|
|
769
|
+
* });
|
|
770
|
+
*
|
|
771
|
+
* // Simple logging
|
|
772
|
+
* logger.info('User logged in');
|
|
773
|
+
* logger.error('Failed to fetch data');
|
|
774
|
+
*
|
|
775
|
+
* // Structured logging with attributes
|
|
776
|
+
* logger.info('User action', {
|
|
777
|
+
* userId: '123',
|
|
778
|
+
* action: 'purchase',
|
|
779
|
+
* amount: 99.99
|
|
780
|
+
* });
|
|
781
|
+
*
|
|
782
|
+
* // Subscribe to log stream (e.g., for sending to OTel collector)
|
|
783
|
+
* logger.getLogStream().subscribe(logRecord => {
|
|
784
|
+
* // Send to OTel exporter, custom backend, etc.
|
|
785
|
+
* console.log(JSON.stringify(logRecord));
|
|
786
|
+
* });
|
|
787
|
+
*
|
|
788
|
+
* // Log with trace correlation
|
|
789
|
+
* logger.info('Processing request',
|
|
790
|
+
* { requestId: 'abc-123' },
|
|
791
|
+
* { traceId: '1234...', spanId: '5678...' }
|
|
792
|
+
* );
|
|
793
|
+
* ```
|
|
794
|
+
*/
|
|
795
|
+
class Logger {
|
|
796
|
+
logSubject = new Subject();
|
|
797
|
+
name;
|
|
798
|
+
minSeverity;
|
|
799
|
+
resource;
|
|
800
|
+
consoleOutput;
|
|
801
|
+
/**
|
|
802
|
+
* Create a new Logger instance
|
|
803
|
+
* @param options - Optional configuration for the logger
|
|
804
|
+
*/
|
|
805
|
+
constructor(options) {
|
|
806
|
+
this.name = options?.name;
|
|
807
|
+
this.minSeverity = options?.minSeverity ?? LogSeverity.TRACE;
|
|
808
|
+
this.resource = options?.resource;
|
|
809
|
+
this.consoleOutput = options?.consoleOutput ?? true;
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Get the logger name
|
|
813
|
+
* @returns The logger name if provided during initialization
|
|
814
|
+
*/
|
|
815
|
+
getName() {
|
|
816
|
+
return this.name;
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Get the observable log stream
|
|
820
|
+
* Subscribe to this to receive all log records emitted by this logger
|
|
821
|
+
* @returns Observable that emits LogRecord objects
|
|
822
|
+
*/
|
|
823
|
+
getLogStream() {
|
|
824
|
+
return this.logSubject.asObservable();
|
|
825
|
+
}
|
|
826
|
+
/**
|
|
827
|
+
* Emit a log record
|
|
828
|
+
* @param severity - Severity level
|
|
829
|
+
* @param severityText - Severity level text
|
|
830
|
+
* @param body - Log message
|
|
831
|
+
* @param attributes - Optional structured attributes
|
|
832
|
+
* @param context - Optional trace context (traceId, spanId)
|
|
833
|
+
*/
|
|
834
|
+
log(severity, severityText, body, attributes, context) {
|
|
835
|
+
// Check if we should log based on minimum severity
|
|
836
|
+
if (severity < this.minSeverity) {
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
const logRecord = {
|
|
840
|
+
timestamp: Date.now(),
|
|
841
|
+
severityNumber: severity,
|
|
842
|
+
severityText,
|
|
843
|
+
body,
|
|
844
|
+
attributes,
|
|
845
|
+
traceId: context?.traceId,
|
|
846
|
+
spanId: context?.spanId,
|
|
847
|
+
resource: this.resource
|
|
848
|
+
};
|
|
849
|
+
// Emit to observable stream
|
|
850
|
+
this.logSubject.next(logRecord);
|
|
851
|
+
// Console output if enabled
|
|
852
|
+
if (this.consoleOutput) {
|
|
853
|
+
this.outputToConsole(logRecord);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Output log to console
|
|
858
|
+
* @param logRecord - The log record to output
|
|
859
|
+
*/
|
|
860
|
+
outputToConsole(logRecord) {
|
|
861
|
+
const prefix = this.name ? `[${this.name}]` : '';
|
|
862
|
+
const message = `${prefix} ${logRecord.body}`;
|
|
863
|
+
const meta = logRecord.attributes;
|
|
864
|
+
switch (logRecord.severityNumber) {
|
|
865
|
+
case LogSeverity.TRACE:
|
|
866
|
+
case LogSeverity.DEBUG:
|
|
867
|
+
console.debug(message, meta);
|
|
868
|
+
break;
|
|
869
|
+
case LogSeverity.INFO:
|
|
870
|
+
console.info(message, meta);
|
|
871
|
+
break;
|
|
872
|
+
case LogSeverity.WARN:
|
|
873
|
+
console.warn(message, meta);
|
|
874
|
+
break;
|
|
875
|
+
case LogSeverity.ERROR:
|
|
876
|
+
case LogSeverity.FATAL:
|
|
877
|
+
console.error(message, meta);
|
|
878
|
+
break;
|
|
879
|
+
default:
|
|
880
|
+
console.log(message, meta);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
/**
|
|
884
|
+
* Log a TRACE level message
|
|
885
|
+
* @param body - Log message
|
|
886
|
+
* @param attributes - Optional structured attributes
|
|
887
|
+
* @param context - Optional trace context
|
|
888
|
+
*/
|
|
889
|
+
trace(body, attributes, context) {
|
|
890
|
+
this.log(LogSeverity.TRACE, 'TRACE', body, attributes, context);
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* Log a DEBUG level message
|
|
894
|
+
* @param body - Log message
|
|
895
|
+
* @param attributes - Optional structured attributes
|
|
896
|
+
* @param context - Optional trace context
|
|
897
|
+
*/
|
|
898
|
+
debug(body, attributes, context) {
|
|
899
|
+
this.log(LogSeverity.DEBUG, 'DEBUG', body, attributes, context);
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* Log an INFO level message
|
|
903
|
+
* @param body - Log message
|
|
904
|
+
* @param attributes - Optional structured attributes
|
|
905
|
+
* @param context - Optional trace context
|
|
906
|
+
*/
|
|
907
|
+
info(body, attributes, context) {
|
|
908
|
+
this.log(LogSeverity.INFO, 'INFO', body, attributes, context);
|
|
909
|
+
}
|
|
910
|
+
/**
|
|
911
|
+
* Log a WARN level message
|
|
912
|
+
* @param body - Log message
|
|
913
|
+
* @param attributes - Optional structured attributes
|
|
914
|
+
* @param context - Optional trace context
|
|
915
|
+
*/
|
|
916
|
+
warn(body, attributes, context) {
|
|
917
|
+
this.log(LogSeverity.WARN, 'WARN', body, attributes, context);
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* Log an ERROR level message
|
|
921
|
+
* @param body - Log message
|
|
922
|
+
* @param attributes - Optional structured attributes
|
|
923
|
+
* @param context - Optional trace context
|
|
924
|
+
*/
|
|
925
|
+
error(body, attributes, context) {
|
|
926
|
+
this.log(LogSeverity.ERROR, 'ERROR', body, attributes, context);
|
|
927
|
+
}
|
|
928
|
+
/**
|
|
929
|
+
* Log a FATAL level message
|
|
930
|
+
* @param body - Log message
|
|
931
|
+
* @param attributes - Optional structured attributes
|
|
932
|
+
* @param context - Optional trace context
|
|
933
|
+
*/
|
|
934
|
+
fatal(body, attributes, context) {
|
|
935
|
+
this.log(LogSeverity.FATAL, 'FATAL', body, attributes, context);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
export { APP_CONFIG, AUTH0_CONFIG, AuthService, EventBus, LogSeverity, Logger, STORAGE_CONFIG, STORAGE_KEYS, configureAuth0, createAuthService, getStorageItem$1 as getStorageItem, removeStorageItem$1 as removeStorageItem, resetAuth0Config, setStorageItem$1 as setStorageItem };
|
|
669
940
|
//# sourceMappingURL=index.mjs.map
|