@hashrytech/quick-components-kit 0.13.5 → 0.14.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/CHANGELOG.md +6 -0
- package/dist/modules/api-client.d.ts +3 -10
- package/dist/modules/api-client.js +6 -15
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -26,12 +26,7 @@ export interface ApiClientConfig {
|
|
|
26
26
|
baseURL: string;
|
|
27
27
|
/** Default headers to be sent with every request. */
|
|
28
28
|
defaultHeaders?: HeadersInit;
|
|
29
|
-
/**
|
|
30
|
-
* operations where you might store the token in a Svelte store or similar.
|
|
31
|
-
* For server-side operations using SvelteKit's `fetch`, the token is typically
|
|
32
|
-
* handled by `src/hooks.server.ts` via `handleFetch`.
|
|
33
|
-
*/
|
|
34
|
-
getAccessToken?: () => string | undefined;
|
|
29
|
+
/** Specified redirects if the response matches the specified rules. */
|
|
35
30
|
autoRedirects?: AutoRedirectRule[];
|
|
36
31
|
}
|
|
37
32
|
export interface RequestOptions extends RequestInit {
|
|
@@ -86,8 +81,7 @@ export interface ApiClientEvents {
|
|
|
86
81
|
export declare class ApiClient {
|
|
87
82
|
private baseURL;
|
|
88
83
|
private defaultHeaders;
|
|
89
|
-
private
|
|
90
|
-
private getAccessTokenFromStore;
|
|
84
|
+
private accessToken;
|
|
91
85
|
private fetchInstance?;
|
|
92
86
|
private autoRedirects;
|
|
93
87
|
private requestInterceptors;
|
|
@@ -103,10 +97,9 @@ export declare class ApiClient {
|
|
|
103
97
|
});
|
|
104
98
|
/**
|
|
105
99
|
* Sets the Bearer token for client-side requests.
|
|
106
|
-
* This will override `getAccessToken` for subsequent requests using this client instance.
|
|
107
100
|
* @param token - The access token string, or undefined to clear it.
|
|
108
101
|
*/
|
|
109
|
-
|
|
102
|
+
setAccessToken(token: string | undefined): void;
|
|
110
103
|
/**
|
|
111
104
|
* Sets a custom fetch implementation globally for all requests made by this client.
|
|
112
105
|
* Useful for passing enhanced `fetch` from SvelteKit's load functions.
|
|
@@ -32,10 +32,7 @@ export class ApiError extends Error {
|
|
|
32
32
|
export class ApiClient {
|
|
33
33
|
baseURL;
|
|
34
34
|
defaultHeaders;
|
|
35
|
-
|
|
36
|
-
// otherwise relies on getAccessTokenFromStore or SvelteKit's `handleFetch`.
|
|
37
|
-
clientAuthToken;
|
|
38
|
-
getAccessTokenFromStore;
|
|
35
|
+
accessToken;
|
|
39
36
|
fetchInstance;
|
|
40
37
|
autoRedirects = [];
|
|
41
38
|
requestInterceptors = [];
|
|
@@ -49,16 +46,14 @@ export class ApiClient {
|
|
|
49
46
|
constructor(config) {
|
|
50
47
|
this.baseURL = config.baseURL;
|
|
51
48
|
this.defaultHeaders = config.defaultHeaders || { 'Content-Type': 'application/json' };
|
|
52
|
-
this.getAccessTokenFromStore = config.getAccessToken;
|
|
53
49
|
this.autoRedirects = config.autoRedirects || [];
|
|
54
50
|
}
|
|
55
51
|
/**
|
|
56
52
|
* Sets the Bearer token for client-side requests.
|
|
57
|
-
* This will override `getAccessToken` for subsequent requests using this client instance.
|
|
58
53
|
* @param token - The access token string, or undefined to clear it.
|
|
59
54
|
*/
|
|
60
|
-
|
|
61
|
-
this.
|
|
55
|
+
setAccessToken(token) {
|
|
56
|
+
this.accessToken = token;
|
|
62
57
|
}
|
|
63
58
|
/**
|
|
64
59
|
* Sets a custom fetch implementation globally for all requests made by this client.
|
|
@@ -113,12 +108,8 @@ export class ApiClient {
|
|
|
113
108
|
}
|
|
114
109
|
}
|
|
115
110
|
// Add auth token unless skipped
|
|
116
|
-
if (!options.skipAuth) {
|
|
117
|
-
|
|
118
|
-
const token = this.clientAuthToken || (this.getAccessTokenFromStore ? this.getAccessTokenFromStore() : undefined);
|
|
119
|
-
if (token) {
|
|
120
|
-
requestHeaders.set('Authorization', `Bearer ${token}`);
|
|
121
|
-
}
|
|
111
|
+
if (!options.skipAuth && this.accessToken) {
|
|
112
|
+
requestHeaders.set('Authorization', `Bearer ${this.accessToken}`);
|
|
122
113
|
}
|
|
123
114
|
let processedBody = undefined;
|
|
124
115
|
if (body instanceof FormData) {
|
|
@@ -400,7 +391,7 @@ export class ApiClient {
|
|
|
400
391
|
const url = new URL(endpoint, this.baseURL).toString();
|
|
401
392
|
const formData = new FormData();
|
|
402
393
|
formData.append(fieldName, file);
|
|
403
|
-
const token = this.
|
|
394
|
+
const token = this.accessToken;
|
|
404
395
|
const headers = new Headers(this.defaultHeaders);
|
|
405
396
|
// Merge user-supplied headers
|
|
406
397
|
if (options.headers) {
|