@hkdigital/lib-sveltekit 0.0.58 → 0.0.61

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.
Files changed (59) hide show
  1. package/dist/classes/promise/HkPromise.d.ts +2 -2
  2. package/dist/classes/promise/HkPromise.js +2 -2
  3. package/dist/classes/stores/SubscribersCount.js +1 -1
  4. package/dist/classes/streams/LogTransformStream.js +2 -2
  5. package/dist/components/icon/HkIcon.svelte +1 -1
  6. package/dist/components/layout/HkAppLayout.state.svelte.js +1 -1
  7. package/dist/components/tab-bar/HkTabBar.state.svelte.d.ts +1 -1
  8. package/dist/components/tab-bar/HkTabBar.state.svelte.js +1 -1
  9. package/dist/constants/errors/api.d.ts +10 -0
  10. package/dist/constants/errors/api.js +9 -0
  11. package/dist/constants/errors/generic.d.ts +6 -0
  12. package/dist/constants/errors/generic.js +5 -0
  13. package/dist/constants/errors/index.d.ts +3 -0
  14. package/dist/constants/errors/index.js +3 -0
  15. package/dist/constants/errors/jwt.d.ts +8 -0
  16. package/dist/constants/errors/jwt.js +5 -0
  17. package/dist/constants/http/headers.d.ts +4 -0
  18. package/dist/constants/http/headers.js +5 -0
  19. package/dist/constants/http/index.d.ts +2 -0
  20. package/dist/constants/http/index.js +2 -0
  21. package/dist/constants/http/methods.d.ts +2 -0
  22. package/dist/constants/http/methods.js +2 -0
  23. package/dist/constants/mime/application.d.ts +2 -0
  24. package/dist/constants/mime/application.js +2 -0
  25. package/dist/constants/mime/image.d.ts +3 -0
  26. package/dist/constants/mime/image.js +3 -0
  27. package/dist/constants/mime/index.d.ts +3 -0
  28. package/dist/constants/mime/index.js +3 -0
  29. package/dist/constants/mime/text.d.ts +2 -0
  30. package/dist/constants/mime/text.js +2 -0
  31. package/dist/util/array/index.d.ts +3 -3
  32. package/dist/util/array/index.js +2 -2
  33. package/dist/util/compare/index.d.ts +14 -14
  34. package/dist/util/compare/index.js +14 -14
  35. package/dist/util/expect/index.d.ts +46 -46
  36. package/dist/util/expect/index.js +42 -35
  37. package/dist/util/http/errors.d.ts +18 -0
  38. package/dist/util/http/errors.js +97 -0
  39. package/dist/util/http/headers.d.ts +12 -0
  40. package/dist/util/http/headers.js +39 -0
  41. package/dist/util/http/http-request.d.ts +78 -0
  42. package/dist/util/http/http-request.js +259 -0
  43. package/dist/util/http/index.d.ts +4 -0
  44. package/dist/util/http/index.js +20 -0
  45. package/dist/util/http/json-request.d.ts +47 -0
  46. package/dist/util/http/json-request.js +141 -0
  47. package/dist/util/http/mocks.d.ts +12 -0
  48. package/dist/util/http/mocks.js +12 -0
  49. package/dist/util/http/response.d.ts +24 -0
  50. package/dist/util/http/response.js +105 -0
  51. package/dist/util/http/url.d.ts +8 -0
  52. package/dist/util/http/url.js +19 -0
  53. package/dist/util/is/index.d.ts +12 -12
  54. package/dist/util/is/index.js +7 -7
  55. package/dist/util/object/index.d.ts +16 -16
  56. package/dist/util/object/index.js +11 -11
  57. package/dist/util/singleton/index.d.ts +1 -1
  58. package/dist/util/singleton/index.js +1 -1
  59. package/package.json +33 -16
@@ -32,7 +32,7 @@ const ObjectSchema = v.custom(isObject, `Invalid type: Expected object`);
32
32
  /**
33
33
  * Throws a validation error if value is not a string
34
34
  *
35
- * @param {*} value
35
+ * @param {any} value
36
36
  */
