@msssystems/mss-link-sdk 0.2.1 → 0.2.2
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.
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
export interface HttpClientOptions {
|
|
2
2
|
baseUrl: string;
|
|
3
|
-
token?: string | (() => string | Promise<string>);
|
|
3
|
+
token?: string | (() => string | Promise<string>) | undefined;
|
|
4
4
|
fetch?: typeof fetch;
|
|
5
|
+
onUnauthorized?: (() => Promise<void>) | undefined;
|
|
5
6
|
}
|
|
6
7
|
export declare class HttpClient {
|
|
7
8
|
private baseUrl;
|
|
8
9
|
private token?;
|
|
9
10
|
private customFetch;
|
|
11
|
+
private onUnauthorized?;
|
|
10
12
|
constructor(options: HttpClientOptions);
|
|
11
13
|
setToken(token: string): Promise<void>;
|
|
12
14
|
private getHeaders;
|
package/dist/core/http-client.js
CHANGED
|
@@ -3,9 +3,11 @@ export class HttpClient {
|
|
|
3
3
|
baseUrl;
|
|
4
4
|
token;
|
|
5
5
|
customFetch;
|
|
6
|
+
onUnauthorized;
|
|
6
7
|
constructor(options) {
|
|
7
8
|
this.baseUrl = options.baseUrl.replace(/\/$/, '');
|
|
8
9
|
this.token = options.token;
|
|
10
|
+
this.onUnauthorized = options.onUnauthorized;
|
|
9
11
|
// Fallback to global fetch if not provided
|
|
10
12
|
this.customFetch = options.fetch ?? (typeof globalThis !== 'undefined' ? globalThis.fetch.bind(globalThis) : fetch);
|
|
11
13
|
}
|
|
@@ -42,7 +44,18 @@ export class HttpClient {
|
|
|
42
44
|
if (options.body) {
|
|
43
45
|
fetchOptions.body = JSON.stringify(options.body);
|
|
44
46
|
}
|
|
45
|
-
|
|
47
|
+
let response = await this.customFetch(url.toString(), fetchOptions);
|
|
48
|
+
if (response.status === 401 && this.onUnauthorized) {
|
|
49
|
+
try {
|
|
50
|
+
await this.onUnauthorized();
|
|
51
|
+
// Update headers with new token
|
|
52
|
+
fetchOptions.headers = await this.getHeaders(options.headers);
|
|
53
|
+
response = await this.customFetch(url.toString(), fetchOptions);
|
|
54
|
+
}
|
|
55
|
+
catch (refreshError) {
|
|
56
|
+
// If refresh fails, let the original 401 error throw below
|
|
57
|
+
}
|
|
58
|
+
}
|
|
46
59
|
if (!response.ok) {
|
|
47
60
|
let errorPayload;
|
|
48
61
|
try {
|