@hkdigital/lib-sveltekit 0.2.19 → 0.2.20

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.
@@ -185,7 +185,7 @@ export default class Logger extends EventEmitter {
185
185
  level,
186
186
  message,
187
187
  context: this.#hasContext ? this.#defaultContext : null,
188
- details
188
+ details: details ?? null
189
189
  };
190
190
 
191
191
  // Emit as both specific level event and generic 'log' event
@@ -1,2 +1,7 @@
1
1
  export const METHOD_GET: "GET";
2
2
  export const METHOD_POST: "POST";
3
+ export const METHOD_PUT: "PUT";
4
+ export const METHOD_DELETE: "DELETE";
5
+ export const METHOD_PATCH: "PATCH";
6
+ export const METHOD_OPTIONS: "OPTIONS";
7
+ export const METHOD_HEAD: "HEAD";
@@ -1,2 +1,14 @@
1
+ // Get & Post
1
2
  export const METHOD_GET = 'GET';
2
3
  export const METHOD_POST = 'POST';
4
+
5
+ // And the REST
6
+ export const METHOD_PUT = 'PUT';
7
+ export const METHOD_DELETE = 'DELETE';
8
+ export const METHOD_PATCH = 'PATCH';
9
+
10
+ // Check CORS
11
+ export const METHOD_OPTIONS = 'OPTIONS';
12
+
13
+ // Head
14
+ export const METHOD_HEAD = 'HEAD';
@@ -62,6 +62,170 @@ export function httpGet(options: import("./typedef").HttpRequestOptions): Promis
62
62
  * });
63
63
  */
64
64
  export function httpPost(options: import("./typedef").HttpRequestOptions): Promise<Response>;
