@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.
@@ -1,11 +1,40 @@
1
1
  /**
2
2
  * Set headers in an existing `Headers` object
3
- * - Existing headers with the same name will be overwritten
4
- * - The supplied `Headers` object will be updated, this function does not
5
- * return any value
6
3
  *
7
- * @param {Headers} target - Headers object to set the extra headers in
4
+ * This function adds or updates headers in an existing Headers object.
5
+ * Existing headers with the same name will be overwritten.
6
+ * The supplied `Headers` object will be updated in place.
7
+ *
8
+ * @param {Headers} target
9
+ * Headers object to set the extra headers in
8
10
  *
9
11
  * @param {null|object|[string, string][]} [pairs]
12
+ * Headers to add, can be null, an object of name-value pairs,
13
+ * or an array of [name, value] pairs
14
+ *
15
+ * @throws {Error} If target is not a Headers object
16
+ * @throws {Error} If pairs is a Headers object
17
+ * @throws {Error} If pairs is not null, object, or array
18
+ * @throws {Error} If any header name or value is empty
19
+ *
20
+ * @example
21
+ * // Add headers from an object
22
+ * const headers = new Headers();
23
+ * setRequestHeaders(headers, {
24
+ * 'content-type': 'application/json',
25
+ * 'accept': 'application/json'
26
+ * });
27
+ *
28
+ * @example
29
+ * // Add headers from an array of pairs
30
+ * const headers = new Headers();
31
+ * setRequestHeaders(headers, [
32
+ * ['content-type', 'application/json'],
33
+ * ['authorization', 'Bearer token123']
34
+ * ]);
35
+ *
36
+ * @example
37
+ * // Passing null is valid and does nothing
38
+ * setRequestHeaders(headers, null);
10
39
  */
11
40
  export function setRequestHeaders(target: Headers, pairs?: null | object | [string, string][]): void;
@@ -2,44 +2,74 @@ import * as expect from '../expect/index.js';
2
2
 
3
3
  /**
4
4
  * Set headers in an existing `Headers` object
5
- * - Existing headers with the same name will be overwritten
6
- * - The supplied `Headers` object will be updated, this function does not
7
- * return any value
8
5
  *
9
- * @param {Headers} target - Headers object to set the extra headers in
6
+ * This function adds or updates headers in an existing Headers object.
7
+ * Existing headers with the same name will be overwritten.
8
+ * The supplied `Headers` object will be updated in place.
9
+ *
10
+ * @param {Headers} target
11
+ * Headers object to set the extra headers in
10
12
  *
11
13
  * @param {null|object|[string, string][]} [pairs]
14
+ * Headers to add, can be null, an object of name-value pairs,
15
+ * or an array of [name, value] pairs
16
+ *
17
+ * @throws {Error} If target is not a Headers object
18
+ * @throws {Error} If pairs is a Headers object
19
+ * @throws {Error} If pairs is not null, object, or array
20
+ * @throws {Error} If any header name or value is empty
21
+ *
22
+ * @example
23
+ * // Add headers from an object
24
+ * const headers = new Headers();
25
+ * setRequestHeaders(headers, {
26
+ * 'content-type': 'application/json',
27
+ * 'accept': 'application/json'
28
+ * });
29
+ *
30
+ * @example
31
+ * // Add headers from an array of pairs
32
+ * const headers = new Headers();
33
+ * setRequestHeaders(headers, [
34
+ * ['content-type', 'application/json'],
35
+ * ['authorization', 'Bearer token123']
36
+ * ]);
37
+ *
38
+ * @example
39
+ * // Passing null is valid and does nothing
40
+ * setRequestHeaders(headers, null);
12
41
  */
13
42
  export function setRequestHeaders(target, pairs) {
14
- if (!(target instanceof Headers)) {
15
- throw new Error('Invalid parameter [target] (expected Headers object)');
16
- }
43
+ if (!(target instanceof Headers)) {
44
+ throw new Error('Invalid parameter [target] (expected Headers object)');
45
+ }
17
46
 
18
- // expect.objectNoArray(pairs);
47
+ // expect.objectNoArray(pairs);
19
48
 
20
- if (pairs instanceof Headers) {
21
- throw new Error('Invalid parameter [pairs] (should not be a Headers object)');
22
- }
49
+ if (pairs instanceof Headers) {
50
+ throw new Error(
51
+ 'Invalid parameter [pairs] (should not be a Headers object)');
52
+ }
23
53
 
24
- if (!pairs) {
25
- return;
26
- }
54
+ if (!pairs) {
55
+ return;
56
+ }
27
57
 
28
- if (typeof pairs !== 'object') {
29
- throw new Error('Invalid value for parameter [pairs]');
30
- }
58
+ if (typeof pairs !== 'object') {
59
+ throw new Error('Invalid value for parameter [pairs]');
60
+ }
31
61
 
32
- if (!Array.isArray(pairs)) {
33
- pairs = Object.entries(pairs);
34
- }
62
+ if (!Array.isArray(pairs)) {
63
+ pairs = Object.entries(pairs);
64
+ }
35
65
 
36
- for (const [name, value] of pairs) {
37
- expect.notEmptyString(name);
38
- expect.notEmptyString(value);
66
+ for (const [name, value] of pairs) {
67
+ expect.notEmptyString(name);
68
+ expect.notEmptyString(value);
39
69
 
40
- // @note Headers should be encoded lowercase in HTTP2
41
- const nameLower = name.toLowerCase();
70
+ // @note Headers should be encoded lowercase in HTTP2
71
+ const nameLower = name.toLowerCase();
42
72
 
43
- target.set(nameLower, value); /* overwrites existing value */
44
- }
73
+ target.set(nameLower, value); /* overwrites existing value */
74
+ }
45
75
  }
