@hashrytech/quick-components-kit 0.13.5 → 0.14.1
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 +12 -0
- package/dist/modules/api-client.d.ts +3 -10
- package/dist/modules/api-client.js +12 -21
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @hashrytech/quick-components-kit
|
|
2
2
|
|
|
3
|
+
## 0.14.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- fix: fix for sending requests without a baseurl
|
|
8
|
+
|
|
9
|
+
## 0.14.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- refactor: Changing the setAuthToken method to setAccessToken and removing the getAccessToken method
|
|
14
|
+
|
|
3
15
|
## 0.13.5
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -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.
|
|
@@ -102,7 +97,7 @@ export class ApiClient {
|
|
|
102
97
|
* @returns A processed `Request` object ready for `fetch`.
|
|
103
98
|
*/
|
|
104
99
|
async processRequest(endpoint, method, body, options) {
|
|
105
|
-
const url = new URL(endpoint, this.baseURL); // Resolve endpoint relative to baseURL
|
|
100
|
+
const url = this.baseURL ? new URL(endpoint, this.baseURL).toString() : endpoint; // Resolve endpoint relative to baseURL
|
|
106
101
|
const requestHeaders = new Headers(this.defaultHeaders);
|
|
107
102
|
// Merge custom headers from options using Headers constructor for robustness
|
|
108
103
|
if (options.headers) {
|
|
@@ -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) {
|
|
@@ -142,7 +133,7 @@ export class ApiClient {
|
|
|
142
133
|
processedBody = body;
|
|
143
134
|
}
|
|
144
135
|
}
|
|
145
|
-
let request = new Request(url
|
|
136
|
+
let request = new Request(url, {
|
|
146
137
|
method: method,
|
|
147
138
|
headers: requestHeaders,
|
|
148
139
|
body: processedBody,
|
|
@@ -185,11 +176,11 @@ export class ApiClient {
|
|
|
185
176
|
}
|
|
186
177
|
switch (options.responseType) {
|
|
187
178
|
case 'text':
|
|
188
|
-
return
|
|
179
|
+
return await response.text();
|
|
189
180
|
case 'blob':
|
|
190
|
-
return
|
|
181
|
+
return await response.blob();
|
|
191
182
|
case 'arrayBuffer':
|
|
192
|
-
return
|
|
183
|
+
return await response.arrayBuffer();
|
|
193
184
|
case 'raw': return response;
|
|
194
185
|
case 'json':
|
|
195
186
|
default:
|
|
@@ -198,7 +189,7 @@ export class ApiClient {
|
|
|
198
189
|
return {}; // Return an empty object for no content
|
|
199
190
|
}
|
|
200
191
|
try {
|
|
201
|
-
return
|
|
192
|
+
return await response.json();
|
|
202
193
|
}
|
|
203
194
|
catch {
|
|
204
195
|
throw new Error('Failed to parse response as JSON. Response was OK, but not valid JSON.');
|
|
@@ -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) {
|