@vpmedia/simplify 1.74.0 → 1.76.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 +75 -0
- package/dist/const/http_status.d.ts +66 -0
- package/dist/const/http_status.d.ts.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1119 -0
- package/dist/index.js.map +1 -0
- package/dist/logging/AbstractLogHandler.d.ts +17 -0
- package/dist/logging/AbstractLogHandler.d.ts.map +1 -0
- package/dist/logging/ConsoleLogHandler.d.ts +13 -0
- package/dist/logging/ConsoleLogHandler.d.ts.map +1 -0
- package/dist/logging/Logger.d.ts +19 -0
- package/dist/logging/Logger.d.ts.map +1 -0
- package/dist/logging/OpenTelemetryLogHandler.d.ts +15 -0
- package/dist/logging/OpenTelemetryLogHandler.d.ts.map +1 -0
- package/dist/logging/SentryLogHandler.d.ts +13 -0
- package/dist/logging/SentryLogHandler.d.ts.map +1 -0
- package/dist/logging/const.d.ts +14 -0
- package/dist/logging/const.d.ts.map +1 -0
- package/dist/logging/util.d.ts +14 -0
- package/dist/logging/util.d.ts.map +1 -0
- package/dist/pagelifecycle/const.d.ts +15 -0
- package/dist/pagelifecycle/const.d.ts.map +1 -0
- package/dist/pagelifecycle/typedef.d.ts +4 -0
- package/dist/pagelifecycle/typedef.d.ts.map +1 -0
- package/dist/pagelifecycle/util.d.ts +31 -0
- package/dist/pagelifecycle/util.d.ts.map +1 -0
- package/dist/typecheck/TypeCheckError.d.ts +11 -0
- package/dist/typecheck/TypeCheckError.d.ts.map +1 -0
- package/dist/typecheck/TypeChecker.d.ts +27 -0
- package/dist/typecheck/TypeChecker.d.ts.map +1 -0
- package/dist/typecheck/util.d.ts +17 -0
- package/dist/typecheck/util.d.ts.map +1 -0
- package/dist/util/async.d.ts +13 -0
- package/dist/util/async.d.ts.map +1 -0
- package/dist/util/error.d.ts +16 -0
- package/dist/util/error.d.ts.map +1 -0
- package/dist/util/event_emitter.d.ts +42 -0
- package/dist/util/event_emitter.d.ts.map +1 -0
- package/dist/util/fetch.d.ts +22 -0
- package/dist/util/fetch.d.ts.map +1 -0
- package/dist/util/number.d.ts +23 -0
- package/dist/util/number.d.ts.map +1 -0
- package/dist/util/object.d.ts +24 -0
- package/dist/util/object.d.ts.map +1 -0
- package/dist/util/query.d.ts +11 -0
- package/dist/util/query.d.ts.map +1 -0
- package/dist/util/state.d.ts +5 -0
- package/dist/util/state.d.ts.map +1 -0
- package/dist/util/string.d.ts +25 -0
- package/dist/util/string.d.ts.map +1 -0
- package/dist/util/uuid.d.ts +13 -0
- package/dist/util/uuid.d.ts.map +1 -0
- package/dist/util/validate.d.ts +106 -0
- package/dist/util/validate.d.ts.map +1 -0
- package/package.json +32 -16
- package/src/const/http_status.test.ts +7 -0
- package/src/const/{http_status.js → http_status.ts} +1 -1
- package/src/{index.js → index.ts} +8 -0
- package/src/logging/AbstractLogHandler.ts +31 -0
- package/src/logging/{ConsoleLogHandler.js → ConsoleLogHandler.ts} +15 -13
- package/src/logging/Logger.test.ts +69 -0
- package/src/logging/Logger.ts +77 -0
- package/src/logging/OpenTelemetryLogHandler.ts +40 -0
- package/src/logging/SentryLogHandler.ts +44 -0
- package/src/logging/{const.js → const.ts} +1 -1
- package/src/logging/util.test.ts +33 -0
- package/src/logging/util.ts +36 -0
- package/src/pagelifecycle/{const.js → const.ts} +2 -2
- package/src/pagelifecycle/typedef.ts +5 -0
- package/src/pagelifecycle/util.test.ts +99 -0
- package/src/pagelifecycle/{util.js → util.ts} +14 -27
- package/src/typecheck/{TypeCheckError.js → TypeCheckError.ts} +7 -3
- package/src/typecheck/TypeChecker.test.ts +70 -0
- package/src/typecheck/{TypeChecker.js → TypeChecker.ts} +10 -27
- package/src/typecheck/util.test.ts +36 -0
- package/src/typecheck/util.ts +50 -0
- package/src/util/async.test.ts +74 -0
- package/src/util/{async.js → async.ts} +3 -12
- package/src/util/error.test.ts +32 -0
- package/src/util/error.ts +37 -0
- package/src/util/event_emitter.test.ts +228 -0
- package/src/util/event_emitter.ts +147 -0
- package/src/util/fetch.test.ts +62 -0
- package/src/util/{fetch.js → fetch.ts} +40 -31
- package/src/util/number.test.ts +124 -0
- package/src/util/number.ts +58 -0
- package/src/util/object.test.ts +203 -0
- package/src/util/{object.js → object.ts} +14 -21
- package/src/util/query.test.ts +71 -0
- package/src/util/query.ts +35 -0
- package/src/util/state.test.ts +47 -0
- package/src/util/{state.js → state.ts} +3 -6
- package/src/util/string.test.ts +64 -0
- package/src/util/string.ts +65 -0
- package/src/util/uuid.test.ts +53 -0
- package/src/util/uuid.ts +31 -0
- package/src/util/validate.test.ts +309 -0
- package/src/util/validate.ts +230 -0
- package/.vscode/extensions.json +0 -6
- package/.vscode/settings.json +0 -27
- package/src/logging/AbstractLogHandler.js +0 -23
- package/src/logging/Logger.js +0 -115
- package/src/logging/OpenTelemetryLogHandler.js +0 -30
- package/src/logging/SentryLogHandler.js +0 -46
- package/src/logging/util.js +0 -41
- package/src/pagelifecycle/typedef.js +0 -9
- package/src/typecheck/util.js +0 -60
- package/src/util/error.js +0 -33
- package/src/util/event_emitter.js +0 -196
- package/src/util/number.js +0 -118
- package/src/util/query.js +0 -32
- package/src/util/string.js +0 -76
- package/src/util/uuid.js +0 -35
- package/src/util/validate.js +0 -247
- package/types/const/http_status.d.ts +0 -131
- package/types/const/http_status.d.ts.map +0 -1
- package/types/index.d.ts +0 -26
- package/types/index.d.ts.map +0 -1
- package/types/logging/AbstractLogHandler.d.ts +0 -20
- package/types/logging/AbstractLogHandler.d.ts.map +0 -1
- package/types/logging/ConsoleLogHandler.d.ts +0 -9
- package/types/logging/ConsoleLogHandler.d.ts.map +0 -1
- package/types/logging/Logger.d.ts +0 -66
- package/types/logging/Logger.d.ts.map +0 -1
- package/types/logging/OpenTelemetryLogHandler.d.ts +0 -11
- package/types/logging/OpenTelemetryLogHandler.d.ts.map +0 -1
- package/types/logging/SentryLogHandler.d.ts +0 -9
- package/types/logging/SentryLogHandler.d.ts.map +0 -1
- package/types/logging/const.d.ts +0 -14
- package/types/logging/const.d.ts.map +0 -1
- package/types/logging/util.d.ts +0 -4
- package/types/logging/util.d.ts.map +0 -1
- package/types/pagelifecycle/const.d.ts +0 -15
- package/types/pagelifecycle/const.d.ts.map +0 -1
- package/types/pagelifecycle/typedef.d.ts +0 -4
- package/types/pagelifecycle/typedef.d.ts.map +0 -1
- package/types/pagelifecycle/util.d.ts +0 -8
- package/types/pagelifecycle/util.d.ts.map +0 -1
- package/types/typecheck/TypeCheckError.d.ts +0 -13
- package/types/typecheck/TypeCheckError.d.ts.map +0 -1
- package/types/typecheck/TypeChecker.d.ts +0 -40
- package/types/typecheck/TypeChecker.d.ts.map +0 -1
- package/types/typecheck/util.d.ts +0 -4
- package/types/typecheck/util.d.ts.map +0 -1
- package/types/util/async.d.ts +0 -4
- package/types/util/async.d.ts.map +0 -1
- package/types/util/error.d.ts +0 -3
- package/types/util/error.d.ts.map +0 -1
- package/types/util/event_emitter.d.ts +0 -69
- package/types/util/event_emitter.d.ts.map +0 -1
- package/types/util/fetch.d.ts +0 -22
- package/types/util/fetch.d.ts.map +0 -1
- package/types/util/number.d.ts +0 -11
- package/types/util/number.d.ts.map +0 -1
- package/types/util/object.d.ts +0 -6
- package/types/util/object.d.ts.map +0 -1
- package/types/util/query.d.ts +0 -3
- package/types/util/query.d.ts.map +0 -1
- package/types/util/state.d.ts +0 -2
- package/types/util/state.d.ts.map +0 -1
- package/types/util/string.d.ts +0 -7
- package/types/util/string.d.ts.map +0 -1
- package/types/util/uuid.d.ts +0 -4
- package/types/util/uuid.d.ts.map +0 -1
- package/types/util/validate.d.ts +0 -45
- package/types/util/validate.d.ts.map +0 -1
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { isEqual, isGreater, isGreaterOrEqual, isInRange, isLess, isLessOrEqual } from './number.js';
|
|
2
|
+
|
|
3
|
+
export type Validator<T> = (value: unknown) => value is T;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validates `value` as `boolean`.
|
|
7
|
+
*/
|
|
8
|
+
export const isBoolean = (value: unknown): value is boolean => typeof value === 'boolean';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Validates `value` as `number`.
|
|
12
|
+
*/
|
|
13
|
+
export const isNumber = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Validates `value` as positive `number`.
|
|
17
|
+
*/
|
|
18
|
+
export const isPositiveNumber = (value: unknown): value is number => isNumber(value) && value > 0;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Validates `value` as non-negative `number`.
|
|
22
|
+
*/
|
|
23
|
+
export const isNonNegativeNumber = (value: unknown): value is number => isNumber(value) && value >= 0;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Validates `value` as `integer`.
|
|
27
|
+
*/
|
|
28
|
+
export const isInteger = (value: unknown): value is number => isNumber(value) && Number.isInteger(value);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Validates `value` as positive `integer`.
|
|
32
|
+
*/
|
|
33
|
+
export const isPositiveInteger = (value: unknown): value is number => isInteger(value) && value > 0;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Validates `value` as non-negative `integer`.
|
|
37
|
+
*/
|
|
38
|
+
export const isNonNegativeInteger = (value: unknown): value is number => isInteger(value) && value >= 0;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Validates `value` as `string`.
|
|
42
|
+
*/
|
|
43
|
+
export const isString = (value: unknown): value is string => typeof value === 'string';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Validates `value` as `array`.
|
|
47
|
+
*/
|
|
48
|
+
export const isArray = <T = unknown>(value: unknown): value is T[] => Array.isArray(value);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Validates `value` as `null`.
|
|
52
|
+
*/
|
|
53
|
+
export const isNull = (value: unknown): value is null => value === null;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Validates `value` as `undefined`.
|
|
57
|
+
*/
|
|
58
|
+
export const isUndefined = (value: unknown): value is undefined => value === undefined;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Validates `value` as `null` or `undefined`.
|
|
62
|
+
*/
|
|
63
|
+
export const isNullOrUndefined = (value: unknown): value is null | undefined => isNull(value) || isUndefined(value);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Validates `value` as plain `object`.
|
|
67
|
+
*/
|
|
68
|
+
export const isPlainObject = (value: unknown): value is Record<string, unknown> =>
|
|
69
|
+
Object.prototype.toString.call(value) === '[object Object]';
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Validates `value` as `function`.
|
|
73
|
+
*/
|
|
74
|
+
export const isFunction = (value: unknown): value is (...args: any[]) => any => typeof value === 'function';
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Validates `value` as `type`.
|
|
78
|
+
*/
|
|
79
|
+
export const isInstance = <T>(value: unknown, type: new (...args: any[]) => T): value is T =>
|
|
80
|
+
isFunction(type) && value instanceof type;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Validates `value` as `enum`.
|
|
84
|
+
*/
|
|
85
|
+
export const isEnum = (
|
|
86
|
+
value: unknown,
|
|
87
|
+
choices:
|
|
88
|
+
| ReadonlyArray<string | number>
|
|
89
|
+
| ReadonlySet<string | number>
|
|
90
|
+
| Readonly<Record<string | number, string | number>>
|
|
91
|
+
| null
|
|
92
|
+
| undefined
|
|
93
|
+
): boolean => {
|
|
94
|
+
if (!isString(value) && !isNumber(value)) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return (
|
|
98
|
+
(isArray<string | number>(choices) && choices.includes(value)) ||
|
|
99
|
+
(isPlainObject(choices) && Object.values(choices).includes(value)) ||
|
|
100
|
+
(isInstance(choices, Set) && (choices as Set<string | number>).has(value))
|
|
101
|
+
);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Type check an array of values using a validator.
|
|
106
|
+
*/
|
|
107
|
+
export const isArrayOf = <T>(values: unknown, validator: Validator<T>): values is T[] => {
|
|
108
|
+
if (!isArray(values)) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
for (const value of values) {
|
|
112
|
+
if (!validator(value)) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return true;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Type check a plain object of values using a validator.
|
|
121
|
+
*/
|
|
122
|
+
export const isPlainObjectOf = <T>(
|
|
123
|
+
record: unknown,
|
|
124
|
+
validator: Validator<T>
|
|
125
|
+
): record is Record<string | number, T> => {
|
|
126
|
+
if (!isPlainObject(record)) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
for (const value of Object.values(record)) {
|
|
130
|
+
if (!validator(value)) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Refine a base validator with an extra condition.
|
|
139
|
+
*/
|
|
140
|
+
export const refineValidator = <T>(
|
|
141
|
+
base: Validator<T>,
|
|
142
|
+
predicate: (value: T) => boolean,
|
|
143
|
+
name: string | null = null
|
|
144
|
+
): Validator<T> => {
|
|
145
|
+
const refinedValidator = (value: unknown): value is T => base(value) && predicate(value as T);
|
|
146
|
+
Object.defineProperty(refinedValidator, 'name', {
|
|
147
|
+
value: isString(name) ? name : `${base.name}Refined`,
|
|
148
|
+
});
|
|
149
|
+
return refinedValidator;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Logical OR of two validators.
|
|
154
|
+
*/
|
|
155
|
+
export const isAnyOf =
|
|
156
|
+
<A, B>(a: Validator<A>, b: Validator<B>): Validator<A | B> =>
|
|
157
|
+
(value: unknown): value is A | B =>
|
|
158
|
+
a(value) || b(value);
|
|
159
|
+
|
|
160
|
+
export const isNumberGreater = (min: number): Validator<number> =>
|
|
161
|
+
refineValidator(isNumber, (value) => isGreater(value, min));
|
|
162
|
+
|
|
163
|
+
export const isNumberGreaterOrEqual = (min: number): Validator<number> =>
|
|
164
|
+
refineValidator(isNumber, (value) => isGreaterOrEqual(value, min));
|
|
165
|
+
|
|
166
|
+
export const isNumberLess = (min: number): Validator<number> =>
|
|
167
|
+
refineValidator(isNumber, (value) => isLess(value, min));
|
|
168
|
+
|
|
169
|
+
export const isNumberLessOrEqual = (min: number): Validator<number> =>
|
|
170
|
+
refineValidator(isNumber, (value) => isLessOrEqual(value, min));
|
|
171
|
+
|
|
172
|
+
export const isNumberInRange = (min: number, max: number): Validator<number> =>
|
|
173
|
+
refineValidator(isNumber, (value) => isInRange(value, min, max));
|
|
174
|
+
|
|
175
|
+
export const isNumberEqual = (expected: number): Validator<number> =>
|
|
176
|
+
refineValidator(isNumber, (value) => isEqual(value, expected));
|
|
177
|
+
|
|
178
|
+
export const isIntegerGreater = (min: number): Validator<number> =>
|
|
179
|
+
refineValidator(isInteger, (value) => isGreater(value, min));
|
|
180
|
+
|
|
181
|
+
export const isIntegerGreaterOrEqual = (min: number): Validator<number> =>
|
|
182
|
+
refineValidator(isInteger, (value) => isGreaterOrEqual(value, min));
|
|
183
|
+
|
|
184
|
+
export const isIntegerLess = (min: number): Validator<number> =>
|
|
185
|
+
refineValidator(isInteger, (value) => isLess(value, min));
|
|
186
|
+
|
|
187
|
+
export const isIntegerLessOrEqual = (min: number): Validator<number> =>
|
|
188
|
+
refineValidator(isInteger, (value) => isLessOrEqual(value, min));
|
|
189
|
+
|
|
190
|
+
export const isIntegerInRange = (min: number, max: number): Validator<number> =>
|
|
191
|
+
refineValidator(isInteger, (value) => isInRange(value, min, max));
|
|
192
|
+
|
|
193
|
+
export const isIntegerEqual = (expected: number): Validator<number> =>
|
|
194
|
+
refineValidator(isInteger, (value) => isEqual(value, expected));
|
|
195
|
+
|
|
196
|
+
export const isStringLengthGreater = (min: number): Validator<string> =>
|
|
197
|
+
refineValidator(isString, (value) => isGreater(value.length, min));
|
|
198
|
+
|
|
199
|
+
export const isStringLengthGreaterOrEqual = (min: number): Validator<string> =>
|
|
200
|
+
refineValidator(isString, (value) => isGreaterOrEqual(value.length, min));
|
|
201
|
+
|
|
202
|
+
export const isStringLengthLess = (min: number): Validator<string> =>
|
|
203
|
+
refineValidator(isString, (value) => isLess(value.length, min));
|
|
204
|
+
|
|
205
|
+
export const isStringLengthLessOrEqual = (min: number): Validator<string> =>
|
|
206
|
+
refineValidator(isString, (value) => isLessOrEqual(value.length, min));
|
|
207
|
+
|
|
208
|
+
export const isStringLengthInRange = (min: number, max: number): Validator<string> =>
|
|
209
|
+
refineValidator(isString, (value) => isInRange(value.length, min, max));
|
|
210
|
+
|
|
211
|
+
export const isStringLengthEqual = (expected: number): Validator<string> =>
|
|
212
|
+
refineValidator(isString, (value) => isEqual(value.length, expected));
|
|
213
|
+
|
|
214
|
+
export const isArrayLengthGreater = (min: number): Validator<unknown[]> =>
|
|
215
|
+
refineValidator(isArray, (value) => isGreater(value.length, min));
|
|
216
|
+
|
|
217
|
+
export const isArrayLengthGreaterOrEqual = (min: number): Validator<unknown[]> =>
|
|
218
|
+
refineValidator(isArray, (value) => isGreaterOrEqual(value.length, min));
|
|
219
|
+
|
|
220
|
+
export const isArrayLengthLess = (min: number): Validator<unknown[]> =>
|
|
221
|
+
refineValidator(isArray, (value) => isLess(value.length, min));
|
|
222
|
+
|
|
223
|
+
export const isArrayLengthLessOrEqual = (min: number): Validator<unknown[]> =>
|
|
224
|
+
refineValidator(isArray, (value) => isLessOrEqual(value.length, min));
|
|
225
|
+
|
|
226
|
+
export const isArrayLengthInRange = (min: number, max: number): Validator<unknown[]> =>
|
|
227
|
+
refineValidator(isArray, (value) => isInRange(value.length, min, max));
|
|
228
|
+
|
|
229
|
+
export const isArrayLengthEqual = (expected: number): Validator<unknown[]> =>
|
|
230
|
+
refineValidator(isArray, (value) => isEqual(value.length, expected));
|
package/.vscode/extensions.json
DELETED
package/.vscode/settings.json
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"files.watcherExclude": {
|
|
3
|
-
"**/.git/objects/**": true,
|
|
4
|
-
"**/.git/subtree-cache/**": true,
|
|
5
|
-
"**/build/**": true,
|
|
6
|
-
"**/node_modules/**": true
|
|
7
|
-
},
|
|
8
|
-
"editor.codeActionsOnSave": {
|
|
9
|
-
"source.removeUnusedImports": "explicit",
|
|
10
|
-
"source.organizeImports": "explicit"
|
|
11
|
-
},
|
|
12
|
-
"oxc.enable": true,
|
|
13
|
-
"[javascript]": {
|
|
14
|
-
"editor.defaultFormatter": "oxc.oxc-vscode"
|
|
15
|
-
},
|
|
16
|
-
"[javascriptreact]": {
|
|
17
|
-
"editor.defaultFormatter": "oxc.oxc-vscode"
|
|
18
|
-
},
|
|
19
|
-
"javascript.updateImportsOnFileMove.enabled": "always",
|
|
20
|
-
"[typescript]": {
|
|
21
|
-
"editor.defaultFormatter": "oxc.oxc-vscode",
|
|
22
|
-
},
|
|
23
|
-
"[typescriptreact]": {
|
|
24
|
-
"editor.defaultFormatter": "oxc.oxc-vscode"
|
|
25
|
-
},
|
|
26
|
-
"typescript.updateImportsOnFileMove.enabled": "always",
|
|
27
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
export class AbstractLogHandler {
|
|
2
|
-
/**
|
|
3
|
-
* Abstract log handler.
|
|
4
|
-
* @param {number} level - Log handler level.
|
|
5
|
-
*/
|
|
6
|
-
constructor(level) {
|
|
7
|
-
this.level = level;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Emit log record.
|
|
12
|
-
* @param {import('./Logger.js').Logger} logger - Logger instance.
|
|
13
|
-
* @param {number} timestamp - Log timestamp.
|
|
14
|
-
* @param {number} level - Log level.
|
|
15
|
-
* @param {string} message - Log message.
|
|
16
|
-
* @param {object | null | undefined} extra - Log extra data.
|
|
17
|
-
* @param {Error | null | undefined} error - Log error.
|
|
18
|
-
* @throws {Error}
|
|
19
|
-
*/
|
|
20
|
-
emit(logger, timestamp, level, message, extra, error) {
|
|
21
|
-
throw new Error('Not implemented.');
|
|
22
|
-
}
|
|
23
|
-
}
|
package/src/logging/Logger.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { getURLParam } from '../util/query.js';
|
|
2
|
-
import {
|
|
3
|
-
LOG_LEVEL_DEBUG,
|
|
4
|
-
LOG_LEVEL_ERROR,
|
|
5
|
-
LOG_LEVEL_FATAL,
|
|
6
|
-
LOG_LEVEL_INFO,
|
|
7
|
-
LOG_LEVEL_SILENT,
|
|
8
|
-
LOG_LEVEL_WARNING,
|
|
9
|
-
} from './const.js';
|
|
10
|
-
import { getAppEnvironment } from './util.js';
|
|
11
|
-
|
|
12
|
-
const ROOT_LOGGER_NAME = 'root';
|
|
13
|
-
|
|
14
|
-
export class Logger {
|
|
15
|
-
/**
|
|
16
|
-
* @type {import('./AbstractLogHandler.js').AbstractLogHandler[]}
|
|
17
|
-
*/
|
|
18
|
-
static handlers = [];
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Creates a new Logger instance.
|
|
22
|
-
* @param {string} [name] - The logger name.
|
|
23
|
-
*/
|
|
24
|
-
constructor(name = ROOT_LOGGER_NAME) {
|
|
25
|
-
this.name = name ?? ROOT_LOGGER_NAME;
|
|
26
|
-
const appEnvironment = getAppEnvironment();
|
|
27
|
-
const isProduction = appEnvironment === 'production' || appEnvironment === 'release';
|
|
28
|
-
const defaultLevel = isProduction ? LOG_LEVEL_SILENT : LOG_LEVEL_DEBUG;
|
|
29
|
-
const parameterName = `log_${this.name.toLowerCase()}`;
|
|
30
|
-
const paramLevel =
|
|
31
|
-
getURLParam(parameterName, getURLParam('log_all', defaultLevel.toString())) ?? defaultLevel.toString();
|
|
32
|
-
/** @type {number} */
|
|
33
|
-
this.level = Number.parseInt(paramLevel, 10);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Add log handler.
|
|
38
|
-
* @param {import('./AbstractLogHandler.js').AbstractLogHandler} handler - Log handler.
|
|
39
|
-
*/
|
|
40
|
-
static addHandler = (handler) => {
|
|
41
|
-
Logger.handlers.push(handler);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Emit log record.
|
|
46
|
-
* @param {Logger} logger - Logger instance.
|
|
47
|
-
* @param {number} level - Log level.
|
|
48
|
-
* @param {string} message - Log message.
|
|
49
|
-
* @param {object} extra - Log extra data.
|
|
50
|
-
* @param {Error | null | undefined} [error] - Log exception.
|
|
51
|
-
*/
|
|
52
|
-
static emit = (logger, level, message, extra, error) => {
|
|
53
|
-
const timestamp = Date.now();
|
|
54
|
-
for (const handler of Logger.handlers) {
|
|
55
|
-
if (handler.level >= level) {
|
|
56
|
-
handler.emit(logger, timestamp, level, message, extra, error);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Emit debug log.
|
|
63
|
-
* @param {string} message - Log message.
|
|
64
|
-
* @param {object | null | undefined} [extra] - Log extra data.
|
|
65
|
-
*/
|
|
66
|
-
debug(message, extra) {
|
|
67
|
-
Logger.emit(this, LOG_LEVEL_DEBUG, message, extra);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Emit info log.
|
|
72
|
-
* @param {string} message - Log message.
|
|
73
|
-
* @param {object | null | undefined} [extra] - Log extra data.
|
|
74
|
-
*/
|
|
75
|
-
info(message, extra) {
|
|
76
|
-
Logger.emit(this, LOG_LEVEL_INFO, message, extra);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Emit warning log.
|
|
81
|
-
* @param {string} message - Log message.
|
|
82
|
-
* @param {object | null | undefined} [extra] - Log extra data.
|
|
83
|
-
*/
|
|
84
|
-
warn(message, extra) {
|
|
85
|
-
Logger.emit(this, LOG_LEVEL_WARNING, message, extra);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Emit warning log.
|
|
90
|
-
* @param {string} message - Log message.
|
|
91
|
-
* @param {object | null | undefined} [extra] - Log extra data.
|
|
92
|
-
*/
|
|
93
|
-
warning(message, extra) {
|
|
94
|
-
Logger.emit(this, LOG_LEVEL_WARNING, message, extra);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Emit error log.
|
|
99
|
-
* @param {string} message - Log message.
|
|
100
|
-
* @param {object | null | undefined} [extra] - Log extra data.
|
|
101
|
-
*/
|
|
102
|
-
error(message, extra) {
|
|
103
|
-
Logger.emit(this, LOG_LEVEL_ERROR, message, extra);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Emit exception log.
|
|
108
|
-
* @param {string} message - Log message.
|
|
109
|
-
* @param {Error} error - Log error.
|
|
110
|
-
* @param {object | null | undefined} [extra] - Log extra data.
|
|
111
|
-
*/
|
|
112
|
-
exception(message, error, extra) {
|
|
113
|
-
Logger.emit(this, LOG_LEVEL_FATAL, message, extra, error);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { AbstractLogHandler } from './AbstractLogHandler.js';
|
|
2
|
-
|
|
3
|
-
export class OpenTelemetryLogHandler extends AbstractLogHandler {
|
|
4
|
-
/**
|
|
5
|
-
* Open Telemetry log handler.
|
|
6
|
-
* @param {number} level - Log handler level.
|
|
7
|
-
* @param {(logger: import('./Logger.js').Logger, timestamp: number, level: number, message: string, extra: object | null | undefined, error: Error | null | undefined) => void} emitter - Log handler emitter.
|
|
8
|
-
*/
|
|
9
|
-
constructor(level, emitter) {
|
|
10
|
-
super(level);
|
|
11
|
-
this.emitter = emitter;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Emit log record.
|
|
16
|
-
* @param {import('./Logger.js').Logger} logger - Logger instance.
|
|
17
|
-
* @param {number} timestamp - Log timestamp.
|
|
18
|
-
* @param {number} level - Log level.
|
|
19
|
-
* @param {string} message - Log message.
|
|
20
|
-
* @param {object | null | undefined} extra - Log extra data.
|
|
21
|
-
* @param {Error | null | undefined} error - Log error.
|
|
22
|
-
* @throws {Error}
|
|
23
|
-
*/
|
|
24
|
-
emit(logger, timestamp, level, message, extra, error) {
|
|
25
|
-
if (!this.emitter) {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
this.emitter(logger, timestamp, level, message, extra, error);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { addBreadcrumb, captureException, captureMessage } from '@sentry/browser';
|
|
2
|
-
import { AbstractLogHandler } from './AbstractLogHandler.js';
|
|
3
|
-
import { LOG_LEVEL_DEBUG, LOG_LEVEL_WARNING } from './const.js';
|
|
4
|
-
import { getLogLevelName } from './util.js';
|
|
5
|
-
|
|
6
|
-
export class SentryLogHandler extends AbstractLogHandler {
|
|
7
|
-
/**
|
|
8
|
-
* Console log handler.
|
|
9
|
-
* @param {number} level - Log handler level.
|
|
10
|
-
*/
|
|
11
|
-
constructor(level = LOG_LEVEL_DEBUG) {
|
|
12
|
-
super(level);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Emit log record.
|
|
17
|
-
* @param {import('./Logger.js').Logger} logger - Logger instance.
|
|
18
|
-
* @param {number} timestamp - Log timestamp.
|
|
19
|
-
* @param {number} level - Log level.
|
|
20
|
-
* @param {string} message - Log message.
|
|
21
|
-
* @param {object | null | undefined} extra - Log extra data.
|
|
22
|
-
* @param {Error | null | undefined} error - Log error.
|
|
23
|
-
* @throws {Error}
|
|
24
|
-
*/
|
|
25
|
-
emit(logger, timestamp, level, message, extra, error) {
|
|
26
|
-
/** @type {import('@sentry/browser').SeverityLevel} */
|
|
27
|
-
// @ts-expect-error
|
|
28
|
-
const levelName = getLogLevelName(level);
|
|
29
|
-
const logMessage = `[${logger.name}] ${message}`;
|
|
30
|
-
const breadcrumb = {
|
|
31
|
-
type: 'default',
|
|
32
|
-
category: 'console',
|
|
33
|
-
message: logMessage,
|
|
34
|
-
level: levelName,
|
|
35
|
-
data: extra,
|
|
36
|
-
};
|
|
37
|
-
addBreadcrumb(breadcrumb);
|
|
38
|
-
if (error) {
|
|
39
|
-
extra?.tags ? captureException(error, { tags: extra.tags }) : captureException(error);
|
|
40
|
-
} else if (level === LOG_LEVEL_WARNING) {
|
|
41
|
-
extra?.tags
|
|
42
|
-
? captureMessage(logMessage, { level: 'warning', tags: extra.tags })
|
|
43
|
-
: captureMessage(logMessage, { level: 'warning' });
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
package/src/logging/util.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { LOG_LEVEL_NAMES } from './const.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Format log message.
|
|
5
|
-
* @param {import('./Logger.js').Logger} logger - Logger target name.
|
|
6
|
-
* @param {number} timestamp - Log timestamp.
|
|
7
|
-
* @param {number} level - Log level.
|
|
8
|
-
* @param {string} message - Log message.
|
|
9
|
-
* @returns {string} Formatted log message.
|
|
10
|
-
*/
|
|
11
|
-
export const formatLogMessage = (logger, timestamp, level, message) => `${timestamp} [${logger.name}] ${message}`;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Get log level name.
|
|
15
|
-
* @param {number} level - Log level.
|
|
16
|
-
* @returns {string} Log level name.
|
|
17
|
-
*/
|
|
18
|
-
export const getLogLevelName = (level) => LOG_LEVEL_NAMES[level];
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Returns the application environment identifier.
|
|
22
|
-
* @returns {string} App environment type.
|
|
23
|
-
*/
|
|
24
|
-
export const getAppEnvironment = () => {
|
|
25
|
-
let appEnvironment = 'local';
|
|
26
|
-
try {
|
|
27
|
-
if (import.meta['env'].VITE_APP_ENVIRONMENT) {
|
|
28
|
-
appEnvironment = import.meta['env'].VITE_APP_ENVIRONMENT;
|
|
29
|
-
}
|
|
30
|
-
} catch {
|
|
31
|
-
// pass
|
|
32
|
-
}
|
|
33
|
-
try {
|
|
34
|
-
if (process.env.APP_ENVIRONMENT) {
|
|
35
|
-
appEnvironment = process.env.APP_ENVIRONMENT;
|
|
36
|
-
}
|
|
37
|
-
} catch {
|
|
38
|
-
// pass
|
|
39
|
-
}
|
|
40
|
-
return appEnvironment;
|
|
41
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export const TYPEDEF = true;
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @typedef {import('./const.js').PAGE_LIFECYCLE_STATE_HIDDEN | import('./const.js').PAGE_LIFECYCLE_STATE_ACTIVE | import('./const.js').PAGE_LIFECYCLE_STATE_PASSIVE | import('./const.js').PAGE_LIFECYCLE_STATE_FROZEN | import('./const.js').PAGE_LIFECYCLE_STATE_TERMINATED} PageLifecycleState
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @typedef {import('./const.js').DOCUMENT_STATE_LOADING | import('./const.js').DOCUMENT_STATE_INTERACTIVE | import('./const.js').DOCUMENT_STATE_COMPLETE | import('./const.js').DOCUMENT_STATE_DOM_LOADED | import('./const.js').DOCUMENT_STATE_FULLY_LOADED} DocumentState
|
|
9
|
-
*/
|
package/src/typecheck/util.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { getDisplayValue, getTypeFromValue } from '../util/string.js';
|
|
2
|
-
import { isArrayOf, isEnum } from '../util/validate.js';
|
|
3
|
-
import { TypeCheckError } from './TypeCheckError.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Get error message for validator exceptions.
|
|
7
|
-
* @param {string} validatorName - Validator name.
|
|
8
|
-
* @param {unknown} value - Input value.
|
|
9
|
-
*/
|
|
10
|
-
const getErrorMessage = (validatorName, value) => {
|
|
11
|
-
const displayValue = getDisplayValue(value);
|
|
12
|
-
const displayType = getTypeFromValue(value);
|
|
13
|
-
throw new TypeCheckError(`Validation failed: ${validatorName || '<anonymous>'} - ${displayValue} (${displayType})`);
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Type check a value using a validator.
|
|
17
|
-
* @template T
|
|
18
|
-
* @param {unknown} value - The value to check.
|
|
19
|
-
* @param {(value: unknown) => value is T} validator - The validator to check with.
|
|
20
|
-
* @returns {T} The type checked value.
|
|
21
|
-
* @throws {TypeCheckError}
|
|
22
|
-
*/
|
|
23
|
-
export const typeCheck = (value, validator) => {
|
|
24
|
-
if (!validator(value)) {
|
|
25
|
-
const errorMessage = getErrorMessage(validator.name, value);
|
|
26
|
-
throw new TypeCheckError(errorMessage);
|
|
27
|
-
}
|
|
28
|
-
return value;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Type check a value using a validator.
|
|
33
|
-
* @template T
|
|
34
|
-
* @param {unknown[]} value - The value to check.
|
|
35
|
-
* @param {(value: unknown) => value is T} validator - The validator to check the array with.
|
|
36
|
-
* @returns {T[]} The type checked value.
|
|
37
|
-
* @throws {TypeCheckError}
|
|
38
|
-
*/
|
|
39
|
-
export const typeCheckArray = (value, validator) => {
|
|
40
|
-
if (!isArrayOf(value, validator)) {
|
|
41
|
-
const errorMessage = getErrorMessage(validator.name, value);
|
|
42
|
-
throw new TypeCheckError(errorMessage);
|
|
43
|
-
}
|
|
44
|
-
return value;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Type check an enum.
|
|
49
|
-
* @param {string | number} value - The value to check.
|
|
50
|
-
* @param {(string | number)[] | Set<string | number> | Record<string | number, string | number>} choices - Enum list.
|
|
51
|
-
* @returns {string | number} The type checked value.
|
|
52
|
-
* @throws {TypeCheckError}
|
|
53
|
-
*/
|
|
54
|
-
export const typeCheckEnum = (value, choices) => {
|
|
55
|
-
if (!isEnum(value, choices)) {
|
|
56
|
-
const errorMessage = getErrorMessage('isEnum', value);
|
|
57
|
-
throw new TypeCheckError(errorMessage);
|
|
58
|
-
}
|
|
59
|
-
return value;
|
|
60
|
-
};
|
package/src/util/error.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
const DEFAULT_EXCLUDES = new Set(['stack']);
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Retrieves detailed information from an error object.
|
|
5
|
-
* @param {Error} error - The error to extract details from.
|
|
6
|
-
* @param {null | undefined | string[]} [excludes] - An array of property names to exclude from the result.
|
|
7
|
-
* @returns {object} - An object containing the error details.
|
|
8
|
-
*/
|
|
9
|
-
export const getErrorDetails = (error, excludes) => {
|
|
10
|
-
const errorDetails = {
|
|
11
|
-
name: error.name,
|
|
12
|
-
type: error.constructor?.name ?? typeof error,
|
|
13
|
-
};
|
|
14
|
-
if (error.message) {
|
|
15
|
-
errorDetails.message = error.message;
|
|
16
|
-
}
|
|
17
|
-
if (error.cause) {
|
|
18
|
-
errorDetails.cause = error.cause;
|
|
19
|
-
}
|
|
20
|
-
for (const key of Object.getOwnPropertyNames(error)) {
|
|
21
|
-
if (!excludes?.includes(key) && !DEFAULT_EXCLUDES.has(key)) {
|
|
22
|
-
errorDetails[key] = error[key];
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return errorDetails;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Get typed error from an unknown type.
|
|
30
|
-
* @param {unknown} error - The error to cast.
|
|
31
|
-
* @returns {Error} The typed error object.
|
|
32
|
-
*/
|
|
33
|
-
export const getTypedError = (error) => (error instanceof Error ? error : new Error(String(error)));
|