@gofreego/tsutils 0.1.23 → 0.1.25

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.mts CHANGED
@@ -41,6 +41,7 @@ declare class HttpClient {
41
41
  constructor(config?: HttpClientConfig);
42
42
  private buildURL;
43
43
  private request;
44
+ setOnUnauthorized(handler: (error: HttpError) => void): void;
44
45
  setDefaultHeader(key: string, value: string): void;
45
46
  removeDefaultHeader(key: string): void;
46
47
  getDefaultHeaders(): Record<string, string>;
@@ -174,13 +175,10 @@ interface ISessionManager {
174
175
  getSessionId(): string | undefined;
175
176
  /** True when a non-expired access token is stored. */
176
177
  isAuthenticated(): boolean;
177
- /** Called on app startup: restore the auth header or wipe an expired session. */
178
- initialize(): void;
179
178
  }
180
179
  declare class SessionManager implements ISessionManager {
181
180
  private static instance;
182
181
  private readonly key;
183
- private readonly client;
184
182
  private cache;
185
183
  private constructor();
186
184
  static getInstance(client: HttpClient): SessionManager;
@@ -192,9 +190,6 @@ declare class SessionManager implements ISessionManager {
192
190
  getRefreshToken(): string | undefined;
193
191
  getSessionId(): string | undefined;
194
192
  isAuthenticated(): boolean;
195
- initialize(): void;
196
- private setAuthToken;
197
- private clearAuthToken;
198
193
  }
199
194
 
200
195
  interface IAuthService {
package/dist/index.d.ts CHANGED
@@ -41,6 +41,7 @@ declare class HttpClient {
41
41
  constructor(config?: HttpClientConfig);
42
42
  private buildURL;
43
43
  private request;
44
+ setOnUnauthorized(handler: (error: HttpError) => void): void;
44
45
  setDefaultHeader(key: string, value: string): void;
45
46
  removeDefaultHeader(key: string): void;
46
47
  getDefaultHeaders(): Record<string, string>;
@@ -174,13 +175,10 @@ interface ISessionManager {
174
175
  getSessionId(): string | undefined;
175
176
  /** True when a non-expired access token is stored. */
176
177
  isAuthenticated(): boolean;
177
- /** Called on app startup: restore the auth header or wipe an expired session. */
178
- initialize(): void;
179
178
  }
180
179
  declare class SessionManager implements ISessionManager {
181
180
  private static instance;
182
181
  private readonly key;
183
- private readonly client;
184
182
  private cache;
185
183
  private constructor();
186
184
  static getInstance(client: HttpClient): SessionManager;
@@ -192,9 +190,6 @@ declare class SessionManager implements ISessionManager {
192
190
  getRefreshToken(): string | undefined;
193
191
  getSessionId(): string | undefined;
194
192
  isAuthenticated(): boolean;
195
- initialize(): void;
196
- private setAuthToken;
197
- private clearAuthToken;
198
193
  }
199
194
 
200
195
  interface IAuthService {
package/dist/index.js CHANGED
@@ -659,6 +659,9 @@ var HttpClient = class {
659
659
  throw error;
660
660
  }
661
661
  }
662
+ setOnUnauthorized(handler) {
663
+ this.onUnauthorized = handler;
664
+ }
662
665
  setDefaultHeader(key, value) {
663
666
  this.defaultHeaders[key] = value;
664
667
  }
@@ -1426,8 +1429,8 @@ function formatDate(date, options = {
1426
1429
  var SESSION_KEY = "session_details";
1427
1430
  var SessionManager = class _SessionManager {
1428
1431
  constructor(client, key = SESSION_KEY) {
1432
+ // private readonly client: HttpClient
1429
1433
  this.cache = null;
1430
- this.client = client;
1431
1434
  this.key = key;
1432
1435
  }
1433
1436
  static getInstance(client) {
@@ -1439,7 +1442,6 @@ var SessionManager = class _SessionManager {
1439
1442
  save(session) {
1440
1443
  this.cache = session;
1441
1444
  LocalStorage.setItem(this.key, session);
1442
- this.setAuthToken(session.accessToken);
1443
1445
  }
1444
1446
  patch(updates) {
1445
1447
  const current = this.get();
@@ -1447,14 +1449,10 @@ var SessionManager = class _SessionManager {
1447
1449
  const updated = { ...current, ...updates };
1448
1450
  this.cache = updated;
1449
1451
  LocalStorage.setItem(this.key, updated);
1450
- if (updates.accessToken) {
1451
- this.setAuthToken(updates.accessToken);
1452
- }
1453
1452
  }
1454
1453
  clear() {
1455
1454
  this.cache = null;
1456
1455
  LocalStorage.removeItem(this.key);
1457
- this.clearAuthToken();
1458
1456
  }
1459
1457
  get() {
1460
1458
  if (this.cache !== null) return this.cache;
@@ -1475,20 +1473,6 @@ var SessionManager = class _SessionManager {
1475
1473
  if (!session?.accessToken) return false;
1476
1474
  return Number(session.expiresAt) > Date.now() / 1e3;
1477
1475
  }
1478
- initialize() {
1479
- const session = this.get();
1480
- if (session?.accessToken && Number(session.expiresAt) > Date.now() / 1e3) {
1481
- this.setAuthToken(session.accessToken);
1482
- } else {
1483
- this.clear();
1484
- }
1485
- }
1486
- setAuthToken(token) {
1487
- this.client.setDefaultHeader("Authorization", `Bearer ${token}`);
1488
- }
1489
- clearAuthToken() {
1490
- this.client.removeDefaultHeader("Authorization");
1491
- }
1492
1476
  };
1493
1477
 
1494
1478
  // src/auth/authService.ts
@@ -1511,6 +1495,7 @@ var AuthService = class _AuthService {
1511
1495
  );
1512
1496
  if (response.data.accessToken) {
1513
1497
  this.sessionManager.save(response.data);
1498
+ this.client.setDefaultHeader("Authorization", `Bearer ${response.data.accessToken}`);
1514
1499
  }
1515
1500
  return response.data;
1516
1501
  }
@@ -1528,6 +1513,7 @@ var AuthService = class _AuthService {
1528
1513
  accessToken: response.data.accessToken,
1529
1514
  refreshToken: response.data.refreshToken
1530
1515
  });
1516
+ this.client.setDefaultHeader("Authorization", `Bearer ${response.data.accessToken}`);
1531
1517
  }
1532
1518
  return response.data;
1533
1519
  }
@@ -1541,6 +1527,7 @@ var AuthService = class _AuthService {
1541
1527
  return response.data;
1542
1528
  } finally {
1543
1529
  this.sessionManager.clear();
1530
+ this.client.removeDefaultHeader("Authorization");
1544
1531
  }
1545
1532
  }
1546
1533
  async generateLoginToken(request) {
@@ -1557,6 +1544,7 @@ var AuthService = class _AuthService {
1557
1544
  );
1558
1545
  if (response.data.accessToken) {
1559
1546
  this.sessionManager.save(response.data);
1547
+ this.client.setDefaultHeader("Authorization", `Bearer ${response.data.accessToken}`);
1560
1548
  }
1561
1549
  return response.data;
1562
1550
  }
@@ -1573,7 +1561,12 @@ var AuthService = class _AuthService {
1573
1561
  return this.sessionManager.getSessionId();
1574
1562
  }
1575
1563
  initializeAuth() {
1576
- this.sessionManager.initialize();
1564
+ const session = this.sessionManager.get();
1565
+ if (session?.accessToken && Number(session.expiresAt) > Date.now() / 1e3) {
1566
+ this.client.setDefaultHeader("Authorization", `Bearer ${session.accessToken}`);
1567
+ } else {
1568
+ this.client.removeDefaultHeader("Authorization");
1569
+ }
1577
1570
  }
1578
1571
  };
1579
1572