65
+ /**
66
+ * Make a PUT request
67
+ *
68
+ * This function performs an HTTP PUT request, typically used for updating
69
+ * or replacing resources. It supports optional body, headers, credentials,
70
+ * and timeout functionality.
71
+ *
72
+ * @param {import('./typedef').HttpRequestOptions} options
73
+ * Request configuration options
74
+ *
75
+ * @returns {Promise<Response>} Response promise
76
+ *
77
+ * @example
78
+ * // Update a user resource
79
+ * const response = await httpPut({
80
+ * url: 'https://api.example.com/users/123',
81
+ * body: JSON.stringify({ name: 'John Doe', email: 'john.doe@example.com' }),
82
+ * headers: { 'content-type': 'application/json' }
83
+ * });
84
+ *
85
+ * @example
86
+ * // Replace a resource with timeout
87
+ * const response = await httpPut({
88
+ * url: 'https://api.example.com/documents/456',
89
+ * body: documentData,
90
+ * timeoutMs: 10000 // 10 seconds timeout
91
+ * });
92
+ */
93
+ export function httpPut(options: import("./typedef").HttpRequestOptions): Promise<Response>;
94
+ /**
95
+ * Make a DELETE request
96
+ *
97
+ * This function performs an HTTP DELETE request, typically used for
98
+ * removing resources. It supports optional headers, credentials,
99
+ * and timeout functionality.
100
+ *
101
+ * @param {import('./typedef').HttpRequestOptions} options
102
+ * Request configuration options
103
+ *
104
+ * @returns {Promise<Response>} Response promise
105
+ *
106
+ * @example
107
+ * // Delete a user resource
108
+ * const response = await httpDelete({
109
+ * url: 'https://api.example.com/users/123'
110
+ * });
111
+ *
112
+ * @example
113
+ * // Delete with authorization and timeout
114
+ * const response = await httpDelete({
115
+ * url: 'https://api.example.com/posts/456',
116
+ * headers: { 'authorization': 'Bearer token123' },
117
+ * timeoutMs: 5000
118
+ * });
119
+ */
120
+ export function httpDelete(options: import("./typedef").HttpRequestOptions): Promise<Response>;
121
+ /**
122
+ * Make a PATCH request
123
+ *
124
+ * This function performs an HTTP PATCH request, typically used for
125
+ * partial updates to resources. It supports optional body, headers,
126
+ * credentials, and timeout functionality.
127
+ *
128
+ * @param {import('./typedef').HttpRequestOptions} options
129
+ * Request configuration options
130
+ *
131
+ * @returns {Promise<Response>} Response promise
132
+ *
133
+ * @example
134
+ * // Partially update a user's email
135
+ * const response = await httpPatch({
136
+ * url: 'https://api.example.com/users/123',
137
+ * body: JSON.stringify({ email: 'newemail@example.com' }),
138
+ * headers: { 'content-type': 'application/json' }
139
+ * });
140
+ *
141
+ * @example
142
+ * // Apply JSON Patch operations
143
+ * const response = await httpPatch({
144
+ * url: 'https://api.example.com/documents/456',
145
+ * body: JSON.stringify([
146
+ * { op: 'replace', path: '/title', value: 'New Title' },
147
+ * { op: 'add', path: '/tags/-', value: 'updated' }
148
+ * ]),
149
+ * headers: { 'content-type': 'application/json-patch+json' }
150
+ * });
151
+ */
152
+ export function httpPatch(options: import("./typedef").HttpRequestOptions): Promise<Response>;
153
+ /**
154
+ * Make an OPTIONS request
155
+ *
156
+ * This function performs an HTTP OPTIONS request, typically used for
157
+ * discovering allowed methods on a resource or for CORS preflight requests.
158
+ * It supports optional headers, credentials, and timeout functionality.
159
+ *
160
+ * @param {import('./typedef').HttpRequestOptions} options
161
+ * Request configuration options
162
+ *
163
+ * @returns {Promise<Response>} Response promise
164
+ *
165
+ * @example
166
+ * // Check allowed methods on a resource
167
+ * const response = await httpOptions({
168
+ * url: 'https://api.example.com/users/123'
169
+ * });
170
+ *
171
+ * const allowedMethods = response.headers.get('Allow');
172
+ * console.log('Allowed methods:', allowedMethods);
173
+ *
174
+ * @example
175
+ * // CORS preflight check
176
+ * const response = await httpOptions({
177
+ * url: 'https://api.example.com/data',
178
+ * headers: {
179
+ * 'Access-Control-Request-Method': 'POST',
180
+ * 'Access-Control-Request-Headers': 'Content-Type'
181
+ * }
182
+ * });
183
+ */
184
+ export function httpOptions(options: import("./typedef").HttpRequestOptions): Promise<Response>;
185
+ /**
186
+ * Make a HEAD request
187
+ *
188
+ * This function performs an HTTP HEAD request, which returns only the
189
+ * headers of a response without the body. It's useful for checking
190
+ * resource existence, getting metadata, or cache validation.
191
+ *
192
+ * @param {import('./typedef').HttpRequestOptions} options
193
+ * Request configuration options
194
+ *
195
+ * @returns {Promise<Response>} Response promise (with empty body)
196
+ *
197
+ * @example
198
+ * // Check if a resource exists
199
+ * const response = await httpHead({
200
+ * url: 'https://api.example.com/users/123'
201
+ * });
202
+ *
203
+ * if (response.ok) {
204
+ * console.log('User exists');
205
+ * console.log('Last modified:', response.headers.get('Last-Modified'));
206
+ * }
207
+ *
208
+ * @example
209
+ * // Get content length without downloading
210
+ * const response = await httpHead({
211
+ * url: 'https://api.example.com/large-file.zip'
212
+ * });
213
+ *
214
+ * const contentLength = response.headers.get('Content-Length');
215
+ * console.log('File size:', contentLength, 'bytes');
216
+ *
217
+ * @example
218
+ * // Cache validation
219
+ * const response = await httpHead({
220
+ * url: 'https://api.example.com/data',
221
+ * headers: { 'If-None-Match': '"cached-etag-value"' }
222
+ * });
223
+ *
224
+ * if (response.status === 304) {
225
+ * console.log('Cache is still valid');
226
+ * }
227
+ */
228
+ export function httpHead(options: import("./typedef").HttpRequestOptions): Promise<Response>;
65
229
  /**
66
230
  * Make an HTTP request (low-level function)
67
231
  *
@@ -1,4 +1,12 @@
1
- import { METHOD_GET, METHOD_POST } from '../../constants/http/methods.js';
1
+ import {
2
+ METHOD_GET,
3
+ METHOD_POST,
4
+ METHOD_PUT,
5
+ METHOD_DELETE,
6
+ METHOD_PATCH,
7
+ METHOD_OPTIONS,
8
+ METHOD_HEAD
9
+ } from '../../constants/http/methods.js';
2
10
 
3
11
  import { APPLICATION_JSON } from '../../constants/mime/application.js';
4
12
  import { CONTENT_TYPE } from '../../constants/http/headers.js';
@@ -30,7 +38,7 @@ export const DEFAULT_HTTP_CONFIG = {
30
38
  body: null,
31
39
  headers: null,
32
40
  withCredentials: false,
33
- timeoutMs: null, // No timeout by default
41
+ timeoutMs: null, // No timeout by default
34
42
 
35
43
  // Fetch
36
44
  mode: 'cors',
@@ -118,6 +126,200 @@ export async function httpPost(options) {
118
126
  });
119
127
  }
120
128
 
129
+ /**
130
+ * Make a PUT request
131
+ *
132
+ * This function performs an HTTP PUT request, typically used for updating
133
+ * or replacing resources. It supports optional body, headers, credentials,
134
+ * and timeout functionality.
135
+ *
136
+ * @param {import('./typedef').HttpRequestOptions} options
137
+ * Request configuration options
138
+ *
139
+ * @returns {Promise<Response>} Response promise
140
+ *
141
+ * @example
142
+ * // Update a user resource
143
+ * const response = await httpPut({
144
+ * url: 'https://api.example.com/users/123',
145
+ * body: JSON.stringify({ name: 'John Doe', email: 'john.doe@example.com' }),
146
+ * headers: { 'content-type': 'application/json' }
147
+ * });
148
+ *
149
+ * @example
150
+ * // Replace a resource with timeout
151
+ * const response = await httpPut({
152
+ * url: 'https://api.example.com/documents/456',
153
+ * body: documentData,
154
+ * timeoutMs: 10000 // 10 seconds timeout
155
+ * });
156
+ */
157
+ export async function httpPut(options) {
158
+ return await httpRequest({
159
+ ...options,
160
+ method: METHOD_PUT
161
+ });
162
+ }
163
+
164
+ /**
165
+ * Make a DELETE request
166
+ *
167
+ * This function performs an HTTP DELETE request, typically used for
168
+ * removing resources. It supports optional headers, credentials,
169
+ * and timeout functionality.
170
+ *
171
+ * @param {import('./typedef').HttpRequestOptions} options
172
+ * Request configuration options
173
+ *
174
+ * @returns {Promise<Response>} Response promise
175
+ *
176
+ * @example
177
+ * // Delete a user resource
178
+ * const response = await httpDelete({
179
+ * url: 'https://api.example.com/users/123'
180
+ * });
181
+ *
182
+ * @example
183
+ * // Delete with authorization and timeout
184
+ * const response = await httpDelete({
185
+ * url: 'https://api.example.com/posts/456',
186
+ * headers: { 'authorization': 'Bearer token123' },
187
+ * timeoutMs: 5000
188
+ * });
189
+ */
190
+ export async function httpDelete(options) {
191
+ return await httpRequest({
192
+ ...options,
193
+ method: METHOD_DELETE
194
+ });
195
+ }
196
+
197
+ /**
198
+ * Make a PATCH request
199
+ *
200
+ * This function performs an HTTP PATCH request, typically used for
201
+ * partial updates to resources. It supports optional body, headers,
202
+ * credentials, and timeout functionality.
203
+ *
204
+ * @param {import('./typedef').HttpRequestOptions} options
205
+ * Request configuration options
206
+ *
207
+ * @returns {Promise<Response>} Response promise
208
+ *
209
+ * @example
210
+ * // Partially update a user's email
211
+ * const response = await httpPatch({
212
+ * url: 'https://api.example.com/users/123',
213
+ * body: JSON.stringify({ email: 'newemail@example.com' }),
214
+ * headers: { 'content-type': 'application/json' }
215
+ * });
216
+ *
217
+ * @example
218
+ * // Apply JSON Patch operations
219
+ * const response = await httpPatch({
220
+ * url: 'https://api.example.com/documents/456',
221
+ * body: JSON.stringify([
222
+ * { op: 'replace', path: '/title', value: 'New Title' },
223
+ * { op: 'add', path: '/tags/-', value: 'updated' }
224
+ * ]),
225
+ * headers: { 'content-type': 'application/json-patch+json' }
226
+ * });
227
+ */
228
+ export async function httpPatch(options) {
229
+ return await httpRequest({
230
+ ...options,
231
+ method: METHOD_PATCH
232
+ });
233
+ }
234
+
235
+ /**
236
+ * Make an OPTIONS request
237
+ *
238
+ * This function performs an HTTP OPTIONS request, typically used for
239
+ * discovering allowed methods on a resource or for CORS preflight requests.
240
+ * It supports optional headers, credentials, and timeout functionality.
241
+ *
242
+ * @param {import('./typedef').HttpRequestOptions} options
243
+ * Request configuration options
244
+ *
245
+ * @returns {Promise<Response>} Response promise
246
+ *
247
+ * @example
248
+ * // Check allowed methods on a resource
249
+ * const response = await httpOptions({
250
+ * url: 'https://api.example.com/users/123'
251
+ * });
252
+ *
253
+ * const allowedMethods = response.headers.get('Allow');
254
+ * console.log('Allowed methods:', allowedMethods);
255
+ *
256
+ * @example
257
+ * // CORS preflight check
258
+ * const response = await httpOptions({
259
+ * url: 'https://api.example.com/data',
260
+ * headers: {
261
+ * 'Access-Control-Request-Method': 'POST',
262
+ * 'Access-Control-Request-Headers': 'Content-Type'
263
+ * }
264
+ * });
265
+ */
266
+ export async function httpOptions(options) {
267
+ return await httpRequest({
268
+ ...options,
269
+ method: METHOD_OPTIONS
270
+ });
271
+ }
272
+
273
+ /**
274
+ * Make a HEAD request
275
+ *
276
+ * This function performs an HTTP HEAD request, which returns only the
277
+ * headers of a response without the body. It's useful for checking
278
+ * resource existence, getting metadata, or cache validation.
279
+ *
280
+ * @param {import('./typedef').HttpRequestOptions} options
281
+ * Request configuration options
282
+ *
283
+ * @returns {Promise<Response>} Response promise (with empty body)
284
+ *
285
+ * @example
286
+ * // Check if a resource exists
287
+ * const response = await httpHead({
288
+ * url: 'https://api.example.com/users/123'
289
+ * });
290
+ *
291
+ * if (response.ok) {
292
+ * console.log('User exists');
293
+ * console.log('Last modified:', response.headers.get('Last-Modified'));
294
+ * }
295
+ *
296
+ * @example
297
+ * // Get content length without downloading
298
+ * const response = await httpHead({
299
+ * url: 'https://api.example.com/large-file.zip'
300
+ * });
301
+ *
302
+ * const contentLength = response.headers.get('Content-Length');
303
+ * console.log('File size:', contentLength, 'bytes');
304
+ *
305
+ * @example
306
+ * // Cache validation
307
+ * const response = await httpHead({
308
+ * url: 'https://api.example.com/data',
309
+ * headers: { 'If-None-Match': '"cached-etag-value"' }
310
+ * });
311
+ *
312
+ * if (response.status === 304) {
313
+ * console.log('Cache is still valid');
314
+ * }
315
+ */
316
+ export async function httpHead(options) {
317
+ return await httpRequest({
318
+ ...options,
319
+ method: METHOD_HEAD
320
+ });
321
+ }
322
+
121
323
  // -----------------------------------------------------------------------------