37
37
  export function string(value) {
38
38
  v.parse(v.string(), value);
@@ -41,7 +41,7 @@ export function string(value) {
41
41
  /**
42
42
  * Throws a validation error if value is not a boolean
43
43
  *
44
- * @param {*} value
44
+ * @param {any} value
45
45
  */
46
46
  export function boolean(value) {
47
47
  v.parse(v.boolean(), value);
@@ -50,7 +50,7 @@ export function boolean(value) {
50
50
  /**
51
51
  * Throws a validation error if value is not a number
52
52
  *
53
- * @param {*} value
53
+ * @param {any} value
54
54
  */
55
55
  export function number(value) {
56
56
  v.parse(v.number(), value);
@@ -59,29 +59,36 @@ export function number(value) {
59
59
  /**
60
60
  * Throws a validation error if value is not a Symbol
61
61
  *
62
- * @param {*} value
62
+ * @param {any} value
63
63
  */
64
64
  export function symbol(value) {
65
65
  v.parse(v.symbol(), value);
66
66
  }
67
67
 
68
68
  /**
69
- * Throws a validation error if value is undefined
69
+ * Throws a validation error if value is not defined
70
70
  *
71
- * @param {*} value
71
+ * @param {any} value
72
72
  */
73
- function undefined_(value) {
74
- v.parse(v.undefined(), value);
75
- }
73
+ export function defined(value) {
74
+ v.parse(
75
+ v.custom((value) => {
76
+ if (value === undefined) {
77
+ return false;
78
+ }
76
79
 
77
- export { undefined_ as undefined };
80
+ return true;
81
+ }, 'Invalid type: Expected any value, but received undefined'),
82
+ value
83
+ );
84
+ }
78
85
 
79
86
  // > Base objects
80
87
 
81
88
  /**
82
89
  * Throws a validation error if value is not an object
83
90
  *
84
- * @param {*} value
91
+ * @param {any} value
85
92
  */
86
93
  function object_(value) {
87
94
  v.parse(ObjectSchema, value);
@@ -93,7 +100,7 @@ export { object_ as object };
93
100
  /**
94
101
  * Throws a validation error if value is not an array
95
102
  *
96
- * @param {*} value
103
+ * @param {any} value
97
104
  */
98
105
  function array_(value) {
99
106
  v.parse(v.instance(Array), value);
@@ -104,7 +111,7 @@ export { array_ as array };
104
111
  /**
105
112
  * Throws a validation error if value is not an array of strings
106
113
  *
107
- * @param {*} value
114
+ * @param {any} value
108
115
  */
109
116
  export function stringArray(value) {
110
117
  v.parse(v.array(v.string()), value);
@@ -113,7 +120,7 @@ export function stringArray(value) {
113
120
  /**
114
121
  * Throws a validation error if value is not an array of objects
115
122
  *
116
- * @param {*} value
123
+ * @param {any} value
117
124
  */
118
125
  export function objectArray(value) {
119
126
  v.parse(v.array(v.looseObject({})), value);
@@ -122,22 +129,22 @@ export function objectArray(value) {
122
129
  /**
123
130
  * Throws a validation error if value is not a function
124
131
  *
125
- * @param {*} value
132
+ * @param {any} value
126
133
  */
127
- function _function(value) {
134
+ function function_(value) {
128
135
  v.parse(v.function(), value);
129
136
  }
130
137
 
131
- export { _function as function };
138
+ export { function_ as function };
132
139
 
133
- export { _function as class };
140
+ export { function_ as class };
134
141
 
135
142
  /**
136
143
  * Throws a validation error if value is not a Promise
137
144
  *
138
- * @param {*} value
145
+ * @param {any} value
139
146
  */
140
- export function promise_(value) {
147
+ function promise_(value) {
141
148
  v.parse(v.instance(Promise), value);
142
149
  }
143
150
 
@@ -146,9 +153,9 @@ export { promise_ as promise };
146
153
  /**
147
154
  * Throws a validation error if value is not a Map
148
155
  *
149
- * @param {*} value
156
+ * @param {any} value
150
157
  */
151
- export function map_(value) {
158
+ function map_(value) {
152
159
  v.parse(v.instance(Map), value);
153
160
  }
154
161
 
@@ -157,9 +164,9 @@ export { map_ as map };
157
164
  /**
158
165
  * Throws a validation error if value is not a Set
159
166
  *
160
- * @param {*} value
167
+ * @param {any} value
161
168
  */
162
- export function set_(value) {
169
+ function set_(value) {
163
170
  v.parse(v.instance(Set), value);
164
171
  }
165
172
 
@@ -168,7 +175,7 @@ export { set_ as set };
168
175
  /**
169
176
  * Throws a validation error if value is not an Error instance
170
177
  *
171
- * @param {*} value
178
+ * @param {any} value
172
179
  */
173
180
  export function error_(value) {
174
181
  v.parse(v.instance(Map), value);
@@ -181,7 +188,7 @@ export { error_ as error };
181
188
  /**
182
189
  * Expect a value not to be null
183
190
  *
184
- * @param {*} value
191
+ * @param {any} value
185
192
  */
186
193
  export function notNull(value) {
187
194
  v.notValue(null, value);
@@ -190,7 +197,7 @@ export function notNull(value) {
190
197
  /**
191
198
  * Expect a value to be a boolean and true
192
199
  *
193
- * @param {*} value
200
+ * @param {any} value
194
201
  */
195
202
  export function _true(value) {
196
203
  v.value(true, value);
@@ -211,7 +218,7 @@ export { _true as true };
211
218
  /**
212
219
  * Throws a validation error if value is not a string
213
220
  *
214
- * @param {*} value
221
+ * @param {any} value
215
222
  */
216
223
  export function notEmptyString(value) {
217
224
  const schema = v.pipe(v.string(), v.minLength(1));
@@ -227,7 +234,7 @@ export function notEmptyString(value) {
227
234
  /**
228
235
  * Throws a validation error if value is not iterable
229
236
  *
230
- * @param {*} value
237
+ * @param {any} value
231
238
  */
232
239
  export function iterable(value) {
233
240
  const schema = v.pipe(v.looseObject({ [Symbol.iterator]: v.function() }));
@@ -241,7 +248,7 @@ export function iterable(value) {
241
248
  * Throws a validation error if value is not a a store (has not subscribe
242
249
  * method)
243
250
  *
244
- * @param {*} value
251
+ * @param {any} value
245
252
  */
246
253
  export function store(value) {
247
254
  v.parse(
@@ -262,7 +269,7 @@ export function store(value) {
262
269
  * Throws a validation error if value is not array like
263
270
  * - Checks if the value is an object and has a property `length`
264
271
  *
265
- * @param {*} value
272
+ * @param {any} value
266
273
  */
267
274
  export function arrayLike(value) {
268
275
  v.parse(v.object({ length: v.number() }), value);
@@ -277,7 +284,7 @@ export function arrayLike(value) {
277
284
  * Throws a validation error if value is not an object or the value
278
285
  * is an array
279
286
  *
280
- * @param {*} value
287
+ * @param {any} value
281
288
  */
282
289
  export function objectNoArray(value) {
283
290
  v.parse(
@@ -296,7 +303,7 @@ export function objectNoArray(value) {
296
303
  * Throws a validation error if value is not an object or the value
297
304
  * is a function
298
305
  *
299
- * @param {*} value
306
+ * @param {any} value
300
307
  */
301
308
  export function objectNoFunction(value) {
302
309
  v.parse(
@@ -314,7 +321,7 @@ export function objectNoFunction(value) {
314
321
  /**
315
322
  * Throws a validation error if value is not an object or null
316
323
  *
317
- * @param {*} value
324
+ * @param {any} value
318
325
  */
319
326
  export function objectOrNull(value) {
320
327
  v.parse(v.union([v.value(null), v.looseObject({})]), value);
@@ -326,7 +333,7 @@ export function objectOrNull(value) {
326
333
  /**
327
334
  * Throws a validation error if value is not an array or Set
328
335
  *
329
- * @param {*} value
336
+ * @param {any} value
330
337
  */
331
338
  export function arrayOrSet(value) {
332
339
  v.parse(v.union([v.instance(Array), v.instance(Set)]), value);
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Try to get error information from the server error response
3
+ *
4
+ * This method tries to get error info from JSON responses. The
5
+ * error info may be set as one of the following properties
6
+ *
7
+ * - message
8
+ * - error
9
+ * - messages (array)
10
+ * - errors (array)
11
+ *
12
+ * Otherwise tris method will use the plain text response
13
+ *
14
+ * @param {object} response
15
+ *
16
+ * @returns {Error} error
17
+ */
18
+ export function getErrorFromResponse(response: object): Error;
@@ -0,0 +1,97 @@
1
+ import * as expect from '../expect/index.js';
2
+
3
+ import { CONTENT_TYPE } from '../../constants/http/index.js';
4
+
5
+ import { APPLICATION_JSON } from '../../constants/mime/index.js';
6
+
7
+ /**
8
+ * Try to get error information from the server error response
9
+ *
10
+ * This method tries to get error info from JSON responses. The
11
+ * error info may be set as one of the following properties
12
+ *
13
+ * - message
14
+ * - error
15
+ * - messages (array)
16
+ * - errors (array)
17
+ *
18
+ * Otherwise tris method will use the plain text response
19
+ *
20
+ * @param {object} response
21
+ *
22
+ * @returns {Error} error
23
+ */
24
+ export async function getErrorFromResponse(response) {
25
+ expect.object(response);
26
+
27
+ let message;
28
+ let details = null;
29
+
30
+ const headers = response.headers;
31
+ const contentType = headers.get(CONTENT_TYPE);
32
+
33
+ let content;
34
+
35
+ if (contentType === APPLICATION_JSON) {
36
+ content = await response.json();
37
+
38
+ if (content instanceof Object) {
39
+ if (typeof content.message === 'string') {
40
+ // Use string propery 'message' as error message
41
+ message = content.message;
42
+ } else if (typeof content.error === 'string') {
43
+ // Use string propery 'error' as error message
44
+ message = content.error;
45
+ } else {
46
+ if (Array.isArray(content.errors)) {
47
+ // Use array propery 'errors' for error messages
48
+
49
+ details = content.errors;
50
+ } else if (Array.isArray(content.messages)) {
51
+ // Use array propery 'messages' for error messages
52
+
53
+ details = content.messages;
54
+ }
55
+
56
+ if (details) {
57
+ // Multiple error messages (array) =>
58
+ // create string respresentation by combining
59
+ // text parts from all error messages
60
+ const tmp = [];
61
+
62
+ for (const current of details) {
63
+ if (typeof current === 'string') {
64
+ // Error is a string
65
+ tmp.push(current);
66
+ } else if (current instanceof Object && typeof current.message === 'string') {
67
+ // Error is an object with string property 'message'
68
+ tmp.push(current.message);
69
+ } else {
70
+ // JSON stringify everything else
71
+ tmp.push(JSON.stringify(current));
72
+ }
73
+ } // end for
74
+
75
+ message = tmp.join(', ');
76
+ }
77
+ }
78
+ }
79
+ } else {
80
+ const tmp = await response.text();
81
+
82
+ if (tmp.length) {
83
+ message = tmp;
84
+ } else {
85
+ message = response.statusText;
86
+ }
87
+ }
88
+ // console.log( "message", message );
89
+
90
+ const error = new Error(message);
91
+
92
+ if (details) {
93
+ error.details = details;
94
+ }
95
+
96
+ return error;
97
+ }
@@ -0,0 +1,12 @@
1
+ /**
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
+ *
7
+ * @param {Headers} target - Headers object to set the extra headers in
8
+ *
9
+ * @param {object} [nameValuePairs]
10
+ * Object that contains custom headers. A header is a name, value pair.
11
+ */
12
+ export function setRequestHeaders(target: Headers, nameValuePairs?: object): void;
@@ -0,0 +1,39 @@
1
+ import * as expect from '../expect/index.js';
2
+
3
+ /**
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
+ *
9
+ * @param {Headers} target - Headers object to set the extra headers in
10
+ *
11
+ * @param {object} [nameValuePairs]
12
+ * Object that contains custom headers. A header is a name, value pair.
13
+ */
14
+ export function setRequestHeaders(target, nameValuePairs) {
15
+ if (!(target instanceof Headers)) {
16
+ throw new Error('Invalid parameter [target] (expected Headers object)');
17
+ }
18
+
19
+ expect.objectNoArray(nameValuePairs);
20
+
21
+ if (nameValuePairs instanceof Headers) {
22
+ throw new Error('Invalid parameter [nameValuePairs] (should not be a Headers object)');
23
+ }
24
+
25
+ for (const name in nameValuePairs) {
26
+ expect.notEmptyString(name);
27
+
28
+ const value = nameValuePairs[name];
29
+
30
+ expect.notEmptyString(value);
31
+
32
+ //
33
+ // Headers should be encoded lowercase in HTTP2
34
+ //
35
+ const nameLower = name.toLowerCase();
36
+
37
+ target.set(nameLower, value); /* overwrites existing value */
38
+ }
39
+ }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Make GET request
3
+ *
4
+ * @param {string|URL} url - Url string or URL object
5
+ *
6
+ * @param {object} [urlSearchParams]
7
+ * Parameters that should be added to the request url
8
+ *
9
+ * @param {object} [headers]
10
+ * Object that contains custom headers. A header is a name, value pair.
11
+ *
12
+ * e.g. options.headers = { "content-type": "application/json" }
13
+ *
14
+ * @param {function} [requestHandler]
15
+ * If defined, this function will receive the abort handler function
16
+ *
17
+ * @param {number} [timeoutMs]
18
+ * If defined, this request will abort after the specified number of
19
+ * milliseconds. Values above the the built-in request timeout won't work.
20
+ *
21
+ * @returns {Promise<*>} responsePromise
22
+ */
23
+ export function httpGet({ url, urlSearchParams, headers, requestHandler, timeoutMs }: string | URL): Promise<any>;
24
+ /**
25
+ * Make POST request
26
+ *
27
+ * @param {string|URL} url - Url string or URL object
28
+ *
29
+ * @param {any} [body] - POST data
30
+ *
31
+ * @param {object} [headers]
32
+ * Object that contains custom headers. A header is a name, value pair.
33
+ *
34
+ * e.g. options.headers = { "content-type": "application/json" }
35
+ *
36
+ * @param {function} [requestHandler]
37
+ * If defined, this function will receive the abort handler function
38
+ *
39
+ * @param {number} [timeoutMs]
40
+ * If defined, this request will abort after the specified number of
41
+ * milliseconds. Values above the the built-in request timeout won't work.
42
+ *
43
+ * @returns {Promise<*>} responsePromise
44
+ */
45
+ export function httpPost({ url, body, headers, requestHandler, timeoutMs }: string | URL): Promise<any>;
46
+ /**
47
+ * Make an HTTP request
48
+ * - This is a low level function, consider using
49
+ * httpGet, httpPost, jsonGet or jsonPost instead
50
+ *
51
+ * @param {string|URL} url - Url string or URL object
52
+ *
53
+ * @param {string} method - Request method: METHOD_GET | METHOD_POST
54
+ *
55
+ * @param {object} [urlSearchParams] - URL search parameters as key-value pairs
56
+ *
57
+ * @param {any} [body] - POST data
58
+ *
59
+ * @param {object} [headers]
60
+ * Object that contains custom headers. A header is a name, value pair.
61
+ *
62
+ * e.g. options.headers = { "content-type": "application/json" }
63
+ *
64
+ * @param {function} [requestHandler]
65
+ * If defined, this function will receive the abort handler function
66
+ *
67
+ * @param {number} [timeoutMs]
68
+ * If defined, this request will abort after the specified number of
69
+ * milliseconds. Values above the the built-in request timeout won't work.
70
+ *
71
+ * @throws TypeError - If a network error occurred
72
+ *
73
+ * @note Check the `ok` property of the resolved response to check if the
74
+ * response was successfull (e.g. in case of a 404, ok is false)
75
+ *
76
+ * @returns {Promise<*>} responsePromise
77
+ */
78
+ export function httpRequest({ method, url, urlSearchParams, body, headers, requestHandler, timeoutMs }: string | URL): Promise<any>;