@@ -1,125 +1,103 @@
1
1
  /**
2
- * @callback requestHandler
3
- * @param {Object} _
4
- * @param {AbortController} _.controller
5
- * @param {( reason?: Error ) => void} _.abort
6
- * @param {( delayMs: number) => void} _.timeout
2
+ * Make a GET request
3
+ *
4
+ * This function performs an HTTP GET request with optional parameters,
5
+ * headers, credentials, and timeout functionality.
6
+ *
7
+ * @param {import('./typedef').HttpRequestOptions} options
8
+ * Request configuration options
9
+ *
10
+ * @returns {Promise<Response>} Response promise
11
+ *
12
+ * @example
13
+ * // Basic GET request
14
+ * const response = await httpGet({
15
+ * url: 'https://api.example.com/data'
16
+ * });
17
+ *
18
+ * @example
19
+ * // GET request with URL parameters and timeout
20
+ * const response = await httpGet({
21
+ * url: 'https://api.example.com/search',
22
+ * urlSearchParams: new URLSearchParams({ q: 'search term' }),
23
+ * timeoutMs: 5000
24
+ * });
25
+ *
26
+ * @example
27
+ * // GET request with abort capability
28
+ * const response = await httpGet({
29
+ * url: 'https://api.example.com/large-data',
30
+ * requestHandler: ({ abort }) => {
31
+ * // Store abort function for later use
32
+ * window.abortDataRequest = abort;
33
+ * }
34
+ * });
7
35
  */
36
+ export function httpGet(options: import("./typedef").HttpRequestOptions): Promise<Response>;
8
37
  /**
9
- * Make GET request
10
- *
11
- * @param {object} _
12
- *
13
- * @param {string|URL} _.url - Url string or URL object
14
- *
15
- * @param {object} [_.urlSearchParams]
16
- * Parameters that should be added to the request url
17
- *
18
- * @param {object} [_.headers]
19
- * Object that contains custom headers. A header is a name, value pair.
20
- *
21
- * e.g. options.headers = { "content-type": "application/json" }
22
- *
23
- * @param {boolean} [_.withCredentials=false]
24
- *
25
- * @param {requestHandler} [_.requestHandler]
26
- *
27
- * @param {number} [_.timeoutMs]
28
- * If defined, this request will abort after the specified number of
29
- * milliseconds. Values above the the built-in request timeout won't work.
30
- *
31
- * @returns {Promise<Response>} responsePromise
38
+ * Make a POST request
39
+ *
40
+ * This function performs an HTTP POST request with optional body,
41
+ * headers, credentials, and timeout functionality.
42
+ *
43
+ * @param {import('./typedef').HttpRequestOptions} options
44
+ * Request configuration options
45
+ *
46
+ * @returns {Promise<Response>} Response promise
47
+ *
48
+ * @example
49
+ * // Basic POST request with JSON data
50
+ * const response = await httpPost({
51
+ * url: 'https://api.example.com/users',
52
+ * body: JSON.stringify({ name: 'John Doe', email: 'john@example.com' }),
53
+ * headers: { 'content-type': 'application/json' }
54
+ * });
55
+ *
56
+ * @example
57
+ * // POST request with timeout
58
+ * const response = await httpPost({
59
+ * url: 'https://api.example.com/upload',
60
+ * body: formData,
61
+ * timeoutMs: 30000 // 30 seconds timeout
62
+ * });
32
63
  */
