@hkdigital/lib-sveltekit 0.1.68 → 0.1.70
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/classes/cache/CacheStorage.js__ +45 -0
- package/dist/classes/cache/MemoryResponseCache.d.ts +19 -0
- package/dist/classes/cache/MemoryResponseCache.js +53 -0
- package/dist/classes/cache/PersistentResponseCache.d.ts +46 -0
- package/dist/classes/cache/PersistentResponseCache.js +165 -0
- package/dist/classes/cache/index.d.ts +3 -0
- package/dist/classes/cache/index.js +5 -0
- package/dist/classes/cache/typedef.d.ts +50 -0
- package/dist/classes/cache/typedef.js +25 -0
- package/dist/util/http/caching.d.ts +28 -0
- package/dist/util/http/caching.js +251 -0
- package/dist/util/http/headers.d.ts +33 -4
- package/dist/util/http/headers.js +57 -27
- package/dist/util/http/http-request.d.ts +93 -115
- package/dist/util/http/http-request.js +329 -262
- package/dist/util/http/json-request.d.ts +65 -45
- package/dist/util/http/json-request.js +188 -138
- package/dist/util/http/response.d.ts +91 -22
- package/dist/util/http/response.js +229 -176
- package/dist/util/http/typedef.d.ts +184 -0
- package/dist/util/http/typedef.js +93 -0
- package/dist/widgets/game-box/GameBox.svelte +2 -2
- package/package.json +1 -1
@@ -11,297 +11,364 @@ import { toURL } from './url.js';
|
|
11
11
|
import { setRequestHeaders } from './headers.js';
|
12
12
|
import { waitForAndCheckResponse } from './response.js';
|
13
13
|
|
14
|
-
|
15
|
-
* @callback requestHandler
|
16
|
-
* @param {Object} _
|
17
|
-
* @param {AbortController} _.controller
|
18
|
-
* @param {( reason?: Error ) => void} _.abort
|
19
|
-
* @param {( delayMs: number) => void} _.timeout
|
20
|
-
*/
|
14
|
+
import { getCachedResponse, storeResponseInCache } from './caching.js';
|
21
15
|
|
22
16
|
/**
|
23
|
-
*
|
24
|
-
*
|
25
|
-
* @param {object} _
|
17
|
+
* Default configuration for HTTP requests
|
26
18
|
*
|
27
|
-
*
|
19
|
+
* This object contains default settings used by the HTTP request functions.
|
20
|
+
* It can be used as a reference for available options and their default values.
|
28
21
|
*
|
29
|
-
* @
|
30
|
-
|
22
|
+
* @type {Object}
|
23
|
+
*/
|
24
|
+
export const DEFAULT_HTTP_CONFIG = {
|
25
|
+
// Request
|
26
|
+
method: METHOD_GET,
|
27
|
+
urlSearchParams: null,
|
28
|
+
body: null,
|
29
|
+
headers: null,
|
30
|
+
withCredentials: false,
|
31
|
+
timeoutMs: null, // No timeout by default
|
32
|
+
|
33
|
+
// Fetch
|
34
|
+
mode: 'cors',
|
35
|
+
cache: 'no-cache',
|
36
|
+
redirect: 'follow',
|
37
|
+
referrerPolicy: 'no-referrer',
|
38
|
+
|
39
|
+
// Cache
|
40
|
+
cacheEnabled: true
|
41
|
+
};
|
42
|
+
|
43
|
+
/**
|
44
|
+
* Make a GET request
|
31
45
|
*
|
32
|
-
*
|
33
|
-
*
|
46
|
+
* This function performs an HTTP GET request with optional parameters,
|
47
|
+
* headers, credentials, and timeout functionality.
|
34
48
|
*
|
35
|
-
*
|
49
|
+
* @param {import('./typedef').HttpRequestOptions} options
|
50
|
+
* Request configuration options
|
36
51
|
*
|
37
|
-
* @
|
52
|
+
* @returns {Promise<Response>} Response promise
|
38
53
|
*
|
39
|
-
* @
|
54
|
+
* @example
|
55
|
+
* // Basic GET request
|
56
|
+
* const response = await httpGet({
|
57
|
+
* url: 'https://api.example.com/data'
|
58
|
+
* });
|
40
59
|
*
|
41
|
-
* @
|
42
|
-
*
|
43
|
-
*
|
60
|
+
* @example
|
61
|
+
* // GET request with URL parameters and timeout
|
62
|
+
* const response = await httpGet({
|
63
|
+
* url: 'https://api.example.com/search',
|
64
|
+
* urlSearchParams: new URLSearchParams({ q: 'search term' }),
|
65
|
+
* timeoutMs: 5000
|
66
|
+
* });
|
44
67
|
*
|
45
|
-
* @
|
68
|
+
* @example
|
69
|
+
* // GET request with abort capability
|
70
|
+
* const response = await httpGet({
|
71
|
+
* url: 'https://api.example.com/large-data',
|
72
|
+
* requestHandler: ({ abort }) => {
|
73
|
+
* // Store abort function for later use
|
74
|
+
* window.abortDataRequest = abort;
|
75
|
+
* }
|
76
|
+
* });
|
46
77
|
*/
|
47
|
-
export async function httpGet({
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
requestHandler,
|
53
|
-
timeoutMs
|
54
|
-
}) {
|
55
|
-
const responsePromise = httpRequest({
|
56
|
-
method: METHOD_GET,
|
57
|
-
url,
|
58
|
-
urlSearchParams,
|
59
|
-
headers,
|
60
|
-
requestHandler,
|
61
|
-
timeoutMs
|
62
|
-
});
|
63
|
-
|
64
|
-
return await waitForAndCheckResponse(responsePromise, url);
|
78
|
+
export async function httpGet(options) {
|
79
|
+
return await httpRequest({
|
80
|
+
...options,
|
81
|
+
method: METHOD_GET
|
82
|
+
});
|
65
83
|
}
|
66
84
|
|
67
85
|
/**
|
68
|
-
* Make POST request
|
69
|
-
*
|
70
|
-
* @param {object} _
|
71
|
-
*
|
72
|
-
* @param {string|URL} _.url - Url string or URL object
|
73
|
-
*
|
74
|
-
* @param {any} [_.body] - POST data
|
75
|
-
*
|
76
|
-
* @param {object} [_.headers]
|
77
|
-
* Object that contains custom headers. A header is a name, value pair.
|
86
|
+
* Make a POST request
|
78
87
|
*
|
79
|
-
*
|
88
|
+
* This function performs an HTTP POST request with optional body,
|
89
|
+
* headers, credentials, and timeout functionality.
|
80
90
|
*
|
81
|
-
* @param {
|
91
|
+
* @param {import('./typedef').HttpRequestOptions} options
|
92
|
+
* Request configuration options
|
82
93
|
*
|
83
|
-
* @
|
94
|
+
* @returns {Promise<Response>} Response promise
|
84
95
|
*
|
85
|
-
* @
|
86
|
-
*
|
87
|
-
*
|
96
|
+
* @example
|
97
|
+
* // Basic POST request with JSON data
|
98
|
+
* const response = await httpPost({
|
99
|
+
* url: 'https://api.example.com/users',
|
100
|
+
* body: JSON.stringify({ name: 'John Doe', email: 'john@example.com' }),
|
101
|
+
* headers: { 'content-type': 'application/json' }
|
102
|
+
* });
|
88
103
|
*
|
89
|
-
* @
|
104
|
+
* @example
|
105
|
+
* // POST request with timeout
|
106
|
+
* const response = await httpPost({
|
107
|
+
* url: 'https://api.example.com/upload',
|
108
|
+
* body: formData,
|
109
|
+
* timeoutMs: 30000 // 30 seconds timeout
|
110
|
+
* });
|
90
111
|
*/
|
91
|
-
export async function httpPost({
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
requestHandler,
|
97
|
-
timeoutMs
|
98
|
-
}) {
|
99
|
-
const responsePromise = httpRequest({
|
100
|
-
method: METHOD_POST,
|
101
|
-
url,
|
102
|
-
body,
|
103
|
-
headers,
|
104
|
-
withCredentials,
|
105
|
-
requestHandler,
|
106
|
-
timeoutMs
|
107
|
-
});
|
108
|
-
|
109
|
-
return await waitForAndCheckResponse(responsePromise, url);
|
112
|
+
export async function httpPost(options) {
|
113
|
+
return await httpRequest({
|
114
|
+
...options,
|
115
|
+
method: METHOD_POST
|
116
|
+
});
|
110
117
|
}
|
111
118
|
|
112
119
|
// -----------------------------------------------------------------------------
|
113
120
|
|
114
121
|
/**
|
115
|
-
* Make an HTTP request
|
116
|
-
* - This is a low level function, consider using
|
117
|
-
* httpGet, httpPost, jsonGet or jsonPost instead
|
122
|
+
* Make an HTTP request (low-level function)
|
118
123
|
*
|
119
|
-
*
|
124
|
+
* This is a low-level function that powers httpGet and httpPost.
|
125
|
+
* It provides complete control over request configuration.
|
120
126
|
*
|
121
|
-
* @param {
|
127
|
+
* @param {import('./typedef').HttpRequestOptions} options
|
128
|
+
* Request configuration options
|
122
129
|
*
|
123
|
-
* @
|
130
|
+
* @throws {TypeError} If a network error occurred
|
131
|
+
* @returns {Promise<Response>} Response promise
|
124
132
|
*
|
125
|
-
* @
|
133
|
+
* @example
|
134
|
+
* // Custom HTTP request with PUT method
|
135
|
+
* const response = await httpRequest({
|
136
|
+
* method: 'PUT',
|
137
|
+
* url: 'https://api.example.com/resources/123',
|
138
|
+
* body: JSON.stringify({ status: 'updated' }),
|
139
|
+
* headers: { 'content-type': 'application/json' },
|
140
|
+
* withCredentials: true
|
141
|
+
* });
|
126
142
|
*
|
127
|
-
*
|
128
|
-
*
|
129
|
-
*
|
130
|
-
*
|
131
|
-
*
|
132
|
-
*
|
133
|
-
*
|
134
|
-
* @param {boolean} [_.withCredentials=false]
|
135
|
-
* Whether to include credentials (cookies, HTTP authentication, and client
|
136
|
-
* SSL certificates) with cross-origin requests. When true, sets fetch
|
137
|
-
* credentials to 'include', otherwise to 'omit'.
|
138
|
-
*
|
139
|
-
* @param {requestHandler} [_.requestHandler]
|
140
|
-
*
|
141
|
-
* @param {number} [_.timeoutMs]
|
142
|
-
* If defined, this request will abort after the specified number of
|
143
|
-
* milliseconds. Values above the the built-in request timeout won't work.
|
144
|
-
*
|
145
|
-
* @throws TypeError - If a network error occurred
|
146
|
-
*
|
147
|
-
* @note Check the `ok` property of the resolved response to check if the
|
148
|
-
* response was successfull (e.g. in case of a 404, ok is false)
|
149
|
-
*
|
150
|
-
* @returns {Promise<Response>} responsePromise
|
143
|
+
* // Check if response was successful
|
144
|
+
* if (response.ok) {
|
145
|
+
* // Process response
|
146
|
+
* } else {
|
147
|
+
* // Handle error based on status
|
148
|
+
* }
|
151
149
|
*/
|
152
|
-
export async function httpRequest({
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
150
|
+
export async function httpRequest(options) {
|
151
|
+
// Apply default configuration
|
152
|
+
const config = { ...DEFAULT_HTTP_CONFIG, ...options };
|
153
|
+
|
154
|
+
const {
|
155
|
+
method,
|
156
|
+
url: rawUrl,
|
157
|
+
urlSearchParams,
|
158
|
+
body,
|
159
|
+
headers,
|
160
|
+
withCredentials,
|
161
|
+
requestHandler,
|
162
|
+
timeoutMs,
|
163
|
+
mode,
|
164
|
+
cache,
|
165
|
+
redirect,
|
166
|
+
referrerPolicy,
|
167
|
+
cacheEnabled
|
168
|
+
} = config;
|
169
|
+
|
170
|
+
const url = toURL(rawUrl);
|
171
|
+
|
172
|
+
console.debug(`httpRequest:load [${url.pathname}]`);
|
173
|
+
|
174
|
+
// Only consider caching for GET requests
|
175
|
+
const shouldAttemptCache = cacheEnabled && method === METHOD_GET;
|
176
|
+
|
177
|
+
// Try to get from cache if appropriate
|
178
|
+
if (shouldAttemptCache && cache !== 'no-store' && cache !== 'reload') {
|
179
|
+
const cacheKeyParams = { url, ...headers };
|
180
|
+
const cachedResponse = await getCachedResponse(cacheKeyParams);
|
181
|
+
|
182
|
+
if (cachedResponse) {
|
183
|
+
console.debug(`httpRequest:cache-hit [${url.pathname}]`);
|
184
|
+
return cachedResponse;
|
185
|
+
}
|
186
|
+
else {
|
187
|
+
console.debug(`httpRequest:cache-miss [${url.pathname}]`);
|
188
|
+
}
|
189
|
+
}
|
190
|
+
|
191
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/API/Headers
|
192
|
+
const requestHeaders = new Headers();
|
193
|
+
|
194
|
+
if (headers) {
|
195
|
+
setRequestHeaders(requestHeaders, headers);
|
196
|
+
|
197
|
+
if (
|
198
|
+
headers[CONTENT_TYPE] === APPLICATION_JSON &&
|
199
|
+
typeof body !== 'string'
|
200
|
+
) {
|
201
|
+
throw new Error(
|
202
|
+
`Trying to send request with [content-type:${APPLICATION_JSON}], ` +
|
203
|
+
'but body is not a (JSON encoded) string.'
|
204
|
+
);
|
205
|
+
}
|
206
|
+
// IDEA: try to decode the body to catch errors on client side
|
207
|
+
}
|
208
|
+
|
209
|
+
/** @type {RequestInit} */
|
210
|
+
const init = {
|
211
|
+
mode,
|
212
|
+
cache,
|
213
|
+
credentials: withCredentials ? 'include': 'omit',
|
214
|
+
redirect,
|
215
|
+
referrerPolicy,
|
216
|
+
headers: requestHeaders
|
217
|
+
};
|
218
|
+
|
219
|
+
// Allow search params also for other request types than GET
|
220
|
+
if (urlSearchParams) {
|
221
|
+
if (!(urlSearchParams instanceof URLSearchParams)) {
|
222
|
+
throw new Error(
|
223
|
+
'Invalid parameter [urlSearchParams] ' +
|
224
|
+
'(expected instanceof URLSearchParams)'
|
225
|
+
);
|
226
|
+
}
|
227
|
+
|
228
|
+
const existingParams = url.searchParams;
|
229
|
+
|
230
|
+
for (const [name, value] of urlSearchParams.entries()) {
|
231
|
+
if (existingParams.has(name)) {
|
232
|
+
throw new Error(
|
233
|
+
`Cannot set URL search parameter [${name}] ` +
|
234
|
+
`in url [${url.href}] (already set)`
|
235
|
+
);
|
236
|
+
}
|
237
|
+
|
238
|
+
existingParams.set(name, value);
|
239
|
+
} // end for
|
240
|
+
}
|
241
|
+
|
242
|
+
//
|
243
|
+
// Sort search params to make the url nicer
|
244
|
+
//
|
245
|
+
url.searchParams.sort();
|
246
|
+
|
247
|
+
init.method = method;
|
248
|
+
|
249
|
+
if (METHOD_POST === method) {
|
250
|
+
init.body = body || null; /* : JSON.stringify( body ) */
|
251
|
+
}
|
252
|
+
|
253
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
|
254
|
+
const request = new Request(url, init);
|
255
|
+
|
256
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort
|
257
|
+
const controller = new AbortController();
|
258
|
+
const signal = controller.signal;
|
259
|
+
|
260
|
+
//
|
261
|
+
// A fetch() promise will reject with a TypeError when a network error
|
262
|
+
// is encountered or CORS is misconfigured on the server-side,
|
263
|
+
// although this usually means permission issues or similar
|
264
|
+
// — a 404 does not constitute a network error, for example.
|
265
|
+
// An accurate check for a successful fetch() would include checking
|
266
|
+
// that the promise resolved, then checking that the Response.ok property
|
267
|
+
// has a value of true. The code would look something like this:
|
268
|
+
//
|
269
|
+
// fetch()
|
270
|
+
// .then( () => {
|
271
|
+
// if( !response.ok ) {
|
272
|
+
// throw new Error('Network response was not OK');
|
273
|
+
// }
|
274
|
+
// ...
|
275
|
+
// }
|
276
|
+
// .catch((error) => { .. }
|
277
|
+
//
|
278
|
+
|
279
|
+
const promise = fetch(request, { signal });
|
280
|
+
|
281
|
+
if (requestHandler || timeoutMs) {
|
282
|
+
/**
|
283
|
+
* @type {(reason?: any) => void}
|
284
|
+
*/
|
285
|
+
const abort = (reason) => {
|
286
|
+
if (!reason) {
|
287
|
+
reason = new AbortError(`Request [${url.href}] aborted`);
|
288
|
+
}
|
289
|
+
|
290
|
+
controller.abort(reason);
|
291
|
+
};
|
292
|
+
|
293
|
+
/**
|
294
|
+
* Function that can be used to set a timeout on a request
|
295
|
+
*
|
296
|
+
* @param {number} delayMs - Milliseconds to wait before timeout
|
297
|
+
*/
|
298
|
+
const timeout = (delayMs = 10000) => {
|
299
|
+
expect.positiveNumber(delayMs);
|
300
|
+
|
301
|
+
const timerId = setTimeout(() => {
|
302
|
+
controller.abort(
|
303
|
+
new TimeoutError(`Request [${url.href}] timed out [${delayMs}]`)
|
304
|
+
);
|
305
|
+
}, delayMs);
|
306
|
+
|
307
|
+
promise.finally(() => {
|
308
|
+
clearTimeout(timerId);
|
309
|
+
});
|
310
|
+
};
|
311
|
+
|
312
|
+
if (timeoutMs) {
|
313
|
+
timeout(timeoutMs);
|
314
|
+
}
|
315
|
+
|
316
|
+
if (requestHandler) {
|
317
|
+
expect.function(requestHandler);
|
318
|
+
|
319
|
+
requestHandler({ controller, abort, timeout });
|
320
|
+
}
|
321
|
+
}
|
322
|
+
|
323
|
+
// Wait for the response and check it
|
324
|
+
const response = await waitForAndCheckResponse(promise, url);
|
325
|
+
|
326
|
+
// If caching is enabled, store the response in cache
|
327
|
+
if (shouldAttemptCache && response.ok) {
|
328
|
+
// Extract cache control headers
|
329
|
+
const cacheControl = response.headers.get('Cache-Control') || '';
|
330
|
+
const etag = response.headers.get('ETag');
|
331
|
+
const lastModified = response.headers.get('Last-Modified');
|
332
|
+
|
333
|
+
// Parse cache-control directives
|
334
|
+
const directives = {};
|
335
|
+
cacheControl.split(',').forEach(directive => {
|
336
|
+
const [key, value] = directive.trim().split('=');
|
337
|
+
directives[key.toLowerCase()] = value !== undefined ? value : true;
|
338
|
+
});
|
339
|
+
|
340
|
+
// Determine if cacheable
|
341
|
+
const isCacheable = !directives['no-store'] && !directives['private'];
|
342
|
+
|
343
|
+
if (isCacheable) {
|
344
|
+
// Calculate expiration time
|
345
|
+
let expires = null;
|
346
|
+
if (directives['max-age']) {
|
347
|
+
const maxAge = parseInt(directives['max-age'], 10);
|
348
|
+
expires = Date.now() + (maxAge * 1000);
|
349
|
+
} else if (response.headers.get('Expires')) {
|
350
|
+
expires = new Date(response.headers.get('Expires')).getTime();
|
351
|
+
}
|
352
|
+
|
353
|
+
// Create stale info
|
354
|
+
const staleInfo = {
|
355
|
+
isStale: false,
|
356
|
+
fresh: null,
|
357
|
+
timestamp: Date.now(),
|
358
|
+
expires
|
359
|
+
};
|
360
|
+
|
361
|
+
// Store response in cache
|
362
|
+
const cacheKeyParams = { url, ...headers };
|
363
|
+
await storeResponseInCache(cacheKeyParams, response.clone(), {
|
364
|
+
etag,
|
365
|
+
lastModified,
|
366
|
+
expires,
|
367
|
+
immutable: directives['immutable'] || false
|
368
|
+
});
|
369
|
+
}
|
370
|
+
}
|
371
|
+
|
372
|
+
return response;
|
307
373
|
}
|
374
|
+
|