@gofreego/tsutils 0.1.24 → 0.1.26

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
@@ -6,53 +6,6 @@ import { Highlighter } from 'shiki';
6
6
 
7
7
  declare function NotFoundPage(): react_jsx_runtime.JSX.Element;
8
8
 
9
- interface HttpClientConfig {
10
- baseURL?: string;
11
- timeout?: number;
12
- headers?: Record<string, string>;
13
- onUnauthorized?: (error: HttpError) => void;
14
- }
15
- interface RequestConfig extends Omit<RequestInit, 'body'> {
16
- params?: Record<string, string | number | boolean>;
17
- data?: any;
18
- timeout?: number;
19
- }
20
- interface HttpResponse<T = any> {
21
- data: T;
22
- status: number;
23
- statusText: string;
24
- headers: Headers;
25
- }
26
- interface ErrorData {
27
- code: number;
28
- message: string;
29
- }
30
- interface HttpError extends Error {
31
- status?: number;
32
- statusText?: string;
33
- data?: ErrorData;
34
- }
35
-
36
- declare class HttpClient {
37
- private baseURL;
38
- private timeout;
39
- private defaultHeaders;
40
- private onUnauthorized?;
41
- constructor(config?: HttpClientConfig);
42
- private buildURL;
43
- private request;
44
- setOnUnauthorized(handler: (error: HttpError) => void): void;
45
- setDefaultHeader(key: string, value: string): void;
46
- removeDefaultHeader(key: string): void;
47
- getDefaultHeaders(): Record<string, string>;
48
- get<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
49
- post<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
50
- put<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
51
- patch<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
52
- delete<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
53
- }
54
- declare function extractErrorMessage(error: any): string;
55
-
56
9
  interface User {
57
10
  id: string;
58
11
  uuid: string;
@@ -175,16 +128,13 @@ interface ISessionManager {
175
128
  getSessionId(): string | undefined;
176
129
  /** True when a non-expired access token is stored. */
177
130
  isAuthenticated(): boolean;
178
- /** Called on app startup: restore the auth header or wipe an expired session. */
179
- initialize(): void;
180
131
  }
181
132
  declare class SessionManager implements ISessionManager {
182
133
  private static instance;
183
134
  private readonly key;
184
- private readonly client;
185
135
  private cache;
186
136
  private constructor();
187
- static getInstance(client: HttpClient): SessionManager;
137
+ static getInstance(): SessionManager;
188
138
  save(session: SignInResponse): void;
189
139
  patch(updates: Partial<SignInResponse>): void;
190
140
  clear(): void;
@@ -193,11 +143,55 @@ declare class SessionManager implements ISessionManager {
193
143
  getRefreshToken(): string | undefined;
194
144
  getSessionId(): string | undefined;
195
145
  isAuthenticated(): boolean;
196
- initialize(): void;
197
- private setAuthToken;
198
- private clearAuthToken;
199
146
  }
200
147
 
148
+ interface HttpClientConfig {
149
+ baseURL?: string;
150
+ timeout?: number;
151
+ headers?: Record<string, string>;
152
+ onUnauthorized?: (error: HttpError) => void;
153
+ }
154
+ interface RequestConfig extends Omit<RequestInit, 'body'> {
155
+ params?: Record<string, string | number | boolean>;
156
+ data?: any;
157
+ timeout?: number;
158
+ }
159
+ interface HttpResponse<T = any> {
160
+ data: T;
161
+ status: number;
162
+ statusText: string;
163
+ headers: Headers;
164
+ }
165
+ interface ErrorData {
166
+ code: number;
167
+ message: string;
168
+ }
169
+ interface HttpError extends Error {
170
+ status?: number;
171
+ statusText?: string;
172
+ data?: ErrorData;
173
+ }
174
+
175
+ declare class HttpClient {
176
+ private baseURL;
177
+ private timeout;
178
+ private defaultHeaders;
179
+ private onUnauthorized?;
180
+ constructor(config?: HttpClientConfig);
181
+ private buildURL;
182
+ private request;
183
+ setOnUnauthorized(handler: (error: HttpError) => void): void;
184
+ setDefaultHeader(key: string, value: string): void;
185
+ removeDefaultHeader(key: string): void;
186
+ getDefaultHeaders(): Record<string, string>;
187
+ get<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
188
+ post<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
189
+ put<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
190
+ patch<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
191
+ delete<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
192
+ }
193
+ declare function extractErrorMessage(error: any): string;
194
+
201
195
  interface IAuthService {
202
196
  signIn(request: SignInRequest): Promise<SignInResponse>;
203
197
  refreshToken(request?: Partial<RefreshTokenRequest>): Promise<RefreshTokenResponse>;
package/dist/index.d.ts CHANGED
@@ -6,53 +6,6 @@ import { Highlighter } from 'shiki';
6
6
 
7
7
  declare function NotFoundPage(): react_jsx_runtime.JSX.Element;
8
8
 
9
- interface HttpClientConfig {
10
- baseURL?: string;
11
- timeout?: number;
12
- headers?: Record<string, string>;
13
- onUnauthorized?: (error: HttpError) => void;
14
- }
15
- interface RequestConfig extends Omit<RequestInit, 'body'> {
16
- params?: Record<string, string | number | boolean>;
17
- data?: any;
18
- timeout?: number;
19
- }
20
- interface HttpResponse<T = any> {
21
- data: T;
22
- status: number;
23
- statusText: string;
24
- headers: Headers;
25
- }
26
- interface ErrorData {
27
- code: number;
28
- message: string;
29
- }
30
- interface HttpError extends Error {
31
- status?: number;
32
- statusText?: string;
33
- data?: ErrorData;
34
- }
35
-
36
- declare class HttpClient {
37
- private baseURL;
38
- private timeout;
39
- private defaultHeaders;
40
- private onUnauthorized?;
41
- constructor(config?: HttpClientConfig);
42
- private buildURL;
43
- private request;
44
- setOnUnauthorized(handler: (error: HttpError) => void): void;
45
- setDefaultHeader(key: string, value: string): void;
46
- removeDefaultHeader(key: string): void;
47
- getDefaultHeaders(): Record<string, string>;
48
- get<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
49
- post<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
50
- put<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
51
- patch<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
52
- delete<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
53
- }
54
- declare function extractErrorMessage(error: any): string;
55
-
56
9
  interface User {
57
10
  id: string;
58
11
  uuid: string;
@@ -175,16 +128,13 @@ interface ISessionManager {
175
128
  getSessionId(): string | undefined;
176
129
  /** True when a non-expired access token is stored. */
177
130
  isAuthenticated(): boolean;
178
- /** Called on app startup: restore the auth header or wipe an expired session. */
179
- initialize(): void;
180
131
  }
181
132
  declare class SessionManager implements ISessionManager {
182
133
  private static instance;
183
134
  private readonly key;
184
- private readonly client;
185
135
  private cache;
186
136
  private constructor();
187
- static getInstance(client: HttpClient): SessionManager;
137
+ static getInstance(): SessionManager;
188
138
  save(session: SignInResponse): void;
189
139
  patch(updates: Partial<SignInResponse>): void;
190
140
  clear(): void;
@@ -193,11 +143,55 @@ declare class SessionManager implements ISessionManager {
193
143
  getRefreshToken(): string | undefined;
194
144
  getSessionId(): string | undefined;
195
145
  isAuthenticated(): boolean;
196
- initialize(): void;
197
- private setAuthToken;
198
- private clearAuthToken;
199
146
  }
200
147
 
148
+ interface HttpClientConfig {
149
+ baseURL?: string;
150
+ timeout?: number;
151
+ headers?: Record<string, string>;
152
+ onUnauthorized?: (error: HttpError) => void;
153
+ }
154
+ interface RequestConfig extends Omit<RequestInit, 'body'> {
155
+ params?: Record<string, string | number | boolean>;
156
+ data?: any;
157
+ timeout?: number;
158
+ }
159
+ interface HttpResponse<T = any> {
160
+ data: T;
161
+ status: number;
162
+ statusText: string;
163
+ headers: Headers;
164
+ }
165
+ interface ErrorData {
166
+ code: number;
167
+ message: string;
168
+ }
169
+ interface HttpError extends Error {
170
+ status?: number;
171
+ statusText?: string;
172
+ data?: ErrorData;
173
+ }
174
+
175
+ declare class HttpClient {
176
+ private baseURL;
177
+ private timeout;
178
+ private defaultHeaders;
179
+ private onUnauthorized?;
180
+ constructor(config?: HttpClientConfig);
181
+ private buildURL;
182
+ private request;
183
+ setOnUnauthorized(handler: (error: HttpError) => void): void;
184
+ setDefaultHeader(key: string, value: string): void;
185
+ removeDefaultHeader(key: string): void;
186
+ getDefaultHeaders(): Record<string, string>;
187
+ get<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
188
+ post<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
189
+ put<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
190
+ patch<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
191
+ delete<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
192
+ }
193
+ declare function extractErrorMessage(error: any): string;
194
+
201
195
  interface IAuthService {
202
196
  signIn(request: SignInRequest): Promise<SignInResponse>;
203
197
  refreshToken(request?: Partial<RefreshTokenRequest>): Promise<RefreshTokenResponse>;
package/dist/index.js CHANGED
@@ -1428,21 +1428,20 @@ function formatDate(date, options = {
1428
1428
  // src/auth/sessionManager.ts
1429
1429
  var SESSION_KEY = "session_details";
1430
1430
  var SessionManager = class _SessionManager {
1431
- constructor(client, key = SESSION_KEY) {
1431
+ constructor(key = SESSION_KEY) {
1432
+ // private readonly client: HttpClient
1432
1433
  this.cache = null;
1433
- this.client = client;
1434
1434
  this.key = key;
1435
1435
  }
1436
- static getInstance(client) {
1436
+ static getInstance() {
1437
1437
  if (!_SessionManager.instance) {
1438
- _SessionManager.instance = new _SessionManager(client);
1438
+ _SessionManager.instance = new _SessionManager();
1439
1439
  }
1440
1440
  return _SessionManager.instance;
1441
1441
  }
1442
1442
  save(session) {
1443
1443
  this.cache = session;
1444
1444
  LocalStorage.setItem(this.key, session);
1445
- this.setAuthToken(session.accessToken);
1446
1445
  }
1447
1446
  patch(updates) {
1448
1447
  const current = this.get();
@@ -1450,14 +1449,10 @@ var SessionManager = class _SessionManager {
1450
1449
  const updated = { ...current, ...updates };
1451
1450
  this.cache = updated;
1452
1451
  LocalStorage.setItem(this.key, updated);
1453
- if (updates.accessToken) {
1454
- this.setAuthToken(updates.accessToken);
1455
- }
1456
1452
  }
1457
1453
  clear() {
1458
1454
  this.cache = null;
1459
1455
  LocalStorage.removeItem(this.key);
1460
- this.clearAuthToken();
1461
1456
  }
1462
1457
  get() {
1463
1458
  if (this.cache !== null) return this.cache;
@@ -1478,20 +1473,6 @@ var SessionManager = class _SessionManager {
1478
1473
  if (!session?.accessToken) return false;
1479
1474
  return Number(session.expiresAt) > Date.now() / 1e3;
1480
1475
  }
1481
- initialize() {
1482
- const session = this.get();
1483
- if (session?.accessToken && Number(session.expiresAt) > Date.now() / 1e3) {
1484
- this.setAuthToken(session.accessToken);
1485
- } else {
1486
- this.clear();
1487
- }
1488
- }
1489
- setAuthToken(token) {
1490
- this.client.setDefaultHeader("Authorization", `Bearer ${token}`);
1491
- }
1492
- clearAuthToken() {
1493
- this.client.removeDefaultHeader("Authorization");
1494
- }
1495
1476
  };
1496
1477
 
1497
1478
  // src/auth/authService.ts
@@ -1499,7 +1480,7 @@ var BASE_URL = "/openauth/v1";
1499
1480
  var AuthService = class _AuthService {
1500
1481
  constructor(client) {
1501
1482
  this.client = client;
1502
- this.sessionManager = SessionManager.getInstance(client);
1483
+ this.sessionManager = SessionManager.getInstance();
1503
1484
  }
1504
1485
  static getInstance(client) {
1505
1486
  if (!_AuthService.instance) {
@@ -1514,6 +1495,7 @@ var AuthService = class _AuthService {
1514
1495
  );
1515
1496
  if (response.data.accessToken) {
1516
1497
  this.sessionManager.save(response.data);
1498
+ this.client.setDefaultHeader("Authorization", `Bearer ${response.data.accessToken}`);
1517
1499
  }
1518
1500
  return response.data;
1519
1501
  }
@@ -1531,6 +1513,7 @@ var AuthService = class _AuthService {
1531
1513
  accessToken: response.data.accessToken,
1532
1514
  refreshToken: response.data.refreshToken
1533
1515
  });
1516
+ this.client.setDefaultHeader("Authorization", `Bearer ${response.data.accessToken}`);
1534
1517
  }
1535
1518
  return response.data;
1536
1519
  }
@@ -1544,6 +1527,7 @@ var AuthService = class _AuthService {
1544
1527
  return response.data;
1545
1528
  } finally {
1546
1529
  this.sessionManager.clear();
1530
+ this.client.removeDefaultHeader("Authorization");
1547
1531
  }
1548
1532
  }
1549
1533
  async generateLoginToken(request) {
@@ -1560,6 +1544,7 @@ var AuthService = class _AuthService {
1560
1544
  );
1561
1545
  if (response.data.accessToken) {
1562
1546
  this.sessionManager.save(response.data);
1547
+ this.client.setDefaultHeader("Authorization", `Bearer ${response.data.accessToken}`);
1563
1548
  }
1564
1549
  return response.data;
1565
1550
  }
@@ -1576,7 +1561,12 @@ var AuthService = class _AuthService {
1576
1561
  return this.sessionManager.getSessionId();
1577
1562
  }
1578
1563
  initializeAuth() {
1579
- 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
+ }
1580
1570
  }
1581
1571
  };
1582
1572