33
- export function httpGet({ url, urlSearchParams, headers, withCredentials, requestHandler, timeoutMs }: {
34
- url: string | URL;
35
- urlSearchParams?: object;
36
- headers?: object;
37
- withCredentials?: boolean;
38
- requestHandler?: requestHandler;
39
- timeoutMs?: number;
40
- }): Promise<Response>;
64
+ export function httpPost(options: import("./typedef").HttpRequestOptions): Promise<Response>;
41
65
  /**
42
- * Make POST request
43
- *
44
- * @param {object} _
45
- *
46
- * @param {string|URL} _.url - Url string or URL object
47
- *
48
- * @param {any} [_.body] - POST data
49
- *
50
- * @param {object} [_.headers]
51
- * Object that contains custom headers. A header is a name, value pair.
52
- *
53
- * e.g. options.headers = { "content-type": "application/json" }
54
- *
55
- * @param {boolean} [_.withCredentials=false]
56
- *
57
- * @param {requestHandler} [_.requestHandler]
58
- *
59
- * @param {number} [_.timeoutMs]
60
- * If defined, this request will abort after the specified number of
61
- * milliseconds. Values above the the built-in request timeout won't work.
62
- *
63
- * @returns {Promise<Response>} responsePromise
66
+ * Make an HTTP request (low-level function)
67
+ *
68
+ * This is a low-level function that powers httpGet and httpPost.
69
+ * It provides complete control over request configuration.
70
+ *
71
+ * @param {import('./typedef').HttpRequestOptions} options
72
+ * Request configuration options
73
+ *
74
+ * @throws {TypeError} If a network error occurred
75
+ * @returns {Promise<Response>} Response promise
76
+ *
77
+ * @example
78
+ * // Custom HTTP request with PUT method
79
+ * const response = await httpRequest({
80
+ * method: 'PUT',
81
+ * url: 'https://api.example.com/resources/123',
82
+ * body: JSON.stringify({ status: 'updated' }),
83
+ * headers: { 'content-type': 'application/json' },
84
+ * withCredentials: true
85
+ * });
86
+ *
87
+ * // Check if response was successful
88
+ * if (response.ok) {
89
+ * // Process response
90
+ * } else {
91
+ * // Handle error based on status
92
+ * }
64
93
  */
65
- export function httpPost({ url, body, headers, withCredentials, requestHandler, timeoutMs }: {
66
- url: string | URL;
67
- body?: any;
68
- headers?: object;
69
- withCredentials?: boolean;
70
- requestHandler?: requestHandler;
71
- timeoutMs?: number;
72
- }): Promise<Response>;
94
+ export function httpRequest(options: import("./typedef").HttpRequestOptions): Promise<Response>;
73
95
  /**
74
- * Make an HTTP request
75
- * - This is a low level function, consider using
76
- * httpGet, httpPost, jsonGet or jsonPost instead
77
- *
78
- * @param {object} _
79
- *
80
- * @param {string|URL} _.url - Url string or URL object
81
- *
82
- * @param {string} _.method - Request method: METHOD_GET | METHOD_POST
83
- *
84
- * @param {object} [_.urlSearchParams] - URL search parameters as key-value pairs
85
- *
86
- * @param {any} [_.body] - POST data
87
- *
88
- * @param {object} [_.headers]
89
- * Object that contains custom headers. A header is a name, value pair.
90
- *
91
- * e.g. options.headers = { "content-type": "application/json" }
92
- *
93
- * @param {boolean} [_.withCredentials=false]
94
- * Whether to include credentials (cookies, HTTP authentication, and client
95
- * SSL certificates) with cross-origin requests. When true, sets fetch
96
- * credentials to 'include', otherwise to 'omit'.
97
- *
98
- * @param {requestHandler} [_.requestHandler]
99
- *
100
- * @param {number} [_.timeoutMs]
101
- * If defined, this request will abort after the specified number of
102
- * milliseconds. Values above the the built-in request timeout won't work.
103
- *
104
- * @throws TypeError - If a network error occurred
96
+ * Default configuration for HTTP requests
105
97
  *
106
- * @note Check the `ok` property of the resolved response to check if the
107
- * response was successfull (e.g. in case of a 404, ok is false)
98
+ * This object contains default settings used by the HTTP request functions.
99
+ * It can be used as a reference for available options and their default values.
108
100
  *
109
- * @returns {Promise<Response>} responsePromise
101
+ * @type {Object}
110
102
  */
111
- export function httpRequest({ method, url, urlSearchParams, body, headers, withCredentials, requestHandler, timeoutMs }: {
112
- url: string | URL;
113
- method: string;
114
- urlSearchParams?: object;
115
- body?: any;
116
- headers?: object;
117
- withCredentials?: boolean;
118
- requestHandler?: requestHandler;
119
- timeoutMs?: number;
120
- }): Promise<Response>;
121
- export type requestHandler = (_: {
122
- controller: AbortController;
123
- abort: (reason?: Error) => void;
124
- timeout: (delayMs: number) => void;
125
- }) => any;
103
+ export const DEFAULT_HTTP_CONFIG: any;