@opensourcekd/ng-common-libs 1.2.7 → 2.0.0

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 CHANGED
@@ -2,18 +2,33 @@
2
2
 
3
3
  Angular 18 library with Auth0 authentication service and MicroFrontend event bus.
4
4
 
5
- ## 🎯 Dual-Layer Architecture
5
+ ## 🎯 Simple Architecture
6
6
 
7
- This library features a **dual-layer architecture**:
7
+ **One import path. Everything you need.**
8
8
 
9
- - **Core Layer** (`@opensourcekd/ng-common-libs/core`): Framework-agnostic event bus using only RxJS. Use in React, Vue, Svelte, or vanilla JavaScript projects.
10
- - **Angular Layer** (`@opensourcekd/ng-common-libs` or `/angular`): Angular-specific services with dependency injection.
9
+ All services, utilities, and types are exported from a single package:
10
+
11
+ ```typescript
12
+ import {
13
+ AuthService,
14
+ EventBusService,
15
+ getAuthService,
16
+ getEventBusService,
17
+ configureAuth0,
18
+ UserInfo,
19
+ EventBus
20
+ } from '@opensourcekd/ng-common-libs';
21
+ ```
22
+
23
+ > 📖 **Complete Guide**: See [CONSUMPTION_GUIDE.md](./CONSUMPTION_GUIDE.md) for detailed usage instructions.
11
24
 
12
25
  ## 🚀 Features
13
26
 
14
- - **APP_CONFIG**: Framework-agnostic configuration utility for all environment variables (Auth0, API URLs)
15
- - **EventBusService**: MicroFrontend-ready event bus with replay buffer for cross-app communication
16
- - **Auth0 Integration**: Complete Auth0 authentication service with token management
27
+ - **AuthService**: Complete Auth0 authentication with token management
28
+ - **EventBusService**: MicroFrontend-ready event bus with replay buffer
29
+ - **Module Federation Support**: Singleton pattern works across shell + MFEs
30
+ - **AOT-Safe**: No JIT compiler required, works in production builds
31
+ - **Framework-Agnostic Core**: EventBus can be used in React, Vue, or vanilla JS
17
32
 
18
33
  ## 📦 Installation
19
34
 
@@ -21,225 +36,109 @@ This library features a **dual-layer architecture**:
21
36
  npm install @opensourcekd/ng-common-libs
22
37
  ```
23
38
 
24
- > **Note:** `mitt` and `@auth0/auth0-spa-js` are bundled with the library. You don't need to install them separately.
25
- >
26
- > **Module Federation**: Just configure `@opensourcekd/ng-common-libs` as shared in your webpack config. The bundled dependencies (`mitt`, `@auth0/auth0-spa-js`) will be included automatically, ensuring they're loaded only once across all micro-frontends.
39
+ > **Note:** `mitt` and `@auth0/auth0-spa-js` are bundled. No separate installation needed.
27
40
 
28
- ## 🔧 Usage
41
+ ## 🔧 Quick Start
29
42
 
30
- ### Configuration (Framework-Agnostic)
43
+ ### For Module Federation (Shell + MFEs)
31
44
 
32
- The library provides a framework-agnostic configuration utility with **statically configured values** from GitHub repository variables:
45
+ **In your shell app AND each MFE's `app.config.ts`:**
33
46
 
34
47
  ```typescript
35
- // Import from /core for framework-agnostic usage
36
- import { APP_CONFIG } from '@opensourcekd/ng-common-libs/core';
48
+ import { ApplicationConfig } from '@angular/core';
49
+ import {
50
+ AuthService,
51
+ EventBusService,
52
+ getAuthService,
53
+ getEventBusService,
54
+ configureAuth0
55
+ } from '@opensourcekd/ng-common-libs';
56
+
57
+ // Configure Auth0 once in shell (use your environment config)
58
+ configureAuth0({
59
+ domain: 'your-tenant.auth0.com',
60
+ clientId: 'your-client-id',
61
+ audience: 'https://your-api.example.com',
62
+ });
37
63
 
38
- // Access pre-configured values anywhere in your app
39
- console.log(APP_CONFIG.apiUrl); // Configured during build from GitHub vars
40
- console.log(APP_CONFIG.auth0Domain); // Configured during build from GitHub vars
41
- console.log(APP_CONFIG.auth0ClientId); // Configured during build from GitHub vars
64
+ export const appConfig: ApplicationConfig = {
65
+ providers: [
66
+ // Factory functions ensure singleton across all MFEs
67
+ { provide: EventBusService, useFactory: getEventBusService },
68
+ { provide: AuthService, useFactory: getAuthService },
69
+ ]
70
+ };
42
71
  ```
43
72
 
44
- **Key Features:**
45
- - Values are **statically baked** into the library during build time from GitHub repository variables
46
- - No runtime configuration needed - values are available immediately
47
- - Can be used across any framework: Angular, React, Vue, Svelte, vanilla JS
48
- - Perfect for MicroFrontends (MFEs) and shell applications
73
+ **Why factory functions?**
74
+ They create a **JavaScript module-level singleton** shared across ALL applications (shell + MFEs). Module Federation ensures the module loads once, so all apps get the same instance.
49
75
 
50
- ### Auth0 Authentication
51
-
52
- Complete Auth0 integration with automatic token management and event emission:
76
+ ### For Standalone Components
53
77
 
78
+ Then use normally with `inject()`:
54
79
  ```typescript
