@opensourcekd/ng-common-libs 2.0.4 → 2.0.6

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/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
@@ -79,9 +102,21 @@ declare const APP_CONFIG: {
79
102
  * Centralized configuration for Auth0 integration
80
103
  * Framework-agnostic - works with any JavaScript framework
81
104
  */
105
+ /**
106
+ * Auth0 configuration interface
107
+ */
108
+ interface Auth0ConfigOptions {
109
+ domain?: string;
110
+ clientId?: string;
111
+ redirectUri?: string;
112
+ logoutUri?: string;
113
+ audience?: string;
114
+ scope?: string;
115
+ connection?: string;
116
+ }
82
117
  /**
83
118
  * Auth0 client configuration
84
- * Override these values in your consuming application by setting them before importing AuthService
119
+ * Override these values in your consuming application using configureAuth0()
85
120
  *
86
121
  * Note: redirectUri defaults to window.location.origin (base URL without path).
87
122
  * Auth0 will redirect back to this URL after authentication.
@@ -95,6 +130,25 @@ declare const AUTH0_CONFIG: {
95
130
  scope: string;
96
131
  connection: string | undefined;
97
132
  };
133
+ /**
134
+ * Configure Auth0 settings
135
+ * Call this function early in your application (e.g., in app.config.ts or main.ts)
136
+ * to override the default Auth0 configuration values.
137
+ *
138
+ * @param config - Partial Auth0 configuration to override defaults
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * import { configureAuth0, APP_CONFIG } from '@opensourcekd/ng-common-libs';
143
+ *
144
+ * configureAuth0({
145
+ * domain: APP_CONFIG.auth0Domain,
146
+ * clientId: APP_CONFIG.auth0ClientId,
147
+ * audience: APP_CONFIG.apiUrl,
148
+ * });
149
+ * ```
150
+ */
151
+ declare function configureAuth0(config: Auth0ConfigOptions): void;
98
152
  /**
99
153
  * Storage configuration
100
154
  * Controls where sensitive data is stored
@@ -110,6 +164,12 @@ declare const STORAGE_KEYS: {
110
164
  ACCESS_TOKEN: string;
111
165
  USER_INFO: string;
112
166
  };
167
+ /**
168
+ * Reset AUTH0_CONFIG to default values
169
+ * Useful for testing
170
+ * @internal
171
+ */
172
+ declare function resetAuth0Config(): void;
113
173
  /**
114
174
  * Helper functions for storage operations
115
175
  * These work with both localStorage and sessionStorage
@@ -206,6 +266,12 @@ interface Auth0Config {
206
266
  scope: string;
207
267
  connection?: string;
208
268
  }
269
+ /**
270
+ * AuthService options interface
271
+ */
272
+ interface AuthServiceOptions {
273
+ id?: string;
274
+ }
209
275
  /**
210
276
  * Storage configuration
211
277
  */
@@ -243,10 +309,16 @@ interface StorageKeys {
243
309
  * };
244
310
  * const authService = new AuthService(authConfig, eventBus);
245
311
  *
312
+ * // Or create with an identifier
313
+ * const authService = new AuthService(authConfig, eventBus, undefined, undefined, { id: 'MFE' });
314
+ *
246
315
  * // Use the service
247
316
  * await authService.login();
248
317
  * const user = authService.getUser();
249
318
  * const token = await authService.getToken();
319
+ *
320
+ * // Get the identifier
321
+ * const id = authService.getId(); // 'MFE' or undefined
250
322
  * ```
251
323
  */
252
324
  declare class AuthService {
@@ -259,14 +331,21 @@ declare class AuthService {
259
331
  private storageConfig;
260
332
  private storageKeys;
261
333
  private eventBus;
334
+ private id?;
262
335
  /**
263
336
  * Create a new AuthService instance
264
337
  * @param config - Auth0 configuration
265
338
  * @param eventBus - EventBus instance for emitting auth events
266
339
  * @param storageConfig - Storage configuration (optional, defaults to sessionStorage)
267
340
  * @param storageKeys - Storage keys (optional, defaults to standard keys)
341
+ * @param options - Optional configuration with an id to identify the instance
342
+ */
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
268
347
  */
269
- constructor(config: Auth0Config, eventBus: EventBus, storageConfig?: StorageConfig, storageKeys?: StorageKeys);
348
+ getId(): string | undefined;
270
349
  /**
271
350
  * Initialize Auth0 client
272
351
  */
@@ -341,6 +420,32 @@ declare class AuthService {
341
420
  */
342
421
  private emitAuthEvent;
343
422
  }
423
+ /**
424
+ * Create AuthService instance using AUTH0_CONFIG
425
+ * Helper function for creating AuthService with default configuration from AUTH0_CONFIG
426
+ *
427
+ * Note: Make sure to call configureAuth0() before using this helper
428
+ *
429
+ * @param eventBus - EventBus instance for auth events
430
+ * @returns Configured AuthService instance
431
+ *
432
+ * @example
433
+ * ```typescript
434
+ * import { createAuthService, EventBus, configureAuth0, APP_CONFIG } from '@opensourcekd/ng-common-libs';
435
+ *
436
+ * // Configure Auth0 first
437
+ * configureAuth0({
438
+ * domain: APP_CONFIG.auth0Domain,
439
+ * clientId: APP_CONFIG.auth0ClientId,
440
+ * audience: APP_CONFIG.apiUrl,
441
+ * });
442
+ *
443
+ * // Create instances
444
+ * const eventBus = new EventBus();
445
+ * const authService = createAuthService(eventBus);
446
+ * ```
447
+ */
448
+ declare function createAuthService(eventBus: EventBus): AuthService;
344
449
 
345
- export { APP_CONFIG, AUTH0_CONFIG, AuthService, EventBus, STORAGE_CONFIG, STORAGE_KEYS, getStorageItem, removeStorageItem, setStorageItem };
346
- export type { AppState, Auth0Config, AuthorizationParams, CallbackResult, EventPayload, StorageConfig, StorageKeys, UserData, UserInfo };
450
+ export { APP_CONFIG, AUTH0_CONFIG, AuthService, EventBus, STORAGE_CONFIG, STORAGE_KEYS, configureAuth0, createAuthService, getStorageItem, removeStorageItem, resetAuth0Config, setStorageItem };
451
+ export type { AppState, Auth0Config, Auth0ConfigOptions, AuthServiceOptions, AuthorizationParams, CallbackResult, EventBusOptions, EventPayload, 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
@@ -86,20 +107,47 @@ const APP_CONFIG = {
86
107
  */
87
108
  /**
88
109
  * Auth0 client configuration
89
- * Override these values in your consuming application by setting them before importing AuthService
110
+ * Override these values in your consuming application using configureAuth0()
90
111
  *
91
112
  * Note: redirectUri defaults to window.location.origin (base URL without path).
92
113
  * Auth0 will redirect back to this URL after authentication.
93
114
  */
94
115
  const AUTH0_CONFIG = {
95
- domain: '', // Set in consuming app: process.env['NX_AUTH0_DOMAIN'] || 'your-domain.auth0.com'
96
- clientId: '', // Set in consuming app: process.env['NX_AUTH0_CLIENT_ID'] || 'your-client-id'
116
+ domain: '', // Set via configureAuth0() or process.env
117
+ clientId: '', // Set via configureAuth0() or process.env
97
118
  redirectUri: typeof window !== 'undefined' ? window.location.origin : '',
98
119
  logoutUri: typeof window !== 'undefined' ? window.location.origin : '',
99
- audience: '', // Optional: Set in consuming app if using API authorization
120
+ audience: '', // Optional: Set via configureAuth0()
100
121
  scope: 'openid profile email', // Default scopes
101
- connection: undefined, // Optional: Force specific connection (e.g., 'Username-Password-Authentication')
122
+ connection: undefined, // Optional: Force specific connection
102
123
  };
124
+ /**
125
+ * Configure Auth0 settings
126
+ * Call this function early in your application (e.g., in app.config.ts or main.ts)
127
+ * to override the default Auth0 configuration values.
128
+ *
129
+ * @param config - Partial Auth0 configuration to override defaults
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * import { configureAuth0, APP_CONFIG } from '@opensourcekd/ng-common-libs';
134
+ *
135
+ * configureAuth0({
136
+ * domain: APP_CONFIG.auth0Domain,
137
+ * clientId: APP_CONFIG.auth0ClientId,
138
+ * audience: APP_CONFIG.apiUrl,
139
+ * });
140
+ * ```
141
+ */
142
+ function configureAuth0(config) {
143
+ // Only update properties that are explicitly provided (not undefined)
144
+ Object.keys(config).forEach((key) => {
145
+ const configKey = key;
146
+ if (config[configKey] !== undefined) {
147
+ AUTH0_CONFIG[configKey] = config[configKey];
148
+ }
149
+ });
150
+ }
103
151
  /**
104
152
  * Storage configuration
105
153
  * Controls where sensitive data is stored
@@ -115,6 +163,20 @@ const STORAGE_KEYS = {
115
163
  ACCESS_TOKEN: 'auth0_access_token',
116
164
  USER_INFO: 'auth0_user_info',
117
165
  };
166
+ /**
167
+ * Reset AUTH0_CONFIG to default values
168
+ * Useful for testing
169
+ * @internal
170
+ */
171
+ function resetAuth0Config() {
172
+ AUTH0_CONFIG.domain = '';
173
+ AUTH0_CONFIG.clientId = '';
174
+ AUTH0_CONFIG.redirectUri = typeof window !== 'undefined' ? window.location.origin : '';
175
+ AUTH0_CONFIG.logoutUri = typeof window !== 'undefined' ? window.location.origin : '';
176
+ AUTH0_CONFIG.audience = '';
177
+ AUTH0_CONFIG.scope = 'openid profile email';
178
+ AUTH0_CONFIG.connection = undefined;
179
+ }
118
180
  /**
119
181
  * Helper functions for storage operations
120
182
  * These work with both localStorage and sessionStorage
@@ -202,10 +264,16 @@ function removeStorageItem(key, storageType = 'sessionStorage') {
202
264
  * };
203
265
  * const authService = new AuthService(authConfig, eventBus);
204
266
  *
267
+ * // Or create with an identifier
268
+ * const authService = new AuthService(authConfig, eventBus, undefined, undefined, { id: 'MFE' });
269
+ *
205
270
  * // Use the service
206
271
  * await authService.login();
207
272
  * const user = authService.getUser();
208
273
  * const token = await authService.getToken();
274
+ *
275
+ * // Get the identifier
276
+ * const id = authService.getId(); // 'MFE' or undefined
209
277
  * ```
210
278
  */
211
279
  class AuthService {
@@ -224,12 +292,14 @@ class AuthService {
224
292
  storageConfig;
225
293
  storageKeys;
226
294
  eventBus;
295
+ id;
227
296
  /**
228
297
  * Create a new AuthService instance
229
298
  * @param config - Auth0 configuration
230
299
  * @param eventBus - EventBus instance for emitting auth events
231
300
  * @param storageConfig - Storage configuration (optional, defaults to sessionStorage)
232
301
  * @param storageKeys - Storage keys (optional, defaults to standard keys)
302
+ * @param options - Optional configuration with an id to identify the instance
233
303
  */
234
304
  constructor(config, eventBus, storageConfig = {
235
305
  TOKEN_STORAGE: 'sessionStorage',
@@ -237,16 +307,24 @@ class AuthService {
237
307
  }, storageKeys = {
238
308
  ACCESS_TOKEN: 'auth0_access_token',
239
309
  USER_INFO: 'auth0_user_info'
240
- }) {
310
+ }, options) {
241
311
  this.config = config;
242
312
  this.eventBus = eventBus;
243
313
  this.storageConfig = storageConfig;
244
314
  this.storageKeys = storageKeys;
315
+ this.id = options?.id;
245
316
  this.userSubject = new BehaviorSubject(this.getUserInfoFromStorage());
246
317
  this.user$ = this.userSubject.asObservable();
247
318
  console.log("[AuthService] AuthService instance created (Auth0 client will be initialized on first use)");
248
319
  // Lazy initialization - Auth0 client will be initialized in ensureInitialized() on first use
249
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
+ }
250
328
  /**
251
329
  * Initialize Auth0 client
252
330
  */
@@ -586,6 +664,43 @@ class AuthService {
586
664
  console.log('[AuthService] Auth event emitted:', event.type);
587
665
  }
588
666
  }
667
+ /**
668
+ * Create AuthService instance using AUTH0_CONFIG
669
+ * Helper function for creating AuthService with default configuration from AUTH0_CONFIG
670
+ *
671
+ * Note: Make sure to call configureAuth0() before using this helper
672
+ *
673
+ * @param eventBus - EventBus instance for auth events
674
+ * @returns Configured AuthService instance
675
+ *
676
+ * @example
677
+ * ```typescript
678
+ * import { createAuthService, EventBus, configureAuth0, APP_CONFIG } from '@opensourcekd/ng-common-libs';
679
+ *
680
+ * // Configure Auth0 first
681
+ * configureAuth0({
682
+ * domain: APP_CONFIG.auth0Domain,
683
+ * clientId: APP_CONFIG.auth0ClientId,
684
+ * audience: APP_CONFIG.apiUrl,
685
+ * });
686
+ *
687
+ * // Create instances
688
+ * const eventBus = new EventBus();
689
+ * const authService = createAuthService(eventBus);
690
+ * ```
691
+ */
692
+ function createAuthService(eventBus) {
693
+ const auth0Config = {
694
+ domain: AUTH0_CONFIG.domain,
695
+ clientId: AUTH0_CONFIG.clientId,
696
+ redirectUri: AUTH0_CONFIG.redirectUri,
697
+ logoutUri: AUTH0_CONFIG.logoutUri,
698
+ audience: AUTH0_CONFIG.audience,
699
+ scope: AUTH0_CONFIG.scope,
700
+ connection: AUTH0_CONFIG.connection,
701
+ };
702
+ return new AuthService(auth0Config, eventBus, STORAGE_CONFIG, STORAGE_KEYS);
703
+ }
589
704
 
590
- export { APP_CONFIG, AUTH0_CONFIG, AuthService, EventBus, STORAGE_CONFIG, STORAGE_KEYS, getStorageItem$1 as getStorageItem, removeStorageItem$1 as removeStorageItem, setStorageItem$1 as setStorageItem };
705
+ export { APP_CONFIG, AUTH0_CONFIG, AuthService, EventBus, STORAGE_CONFIG, STORAGE_KEYS, configureAuth0, createAuthService, getStorageItem$1 as getStorageItem, removeStorageItem$1 as removeStorageItem, resetAuth0Config, setStorageItem$1 as setStorageItem };
591
706
  //# sourceMappingURL=index.mjs.map