122
324
 
123
325
  /**
@@ -181,13 +383,11 @@ export async function httpRequest(options) {
181
383
  const cacheKeyParams = { url, ...headers };
182
384
  const cachedResponse = await getCachedResponse(cacheKeyParams);
183
385
 
184
- if( !isTestEnv )
185
- {
386
+ if (!isTestEnv) {
186
387
  if (cachedResponse) {
187
388
  console.debug(`http:cache-hit [${url.pathname}]`);
188
389
  return cachedResponse;
189
- }
190
- else {
390
+ } else {
191
391
  console.debug(`http:cache-miss [${url.pathname}]`);
192
392
  }
193
393
  }
@@ -215,7 +415,7 @@ export async function httpRequest(options) {
215
415
  const init = {
216
416
  mode,
217
417
  cache,
218
- credentials: withCredentials ? 'include': 'omit',
418
+ credentials: withCredentials ? 'include' : 'omit',
219
419
  redirect,
220
420
  referrerPolicy,
221
421
  headers: requestHeaders
@@ -236,7 +436,7 @@ export async function httpRequest(options) {
236
436
  if (existingParams.has(name)) {
237
437
  throw new Error(
238
438
  `Cannot set URL search parameter [${name}] ` +
239
- `in url [${url.href}] (already set)`
439
+ `in url [${url.href}] (already set)`
240
440
  );
241
441
  }
242
442
 
@@ -337,7 +537,7 @@ export async function httpRequest(options) {
337
537
 
338
538
  // Parse cache-control directives
339
539
  const directives = {};
340
- cacheControl.split(',').forEach(directive => {
540
+ cacheControl.split(',').forEach((directive) => {
341
541
  const [key, value] = directive.trim().split('=');
342
542
  directives[key.toLowerCase()] = value !== undefined ? value : true;
343
543
  });
@@ -350,7 +550,7 @@ export async function httpRequest(options) {
350
550
  let expires = null;
351
551
  if (directives['max-age']) {
352
552
  const maxAge = parseInt(directives['max-age'], 10);
353
- expires = Date.now() + (maxAge * 1000);
553
+ expires = Date.now() + maxAge * 1000;
354
554
  } else if (response.headers.get('Expires')) {
355
555
  expires = new Date(response.headers.get('Expires')).getTime();
356
556
  }
@@ -376,4 +576,3 @@ export async function httpRequest(options) {
376
576
 
377
577
  return response;
378
578
  }
379
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-sveltekit",
3
- "version": "0.2.19",
3
+ "version": "0.2.20",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"
@@ -19,20 +19,36 @@
19
19
  "valibot"
20
20
  ],
21
21
  "scripts": {
22
- "dev": "vite dev",
23
- "build": "vite build && npm run package",
22
+ "dev": "run-s svelte:sync dev:start",
23
+ "build": "run-s svelte:sync build:*",
24
24
  "preview": "vite preview",
25
- "package": "svelte-kit sync && svelte-package && publint",
26
- "prepublishOnly": "npm run package",
27
- "publish:npm": "npm version patch && npm publish --access public && git push",
28
- "check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json",
29
- "check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch",
25
+ "check": "run-s svelte:sync check:run",
26
+ "check:watch": "run-s svelte:sync check:watch-run",
30
27
  "format": "prettier --write .",
31
- "lint": "prettier --check . && eslint .",
28
+ "lint": "run-s lint:*",
29
+ "test": "run-s test:unit-run",
30
+ "prepack": "run-s prepack:*",
31
+ "publish:npm": "run-s publish:npm:*",
32
+ "upgrade:hk": "run-s upgrade:hk:update pnpm:install",
33
+ "upgrade:all": "run-s upgrade:all:update pnpm:install",
34
+ "svelte:sync": "svelte-kit sync",
35
+ "dev:start": "vite dev",
36
+ "build:vite": "vite build",
37
+ "check:run": "svelte-check --tsconfig ./jsconfig.json",
38
+ "check:watch-run": "svelte-check --tsconfig ./jsconfig.json --watch",
39
+ "lint:prettier": "prettier --check .",
40
+ "lint:eslint": "eslint .",
32
41
  "test:unit": "vitest",
33
- "test": "npm run test:unit -- --run && npm run test:e2e",
34
- "test:e2e": "playwright test",
35
- "upgrade:all": "ncu -u && pnpm install"
42
+ "test:unit-run": "pnpm run test:unit -- --run",
43
+ "prepack:sync": "svelte-kit sync",
44
+ "prepack:build": "svelte-package",
45
+ "prepack:lint": "publint",
46
+ "publish:npm:version": "npm version patch",
47
+ "publish:npm:publish": "npm publish",
48
+ "publish:npm:push": "git push",
49
+ "upgrade:hk:update": "ncu --dep dev,optional,peer,prod '@hkdigital/*' -u",
50
+ "upgrade:all:update": "ncu --dep dev,optional,peer,prod -u",
51
+ "pnpm:install": "pnpm install"
36
52
  },
37
53
  "files": [
38
54
  "dist",
@@ -90,6 +106,7 @@
90
106
  "fake-indexeddb": "^6.0.0",
91
107
  "globals": "^16.2.0",
92
108
  "jsdom": "^26.1.0",
109
+ "npm-run-all": "^4.1.5",
93
110
  "pino": "^9.7.0",
94
111
  "pino-pretty": "^13.0.0",
95
112
  "postcss": "^8.5.4",