55
80
  import { Component, inject } from '@angular/core';
56
- import { AuthService, configureAuth0 } from '@opensourcekd/ng-common-libs';
57
- import { APP_CONFIG } from '@opensourcekd/ng-common-libs/core';
58
-
59
- // Use pre-configured values from APP_CONFIG
60
- configureAuth0({
61
- domain: APP_CONFIG.auth0Domain,
62
- clientId: APP_CONFIG.auth0ClientId,
63
- audience: APP_CONFIG.apiUrl,
64
- });
81
+ import { AuthService, EventBusService } from '@opensourcekd/ng-common-libs';
65
82
 
66
- @Component({
67
- selector: 'app-login',
68
- template: `
69
- <button (click)="login()">Login with Auth0</button>
70
- <button (click)="logout()" *ngIf="user">Logout</button>
71
- <div *ngIf="user">Welcome, {{ user.name }}!</div>
72
- `
73
- })
74
- export class LoginComponent {
83
+ export class MyComponent {
75
84
  private authService = inject(AuthService);
76
- user = this.authService.getUser();
77
-
78
- async login() {
79
- await this.authService.login();
80
- }
81
-
82
- async logout() {
83
- await this.authService.logout();
84
- }
85
- }
86
- ```
87
-
88
- See the [Auth0 Service Usage Guide](./docs/AUTH_SERVICE_USAGE.md) for complete integration instructions.
89
-
90
- ### EventBusService
91
-
92
- Cross-application event communication for MicroFrontend architectures:
93
-
94
- ```typescript
95
- import { Component, OnInit, inject } from '@angular/core';
96
- import { EventBusService } from '@opensourcekd/ng-common-libs';
97
-
98
- @Component({
99
- selector: 'app-event-example',
100
- template: `<button (click)="sendEvent()">Send Event</button>`
101
- })
102
- export class EventExampleComponent implements OnInit {
103
85
  private eventBus = inject(EventBusService);
104
-
105
- ngOnInit() {
106
- // Subscribe to all events
107
- this.eventBus.onePlusNEvents.subscribe(event => {
108
- console.log('Event received:', event);
109
- });
110
- }
111
-
112
- sendEvent() {
113
- // Send structured event
114
- this.eventBus.sendEvent(JSON.stringify({
115
- type: 'user:action',
116
- payload: { userId: '123' },
117
- timestamp: new Date().toISOString()
118
- }));
119
- }
86
+
87
+ login() { this.authService.login(); }
120
88
  }
121
89
  ```
122
90
 
123
- See the [EventBus Service Usage Guide](./docs/EVENT_BUS_USAGE.md) for advanced patterns and MicroFrontend examples.
124
-
125
- ### For Non-Angular Projects (React, Vue, Svelte, Vanilla JS)
91
+ ### Configuration
126
92
 
127
- Use the framework-agnostic core event bus:
93
+ **Use your own environment configuration** (recommended):
128
94
 
129
95
  ```typescript
130
- // Import from /core
131
- import { EventBus } from '@opensourcekd/ng-common-libs/core';
132
-
133
- // Event Bus
134
- const eventBus = new EventBus();
135
- eventBus.emit('user:login', { userId: '123' });
136
- eventBus.on('user:login').subscribe(data => {
137
- console.log('User logged in:', data);
96
+ import { configureAuth0 } from '@opensourcekd/ng-common-libs';
97
+ import { environment } from './environments/environment';
98
+
99
+ configureAuth0({
100
+ domain: environment.auth0Domain,
101
+ clientId: environment.auth0ClientId,
102
+ audience: environment.apiUrl,
138
103
  });
139
104
  ```
140
105
 
141
- #### React Example
142
-
143
- ```typescript
144
- import { useEffect } from 'react';
145
- import { EventBus } from '@opensourcekd/ng-common-libs/core';
106
+ > ⚠️ **Important**: Do not use `APP_CONFIG` from the library. It contains placeholder values. Always use your own environment configuration.
146
107
 
147
- const eventBus = new EventBus();
148
-
149
- function MyComponent() {
150
- useEffect(() => {
151
- const subscription = eventBus.on('user:login').subscribe(data => {
152
- console.log('User logged in:', data);
153
- });
154
- return () => subscription.unsubscribe();
155
- }, []);
108
+ ## 📚 Documentation
156
109
 
157
- const handleLogin = () => {
158
- eventBus.emit('user:login', { userId: '123' });
159
- };
110
+ - **[CONSUMPTION_GUIDE.md](./CONSUMPTION_GUIDE.md)** - Complete setup guide and API reference
111
+ - **[Auth0 Service Usage](./docs/AUTH_SERVICE_USAGE.md)** - Detailed Auth0 integration
112
+ - **[EventBus Service Usage](./docs/EVENT_BUS_USAGE.md)** - Cross-application event patterns
113
+ - **[Module Federation Guide](./docs/MODULE_FEDERATION.md)** - Runtime sharing configuration
160
114
 
161
- return <button onClick={handleLogin}>Login</button>;
162
- }
163
- ```
115
+ ## 📝 What's Exported
164
116
 
165
- #### Vue Example
117
+ Everything you need from one import:
166
118
 
167
119
  ```typescript
168
- import { onMounted, onUnmounted } from 'vue';
169
- import { EventBus } from '@opensourcekd/ng-common-libs/core';
170
-
171
- const eventBus = new EventBus();
172
-
173
- export default {
174
- setup() {
175
- let subscription;
176
-
177
- onMounted(() => {
178
- subscription = eventBus.on('user:login').subscribe(data => {
179
- console.log('User logged in:', data);
180
- });
181
- });
182
-
183
- onUnmounted(() => {
184
- subscription?.unsubscribe();
185
- });
186
-
187
- const handleLogin = () => {
188
- eventBus.emit('user:login', { userId: '123' });
189
- };
190
-
191
- return { handleLogin };
192
- }
193
- };
120
+ import {
121
+ // Services
122
+ AuthService,
123
+ EventBusService,
124
+
125
+ // Factory Functions
126
+ getAuthService,
127
+ getEventBusService,
128
+
129
+ // Configuration
130
+ configureAuth0,
131
+
132
+ // Core Utilities
133
+ EventBus,
134
+
135
+ // Types & Interfaces
136
+ UserInfo,
137
+ UserData,
138
+ EventPayload,
139
+ } from '@opensourcekd/ng-common-libs';
194
140
  ```
195
141
 
196
- ## 📚 Documentation
197
-
198
- - **[Auth0 Service Usage](./docs/AUTH_SERVICE_USAGE.md)** - Complete Auth0 authentication integration guide
199
- - **[EventBus Service Usage](./docs/EVENT_BUS_USAGE.md)** - Cross-application event communication guide
200
- - **[Deployment Guide](./docs/DEPLOYMENT.md)** - Publishing, versioning, and consumption
201
- - **[Microapp Triggering Guide](./docs/MICROAPP_TRIGGERING.md)** - Automatic build triggering for consuming microapps
202
- - **[Module Federation Guide](./docs/MODULE_FEDERATION.md)** - Runtime sharing with Module Federation
203
-
204
- ## 📝 API Documentation
205
-
206
- ### Core Configuration (Framework-Agnostic)
207
-
208
- - `APP_CONFIG` - Read-only application configuration object with values statically configured during build time:
209
- - `auth0Domain: string` - Auth0 tenant domain (from GitHub var `AUTH0_DOMAIN`)
210
- - `auth0ClientId: string` - Auth0 client ID (from GitHub var `AUTH0_CLIENT_ID`)
211
- - `apiUrl: string` - API base URL (from GitHub var `API_URL`)
212
-
213
- ### Angular Configuration
214
-
215
- - `configureAuth0(config: Partial<AUTH0_CONFIG>): void` - Configure Auth0 settings (domain, clientId, etc.)
216
- - `AUTH0_CONFIG` - Auth0 configuration object (domain, clientId, redirectUri, etc.)
217
-
218
- ### AuthService
219
-
220
- - `login(user?: string, options?: { invitation?: string; organization?: string }): Promise<void>` - Initiate Auth0 login with optional user identifier and organization invitation parameters
221
- - `logout(): Promise<void>` - Logout and clear session
222
- - `handleCallback(): Promise<{ success: boolean, appState?: any }>` - Handle Auth0 callback and return success status with preserved appState
223
- - `getToken(): Promise<string | null>` - Get access token asynchronously (checks storage first, then Auth0)
224
- - `getTokenSync(): string | null` - Get cached access token synchronously (storage only)
225
- - `getUser(): UserInfo | null` - Get current user information
226
- - `getUserData(): UserData | null` - Get simplified user data with role and organization extraction
227
- - `isAuthenticated(): Promise<boolean>` - Check authentication status asynchronously (verifies with Auth0)
228
- - `isAuthenticatedSync(): boolean` - Check authentication status synchronously (storage only)
229
- - `user$: Observable<UserInfo | null>` - Observable stream of user changes
230
-
231
- ### EventBusService
232
-
233
- - `sendEvent(event: string): void` - Send event to all subscribers
234
- - `onePlusNEvents: Observable<string>` - Subscribe to all events with replay buffer
235
-
236
- ### EventBus (Core)
237
-
238
- - `emit<T>(eventType: string, data?: T): void` - Emit an event
239
- - `on<T>(eventType: string): Observable<T>` - Subscribe to an event
240
- - `onMultiple(eventTypes: string[]): Observable<EventPayload>` - Subscribe to multiple events
241
- - `onAll(): Observable<EventPayload>` - Subscribe to all events
242
-
243
142
  ## 🛠️ Development
244
143
 
245
144
  ```bash