fauxbase 0.5.1 → 0.5.3

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
@@ -714,6 +714,32 @@ const fb = createClient({
714
714
  });
715
715
  ```
716
716
 
717
+ ### Authentication
718
+
719
+ When `auth` is configured, STOMP automatically injects `Authorization: Bearer <token>` into connection headers. Both SSE and STOMP reconnect on login/logout so the connection always uses the current token.
720
+
721
+ ```ts
722
+ const fb = createClient({
723
+ driver: { type: 'http', baseUrl: '/api' },
724
+ services: { todo: TodoService },
725
+ auth: UserAuth,
726
+ events: {
727
+ source: {
728
+ type: 'stomp',
729
+ brokerUrl: 'wss://api.example.com/ws',
730
+ subscriptions: { '/topic/todos': 'todo' },
731
+ // No need to set connectHeaders — token is injected automatically
732
+ },
733
+ },
734
+ });
735
+
736
+ // After login, STOMP reconnects with the new token
737
+ await fb.auth.login({ email: 'alice@test.com', password: 'secret' });
738
+
739
+ // After logout, STOMP reconnects without token
740
+ fb.auth.logout();
741
+ ```
742
+
717
743
  ### Custom handlers
718
744
 
719
745
  ```ts
package/dist/index.cjs CHANGED
@@ -268,6 +268,7 @@ var AuthService = class extends Service {
268
268
  authState = null;
269
269
  saveState = null;
270
270
  httpDriver = null;
271
+ authChangeListeners = [];
271
272
  /** @internal — called by createClient to wire persistence */
272
273
  _initAuth(loadState, saveState) {
273
274
  this.saveState = saveState;
@@ -423,10 +424,17 @@ var AuthService = class extends Service {
423
424
  exp: Date.now() + 24 * 60 * 60 * 1e3
424
425
  }));
425
426
  }
427
+ /** @internal — called by createClient to listen for auth state changes */
428
+ _onAuthChange(listener) {
429
+ this.authChangeListeners.push(listener);
430
+ }
426
431
  persistState() {
427
432
  if (this.saveState) {
428
433
  this.saveState(this.authState);
429
434
  }
435
+ for (const listener of this.authChangeListeners) {
436
+ listener();
437
+ }
430
438
  }
431
439
  };
432
440
 
@@ -1447,6 +1455,10 @@ var SSESource = class {
1447
1455
  });
1448
1456
  }
1449
1457
  }
1458
+ reconnect() {
1459
+ this.disconnect();
1460
+ this.connect();
1461
+ }
1450
1462
  disconnect() {
1451
1463
  if (this.eventSource) {
1452
1464
  this.eventSource.close();
@@ -1481,6 +1493,10 @@ var STOMPSource = class {
1481
1493
  connect() {
1482
1494
  this.connectAsync();
1483
1495
  }
1496
+ reconnect() {
1497
+ this.disconnect();
1498
+ this.connect();
1499
+ }
1484
1500
  async connectAsync() {
1485
1501
  let StompJs;
1486
1502
  try {
@@ -1491,9 +1507,16 @@ var STOMPSource = class {
1491
1507
  "STOMP source requires @stomp/stompjs. Install it: npm install @stomp/stompjs"
1492
1508
  );
1493
1509
  }
1510
+ const headers = { ...this.config.connectHeaders };
1511
+ if (this.config.getAuthToken) {
1512
+ const token = this.config.getAuthToken();
1513
+ if (token) {
1514
+ headers["Authorization"] = `Bearer ${token}`;
1515
+ }
1516
+ }
1494
1517
  this.client = new StompJs.Client({
1495
1518
  brokerURL: this.config.brokerUrl,
1496
- connectHeaders: this.config.connectHeaders,
1519
+ connectHeaders: headers,
1497
1520
  onConnect: () => {
1498
1521
  for (const [destination, resource] of Object.entries(this.config.subscriptions)) {
1499
1522
  this.client.subscribe(destination, (message) => {
@@ -1647,10 +1670,19 @@ function createClient(config) {
1647
1670
  if (eventsConfig.source.type === "sse") {
1648
1671
  eventSource = new SSESource(eventsConfig.source, eventBus);
1649
1672
  } else if (eventsConfig.source.type === "stomp") {
1650
- eventSource = new STOMPSource(eventsConfig.source, eventBus);
1673
+ const stompConfig = { ...eventsConfig.source };
1674
+ if (config.auth && !stompConfig.getAuthToken) {
1675
+ stompConfig.getAuthToken = () => client.auth?.token ?? null;
1676
+ }
1677
+ eventSource = new STOMPSource(stompConfig, eventBus);
1651
1678
  }
1652
1679
  eventSource?.connect();
1653
1680
  }
1681
+ if (eventSource && config.auth) {
1682
+ client.auth._onAuthChange(() => {
1683
+ eventSource.reconnect();
1684
+ });
1685
+ }
1654
1686
  client.disconnect = () => {
1655
1687
  eventSource?.disconnect();
1656
1688
  eventBus.destroy();