@lightsparkdev/core 1.3.1 → 1.4.0
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 +20 -0
- package/dist/{chunk-23SS5EX2.js → chunk-NV53XYAS.js} +64 -24
- package/dist/{index-C7dqDM91.d.cts → index-DZbfPQqd.d.cts} +5 -5
- package/dist/{index-C7dqDM91.d.ts → index-DZbfPQqd.d.ts} +5 -5
- package/dist/index.cjs +104 -32
- package/dist/index.d.cts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +44 -12
- package/dist/utils/index.cjs +61 -21
- package/dist/utils/index.d.cts +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -1
- package/package.json +3 -2
- package/src/requester/Requester.ts +52 -11
- package/src/requester/tests/Requester.test.ts +347 -0
- package/src/utils/errors.ts +49 -7
- package/src/utils/localStorage.ts +19 -3
- package/src/utils/typeGuards.ts +10 -1
- package/src/utils/types.ts +0 -4
package/src/utils/errors.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isObject } from "./typeGuards.js";
|
|
1
2
|
import { type JSONType } from "./types.js";
|
|
2
3
|
|
|
3
4
|
export const isError = (e: unknown): e is Error => {
|
|
@@ -35,15 +36,35 @@ export const isErrorMsg = (e: unknown, msg: string) => {
|
|
|
35
36
|
return false;
|
|
36
37
|
};
|
|
37
38
|
|
|
38
|
-
|
|
39
|
+
/* Make non-enumerable properties like message and stack enumerable so they
|
|
40
|
+
can be handled by JSON.stringify */
|
|
41
|
+
function normalizeObject(obj: unknown): Record<string, unknown> {
|
|
42
|
+
const normalized: Record<string, unknown> = {};
|
|
43
|
+
if (isObject(obj)) {
|
|
44
|
+
const props = Object.getOwnPropertyNames(obj);
|
|
45
|
+
for (const prop of props) {
|
|
46
|
+
const objRecord = obj as Record<string, unknown>;
|
|
47
|
+
normalized[prop] = objRecord[prop];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return normalized;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function errorToJSON(
|
|
54
|
+
err: unknown,
|
|
55
|
+
/* Enable stringifying non-primitives globally for subpaths.
|
|
56
|
+
Useful for enforcing single level error objects: */
|
|
57
|
+
stringifyObjects = false,
|
|
58
|
+
) {
|
|
39
59
|
if (!err) {
|
|
40
60
|
return null;
|
|
41
61
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
62
|
+
|
|
63
|
+
/* Objects can add standard toJSON method to determine JSON.stringify output, https://mzl.la/3Gks9zu: */
|
|
64
|
+
if (isObject(err) && "toJSON" in err && typeof err.toJSON === "function") {
|
|
65
|
+
if (stringifyObjects === true) {
|
|
66
|
+
return objectToJSON(err.toJSON());
|
|
67
|
+
}
|
|
47
68
|
return err.toJSON() as JSONType;
|
|
48
69
|
}
|
|
49
70
|
|
|
@@ -57,7 +78,28 @@ export function errorToJSON(err: unknown) {
|
|
|
57
78
|
return { message: err.message };
|
|
58
79
|
}
|
|
59
80
|
|
|
81
|
+
return objectToJSON(err);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function objectToJSON(obj: unknown) {
|
|
85
|
+
const normalizedObj = normalizeObject(obj);
|
|
60
86
|
return JSON.parse(
|
|
61
|
-
JSON.stringify(
|
|
87
|
+
JSON.stringify(normalizedObj, (key, value: unknown) => {
|
|
88
|
+
/* Initial call passes the top level object with empty key: */
|
|
89
|
+
if (key === "") {
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const objProps = Object.getOwnPropertyNames(normalizedObj);
|
|
94
|
+
if (!objProps.includes(key)) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (isObject(value)) {
|
|
99
|
+
return JSON.stringify(value);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return value;
|
|
103
|
+
}),
|
|
62
104
|
) as JSONType;
|
|
63
105
|
}
|
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
import { type ConfigKeys } from "../constants/index.js";
|
|
2
2
|
|
|
3
3
|
export function getLocalStorageConfigItem(key: ConfigKeys) {
|
|
4
|
-
|
|
4
|
+
const localStorageBoolean = getLocalStorageBoolean(key);
|
|
5
|
+
// If config not set, just default to false
|
|
6
|
+
if (localStorageBoolean == null) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return localStorageBoolean;
|
|
5
11
|
}
|
|
6
12
|
|
|
7
13
|
export function getLocalStorageBoolean(key: string) {
|
|
8
14
|
/* localStorage is not available in all contexts, use try/catch: */
|
|
9
15
|
try {
|
|
10
|
-
|
|
16
|
+
if (localStorage.getItem(key) === "1") {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
// Key is not set
|
|
20
|
+
else if (localStorage.getItem(key) == null) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
// Key is set but not "1"
|
|
24
|
+
else {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
11
27
|
} catch (e) {
|
|
12
|
-
return
|
|
28
|
+
return null;
|
|
13
29
|
}
|
|
14
30
|
}
|
|
15
31
|
|
package/src/utils/typeGuards.ts
CHANGED
|
@@ -2,7 +2,16 @@ export function isUint8Array(value: unknown): value is Uint8Array {
|
|
|
2
2
|
return value instanceof Uint8Array;
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
-
export function isObject(value: unknown): value is
|
|
5
|
+
export function isObject(value: unknown): value is object {
|
|
6
6
|
const type = typeof value;
|
|
7
7
|
return value != null && (type == "object" || type == "function");
|
|
8
8
|
}
|
|
9
|
+
|
|
10
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
11
|
+
return (
|
|
12
|
+
typeof value === "object" &&
|
|
13
|
+
value !== null &&
|
|
14
|
+
!Array.isArray(value) &&
|
|
15
|
+
Object.prototype.toString.call(value) === "[object Object]"
|
|
16
|
+
);
|
|
17
|
+
}
|
package/src/utils/types.ts
CHANGED
|
@@ -59,7 +59,3 @@ export type Complete<T> = { [P in keyof T]-?: NonNullable<T[P]> };
|
|
|
59
59
|
export type RequiredKeys<T> = {
|
|
60
60
|
[K in keyof T]-?: Record<string, never> extends Pick<T, K> ? never : K;
|
|
61
61
|
}[keyof T];
|
|
62
|
-
|
|
63
|
-
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
64
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
65
|
-
}
|