@wener/utils 1.1.53 → 1.1.56
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/lib/arrays/arrayFromAsync.js +11 -3
- package/lib/asyncs/AsyncInterval.js +11 -3
- package/lib/asyncs/Promises.js +6 -5
- package/lib/asyncs/createAsyncIterator.js +11 -3
- package/lib/asyncs/createLazyPromise.test.js +11 -3
- package/lib/asyncs/generatorOfStream.js +11 -3
- package/lib/browsers/download.js +11 -3
- package/lib/browsers/loaders.js +11 -3
- package/lib/crypto/hashing.js +11 -3
- package/lib/crypto/hashing.test.js +11 -3
- package/lib/crypto/pem/pem.js +1 -1
- package/lib/errors/Errors.js +201 -2
- package/lib/fetch/createFetchWith.js +11 -3
- package/lib/fetch/dumpRequest.js +12 -4
- package/lib/fetch/dumpRequest.test.js +11 -3
- package/lib/fetch/dumpResponse.js +11 -3
- package/lib/fetch/dumpResponse.test.js +11 -3
- package/lib/index.js +9 -8
- package/lib/io/ArrayBuffers.js +559 -2
- package/lib/io/ByteBuffer.test.js +11 -3
- package/lib/io/parseDataUri.js +11 -3
- package/lib/io/parseDataUri.test.js +31 -11
- package/lib/langs/AsyncCloser.js +11 -3
- package/lib/langs/deepFreeze.js +5 -5
- package/lib/langs/mixin.js +6 -12
- package/lib/langs/mixin.test.js +53 -5
- package/lib/langs/mixin2.js +26 -0
- package/lib/langs/parseBoolean.js +3 -2
- package/lib/langs/parseDate.js +20 -0
- package/lib/langs/shallowEqual.js +5 -5
- package/lib/libs/ms.js +1 -1
- package/lib/maths/random.js +12 -4
- package/lib/objects/merge/isMergeableObject.js +1 -1
- package/lib/objects/merge/merge.js +1 -1
- package/lib/objects/merge/merge.test.js +11 -3
- package/lib/objects/set.js +10 -2
- package/lib/objects/set.test.js +2 -2
- package/lib/scripts/getGenerateContext.js +13 -5
- package/lib/server/fetch/createFetchWithProxyByNodeFetch.js +11 -3
- package/lib/server/fetch/createFetchWithProxyByUndici.js +11 -3
- package/lib/server/polyfill/polyfillBrowser.js +11 -3
- package/lib/server/polyfill/polyfillBrowser.test.js +11 -3
- package/lib/server/polyfill/polyfillCrypto.js +11 -3
- package/lib/server/polyfill/polyfillJsDom.js +31 -11
- package/lib/strings/renderTemplate.test.js +2 -2
- package/lib/web/getRandomValues.js +1 -1
- package/lib/web/structuredClone.js +2 -2
- package/package.json +8 -4
- package/src/asyncs/Promises.ts +2 -1
- package/src/asyncs/timeout.ts +1 -1
- package/src/crypto/hashing.ts +7 -6
- package/src/crypto/pem/pem.ts +3 -2
- package/src/errors/Errors.ts +106 -1
- package/src/fetch/dumpRequest.ts +1 -1
- package/src/index.ts +10 -8
- package/src/io/ArrayBuffers.ts +676 -1
- package/src/langs/mixin.test.ts +35 -5
- package/src/langs/mixin.ts +46 -65
- package/src/langs/mixin2.ts +80 -0
- package/src/langs/parseBoolean.ts +9 -1
- package/src/langs/parseDate.ts +13 -0
- package/src/objects/set.ts +10 -1
- package/src/types.d.ts +1 -1
- package/tsconfig.json +8 -14
- package/lib/errors/Errors.mod.js +0 -206
- package/lib/io/ArrayBuffers.mod.js +0 -531
- package/src/errors/Errors.mod.ts +0 -104
- package/src/io/ArrayBuffers.mod.ts +0 -670
package/src/langs/mixin.test.ts
CHANGED
|
@@ -1,9 +1,43 @@
|
|
|
1
1
|
import { expect, test } from 'vitest';
|
|
2
2
|
import { mixin } from './mixin';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
// import type { Constructor } from '#/types';
|
|
5
|
+
type Constructor<T = {}> = new (...args: any[]) => T;
|
|
6
|
+
function Ent<T extends Function>(): (target: T) => void {
|
|
7
|
+
return () => {};
|
|
8
|
+
}
|
|
5
9
|
|
|
6
10
|
test('mixin', () => {
|
|
11
|
+
// @Ent()
|
|
12
|
+
class User extends mixin(BaseResource, withFooFields) {
|
|
13
|
+
a?: string;
|
|
14
|
+
}
|
|
15
|
+
// @Ent()
|
|
16
|
+
class User2 extends mixin(BaseEnt, withFooFields) {
|
|
17
|
+
x: string = 'x';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// type works
|
|
21
|
+
const usr = new User();
|
|
22
|
+
expect(usr.foo, 'foo');
|
|
23
|
+
|
|
24
|
+
const u2 = new User2();
|
|
25
|
+
console.log(u2);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// <T extends EntityClass<unknown>>(options?: EntityOptions<T>): (target: T) => void;
|
|
29
|
+
|
|
30
|
+
// @Ent()
|
|
31
|
+
class BaseResource {
|
|
32
|
+
id?: string = '';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// @Ent()
|
|
36
|
+
class BaseEnt extends BaseResource {
|
|
37
|
+
base: string = 'base';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
test('mixin deep not working', () => {
|
|
7
41
|
// @ts-ignore
|
|
8
42
|
class User extends mixin(BaseResource, createBarFields()) {}
|
|
9
43
|
|
|
@@ -14,10 +48,6 @@ test('mixin', () => {
|
|
|
14
48
|
expect(usr).toEqual({ foo: 'foo', bar: 'bar', id: '' });
|
|
15
49
|
});
|
|
16
50
|
|
|
17
|
-
class BaseResource {
|
|
18
|
-
id?: string = '';
|
|
19
|
-
}
|
|
20
|
-
|
|
21
51
|
function createBarFields() {
|
|
22
52
|
return <TBase extends Constructor>(Base: TBase) => {
|
|
23
53
|
// nested type not working
|
package/src/langs/mixin.ts
CHANGED
|
@@ -1,79 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
* Helper type to convert a union to an intersection.
|
|
3
|
-
*
|
|
4
|
-
* @internal
|
|
5
|
-
*/
|
|
6
|
-
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never;
|
|
1
|
+
import type { Constructor } from '../types';
|
|
7
2
|
|
|
8
3
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* @typeParam T The type of the instance to create.
|
|
12
|
-
* @example ```ts
|
|
13
|
-
* function Walkable<TBase extends MixinConstructor<Positionable>>(Base: TBase) {
|
|
14
|
-
* ··return class Walkable extends Base {
|
|
15
|
-
* ····public forward() { this.x++; }
|
|
16
|
-
* ····public backward() { this.x--; }
|
|
17
|
-
* ··};
|
|
18
|
-
* }
|
|
19
|
-
* ```
|
|
20
|
-
* @example ```ts
|
|
21
|
-
* function Loggable(Base: MixinConstructor) {
|
|
22
|
-
* ··return class Loggable extends Base {
|
|
23
|
-
* ····public log(message: string) { throw new Error(404); }
|
|
24
|
-
* ··};
|
|
25
|
-
* }
|
|
26
|
-
* ```
|
|
4
|
+
* Mixin function type - takes a base class and returns an extended class
|
|
27
5
|
*/
|
|
28
|
-
export type
|
|
6
|
+
export type MixinFn<TBase extends Constructor = Constructor, TResult extends TBase = TBase> = (Base: TBase) => TResult;
|
|
29
7
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* @typeParam T The type of the base class.
|
|
34
|
-
* @typeParam R The type of the returned class.
|
|
35
|
-
*/
|
|
36
|
-
export type MixinFunction<T extends MixinConstructor = MixinConstructor, R extends T = T & MixinConstructor> = (
|
|
8
|
+
export function mixin<T extends Constructor>(Base: T): T;
|
|
9
|
+
export function mixin<T extends Constructor, M1 extends MixinFn<T>>(Base: T, m1: M1): ReturnType<M1>;
|
|
10
|
+
export function mixin<T extends Constructor, M1 extends MixinFn<T>, M2 extends MixinFn<ReturnType<M1>>>(
|
|
37
11
|
Base: T,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
12
|
+
m1: M1,
|
|
13
|
+
m2: M2,
|
|
14
|
+
): ReturnType<M2>;
|
|
15
|
+
export function mixin<
|
|
16
|
+
T extends Constructor,
|
|
17
|
+
M1 extends MixinFn<T>,
|
|
18
|
+
M2 extends MixinFn<ReturnType<M1>>,
|
|
19
|
+
M3 extends MixinFn<ReturnType<M2>>,
|
|
20
|
+
>(Base: T, m1: M1, m2: M2, m3: M3): ReturnType<M3>;
|
|
21
|
+
export function mixin<
|
|
22
|
+
T extends Constructor,
|
|
23
|
+
M1 extends MixinFn<T>,
|
|
24
|
+
M2 extends MixinFn<ReturnType<M1>>,
|
|
25
|
+
M3 extends MixinFn<ReturnType<M2>>,
|
|
26
|
+
M4 extends MixinFn<ReturnType<M3>>,
|
|
27
|
+
>(Base: T, m1: M1, m2: M2, m3: M3, m4: M4): ReturnType<M4>;
|
|
28
|
+
export function mixin<
|
|
29
|
+
T extends Constructor,
|
|
30
|
+
M1 extends MixinFn<T>,
|
|
31
|
+
M2 extends MixinFn<ReturnType<M1>>,
|
|
32
|
+
M3 extends MixinFn<ReturnType<M2>>,
|
|
33
|
+
M4 extends MixinFn<ReturnType<M3>>,
|
|
34
|
+
M5 extends MixinFn<ReturnType<M4>>,
|
|
35
|
+
>(Base: T, m1: M1, m2: M2, m3: M3, m4: M4, m5: M5): ReturnType<M5>;
|
|
36
|
+
export function mixin<
|
|
37
|
+
T extends Constructor,
|
|
38
|
+
M1 extends MixinFn<T>,
|
|
39
|
+
M2 extends MixinFn<ReturnType<M1>>,
|
|
40
|
+
M3 extends MixinFn<ReturnType<M2>>,
|
|
41
|
+
M4 extends MixinFn<ReturnType<M3>>,
|
|
42
|
+
M5 extends MixinFn<ReturnType<M4>>,
|
|
43
|
+
M6 extends MixinFn<ReturnType<M5>>,
|
|
44
|
+
>(Base: T, m1: M1, m2: M2, m3: M3, m4: M4, m5: M5, m6: M6): ReturnType<M6>;
|
|
57
45
|
|
|
58
46
|
/**
|
|
59
|
-
* Applies the given mixins to
|
|
47
|
+
* Applies the given mixins to a common base class.
|
|
60
48
|
*
|
|
61
49
|
* @param Base The base class to apply the mixins to.
|
|
62
|
-
* @param mixins The mixins to apply
|
|
50
|
+
* @param mixins The mixins to apply sequentially.
|
|
63
51
|
* @returns A class constructor with all mixins applied.
|
|
64
52
|
*
|
|
65
|
-
* @
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* @example ```ts
|
|
69
|
-
* class Dog extends mixin(Animal, FourLegged, Carnivore, PackHunting, Barking, Domesticated) {}
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* class Dog extends mixin(Animal, FourLegged, Carnivore) {}
|
|
70
56
|
* ```
|
|
71
57
|
*/
|
|
72
|
-
export function mixin<T extends
|
|
73
|
-
Base
|
|
74
|
-
...mixins: M
|
|
75
|
-
): MixinReturnValue<T, M> {
|
|
76
|
-
return mixins.reduce((mix, applyMixin) => applyMixin(mix), Base) as MixinReturnValue<T, M>;
|
|
58
|
+
export function mixin<T extends Constructor>(Base: T, ...mixins: MixinFn<any>[]): Constructor {
|
|
59
|
+
return mixins.reduce((mix, applyMixin) => applyMixin(mix), Base as Constructor);
|
|
77
60
|
}
|
|
78
|
-
|
|
79
|
-
// https://github.com/1nVitr0/lib-ts-mixin-extended
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper type to convert a union to an intersection.
|
|
3
|
+
*
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Constructor function, that creates a new instance of the given type.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T The type of the instance to create.
|
|
12
|
+
* @example ```ts
|
|
13
|
+
* function Walkable<TBase extends MixinConstructor<Positionable>>(Base: TBase) {
|
|
14
|
+
* ··return class Walkable extends Base {
|
|
15
|
+
* ····public forward() { this.x++; }
|
|
16
|
+
* ····public backward() { this.x--; }
|
|
17
|
+
* ··};
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
* @example ```ts
|
|
21
|
+
* function Loggable(Base: MixinConstructor) {
|
|
22
|
+
* ··return class Loggable extends Base {
|
|
23
|
+
* ····public log(message: string) { throw new Error(404); }
|
|
24
|
+
* ··};
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export type MixinConstructor<T = {}> = new (...args: any[]) => T;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Function that applies a mixin to a given base class.
|
|
32
|
+
*
|
|
33
|
+
* @typeParam T The type of the base class.
|
|
34
|
+
* @typeParam R The type of the returned class.
|
|
35
|
+
*/
|
|
36
|
+
export type MixinFunction<T extends MixinConstructor = MixinConstructor, R extends T = T & MixinConstructor> = (
|
|
37
|
+
Base: T,
|
|
38
|
+
) => R;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The return type of the mixin function.
|
|
42
|
+
*
|
|
43
|
+
* @typeParam T The type of the base class.
|
|
44
|
+
* @typeParam M The type of the mixin functions.
|
|
45
|
+
*/
|
|
46
|
+
export type MixinReturnValue<T extends MixinConstructor, M extends MixinFunction<T, any>[]> = UnionToIntersection<
|
|
47
|
+
T | { [K in keyof M]: M[K] extends MixinFunction<any, infer U> ? U : never }[number]
|
|
48
|
+
>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The instance created by a mixin function.
|
|
52
|
+
*
|
|
53
|
+
* @typeParam F The type of the mixin function.
|
|
54
|
+
*/
|
|
55
|
+
export type MixinInstance<F extends MixinFunction<any>> =
|
|
56
|
+
F extends MixinFunction<MixinConstructor<any>, infer R> ? InstanceType<R> : never;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Applies the given mixins to the a common base class.
|
|
60
|
+
*
|
|
61
|
+
* @param Base The base class to apply the mixins to.
|
|
62
|
+
* @param mixins The mixins to apply. All mixins must extend a common base class or an empty class.
|
|
63
|
+
* @returns A class constructor with all mixins applied.
|
|
64
|
+
*
|
|
65
|
+
* @typeParam T The type of the base class.
|
|
66
|
+
* @typeParam M The type of the mixin functions.
|
|
67
|
+
*
|
|
68
|
+
* @example ```ts
|
|
69
|
+
* class Dog extends mixin(Animal, FourLegged, Carnivore, PackHunting, Barking, Domesticated) {}
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export function mixin<T extends MixinConstructor, M extends MixinFunction<T, any>[]>(
|
|
73
|
+
Base: T,
|
|
74
|
+
...mixins: M
|
|
75
|
+
): MixinReturnValue<T, M> {
|
|
76
|
+
return mixins.reduce((mix, applyMixin) => applyMixin(mix), Base) as MixinReturnValue<T, M>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// this is complex, may cause ts unable to resolve prototype typing
|
|
80
|
+
// https://github.com/1nVitr0/lib-ts-mixin-extended
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
export interface ParseBooleanOptions {
|
|
2
|
+
strict?: boolean;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function parseBoolean(s: string | boolean | number | null | undefined, options: { strict: true }): boolean | undefined;
|
|
6
|
+
export function parseBoolean(s: string | boolean | number | null | undefined | any, options?: ParseBooleanOptions): boolean;
|
|
7
|
+
/** @deprecated Use `parseBoolean(s, { strict: true })` instead */
|
|
1
8
|
export function parseBoolean(s: string | boolean | number | null | undefined, strict: true): boolean | undefined;
|
|
2
9
|
export function parseBoolean(s: string | boolean | number | null | undefined | any): boolean;
|
|
3
|
-
export function parseBoolean(s?: string | boolean | number | null,
|
|
10
|
+
export function parseBoolean(s?: string | boolean | number | null, options?: boolean | ParseBooleanOptions): boolean | undefined {
|
|
11
|
+
const strict = typeof options === 'boolean' ? options : options?.strict ?? false;
|
|
4
12
|
if (typeof s === 'boolean') {
|
|
5
13
|
return s;
|
|
6
14
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function parseDate(value: Date | string | undefined | null): Date | undefined {
|
|
2
|
+
if (!value) {
|
|
3
|
+
return undefined;
|
|
4
|
+
}
|
|
5
|
+
if (value instanceof Date) {
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
const parsed = new Date(value);
|
|
9
|
+
if (isNaN(parsed.getTime())) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
return parsed;
|
|
13
|
+
}
|
package/src/objects/set.ts
CHANGED
|
@@ -13,7 +13,9 @@ export function set<T extends object, V>(obj: T, key: ObjectKey | ObjectPath, va
|
|
|
13
13
|
let x, k;
|
|
14
14
|
while (i < len) {
|
|
15
15
|
k = path[i++];
|
|
16
|
+
// Security: Prevent prototype pollution
|
|
16
17
|
if (k === '__proto__' || k === 'constructor' || k === 'prototype') break;
|
|
18
|
+
|
|
17
19
|
// noinspection PointlessArithmeticExpressionJS
|
|
18
20
|
current = current[k] =
|
|
19
21
|
i === len
|
|
@@ -22,7 +24,14 @@ export function set<T extends object, V>(obj: T, key: ObjectKey | ObjectPath, va
|
|
|
22
24
|
: val
|
|
23
25
|
: typeof (x = current[k]) === typeof path
|
|
24
26
|
? x
|
|
25
|
-
: //
|
|
27
|
+
: // Determine if we should create an Object or an Array for the next level
|
|
28
|
+
// If the next key is NOT an integer-like index, or contains a dot, create an Object.
|
|
29
|
+
// Otherwise, create an Array.
|
|
30
|
+
//
|
|
31
|
+
// path[i] * 0 !== 0 checks if it is NOT a number (NaN * 0 is NaN).
|
|
32
|
+
// !!~('' + path[i]).indexOf('.') checks if it contains a dot.
|
|
33
|
+
//
|
|
34
|
+
// @ts-expect-error hacky type check from dset
|
|
26
35
|
path[i] * 0 !== 0 || !!~('' + path[i]).indexOf('.') // eslint-disable-line
|
|
27
36
|
? {}
|
|
28
37
|
: [];
|
package/src/types.d.ts
CHANGED
package/tsconfig.json
CHANGED
|
@@ -3,17 +3,13 @@
|
|
|
3
3
|
"moduleResolution": "bundler",
|
|
4
4
|
"target": "ESNext",
|
|
5
5
|
"module": "ESNext",
|
|
6
|
-
"lib": [
|
|
7
|
-
"dom",
|
|
8
|
-
"dom.iterable",
|
|
9
|
-
"ESNext"
|
|
10
|
-
],
|
|
6
|
+
"lib": ["dom", "dom.iterable", "ESNext"],
|
|
11
7
|
"strict": true,
|
|
12
8
|
"sourceMap": true,
|
|
13
9
|
"skipLibCheck": true,
|
|
14
10
|
"esModuleInterop": true,
|
|
15
11
|
"verbatimModuleSyntax": true,
|
|
16
|
-
"erasableSyntaxOnly":
|
|
12
|
+
"erasableSyntaxOnly": false,
|
|
17
13
|
"allowSyntheticDefaultImports": true,
|
|
18
14
|
"noImplicitAny": true,
|
|
19
15
|
"noImplicitReturns": true,
|
|
@@ -25,13 +21,11 @@
|
|
|
25
21
|
"isolatedModules": true,
|
|
26
22
|
"outDir": "lib",
|
|
27
23
|
"baseUrl": ".",
|
|
28
|
-
"rootDir": "./src"
|
|
24
|
+
"rootDir": "./src",
|
|
25
|
+
"paths": {
|
|
26
|
+
"#/*": ["./src/*"]
|
|
27
|
+
}
|
|
29
28
|
},
|
|
30
|
-
"exclude": [
|
|
31
|
-
|
|
32
|
-
],
|
|
33
|
-
"include": [
|
|
34
|
-
"src/types.d.ts",
|
|
35
|
-
"src"
|
|
36
|
-
]
|
|
29
|
+
"exclude": ["node_modules", "**/*.test.ts", "**/*.spec.ts"],
|
|
30
|
+
"include": ["src/types.d.ts", "src"]
|
|
37
31
|
}
|
package/lib/errors/Errors.mod.js
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
function _define_property(obj, key, value) {
|
|
2
|
-
if (key in obj) {
|
|
3
|
-
Object.defineProperty(obj, key, {
|
|
4
|
-
value: value,
|
|
5
|
-
enumerable: true,
|
|
6
|
-
configurable: true,
|
|
7
|
-
writable: true
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
obj[key] = value;
|
|
12
|
-
}
|
|
13
|
-
return obj;
|
|
14
|
-
}
|
|
15
|
-
function _instanceof(left, right) {
|
|
16
|
-
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
17
|
-
return !!right[Symbol.hasInstance](left);
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
return left instanceof right;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function _object_spread(target) {
|
|
24
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
25
|
-
var source = arguments[i] != null ? arguments[i] : {};
|
|
26
|
-
var ownKeys = Object.keys(source);
|
|
27
|
-
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
28
|
-
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
|
|
29
|
-
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
30
|
-
}));
|
|
31
|
-
}
|
|
32
|
-
ownKeys.forEach(function (key) {
|
|
33
|
-
_define_property(target, key, source[key]);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
return target;
|
|
37
|
-
}
|
|
38
|
-
function ownKeys(object, enumerableOnly) {
|
|
39
|
-
var keys = Object.keys(object);
|
|
40
|
-
if (Object.getOwnPropertySymbols) {
|
|
41
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
|
42
|
-
if (enumerableOnly) {
|
|
43
|
-
symbols = symbols.filter(function (sym) {
|
|
44
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
keys.push.apply(keys, symbols);
|
|
48
|
-
}
|
|
49
|
-
return keys;
|
|
50
|
-
}
|
|
51
|
-
function _object_spread_props(target, source) {
|
|
52
|
-
source = source != null ? source : {};
|
|
53
|
-
if (Object.getOwnPropertyDescriptors) {
|
|
54
|
-
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
ownKeys(Object(source)).forEach(function (key) {
|
|
58
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
return target;
|
|
62
|
-
}
|
|
63
|
-
import { DetailError, DetailHolder } from "./DetailError.js";
|
|
64
|
-
/*
|
|
65
|
-
https://github.com/tc39/proposal-error-cause
|
|
66
|
-
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
|
|
67
|
-
Stage 3
|
|
68
|
-
Chrome 93, Safari 15, Node 16.9
|
|
69
|
-
|
|
70
|
-
https://www.npmjs.com/package/pony-cause
|
|
71
|
-
*/ export var BadRequest = create({
|
|
72
|
-
status: 400
|
|
73
|
-
});
|
|
74
|
-
export var Unauthorized = create({
|
|
75
|
-
status: 403
|
|
76
|
-
});
|
|
77
|
-
export var Forbidden = create({
|
|
78
|
-
status: 403
|
|
79
|
-
});
|
|
80
|
-
export var NotFound = create({
|
|
81
|
-
status: 404
|
|
82
|
-
});
|
|
83
|
-
export var InternalServerError = create({
|
|
84
|
-
status: 500
|
|
85
|
-
});
|
|
86
|
-
export var NotImplemented = create({
|
|
87
|
-
status: 501
|
|
88
|
-
});
|
|
89
|
-
export var ServiceUnavailable = create({
|
|
90
|
-
status: 503
|
|
91
|
-
});
|
|
92
|
-
export var IllegalState = create({
|
|
93
|
-
status: 500,
|
|
94
|
-
message: "Illegal State"
|
|
95
|
-
});
|
|
96
|
-
export var UnsupportedOperation = create({
|
|
97
|
-
status: 500,
|
|
98
|
-
message: "Unsupported Operation"
|
|
99
|
-
});
|
|
100
|
-
export var IllegalArgument = create({
|
|
101
|
-
status: 400,
|
|
102
|
-
message: "Illegal Argument"
|
|
103
|
-
});
|
|
104
|
-
export var InvalidType = create({
|
|
105
|
-
status: 400,
|
|
106
|
-
message: "Invalid Type"
|
|
107
|
-
});
|
|
108
|
-
// known errors
|
|
109
|
-
// TypeError when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type.
|
|
110
|
-
// RangeError when a number is not within the correct range allowed.
|
|
111
|
-
// AggregateError when multiple errors need to be reported by an operation, for example by Promise.all().
|
|
112
|
-
// 例如 Promise.any() 会返回一个 AggregateError,其中包含所有 rejected 的 Promise 的错误。
|
|
113
|
-
// EvalError when an error occurs during the evaluation of JavaScript code.
|
|
114
|
-
// ReferenceError when a non-existent variable is referenced.
|
|
115
|
-
// SyntaxError when a syntax error occurs while parsing code in eval().
|
|
116
|
-
// URIError when encodeURI() or decodeURI() are passed invalid parameters.
|
|
117
|
-
// InternalError when an internal error in the JavaScript engine is thrown. E.g. "too much recursion".
|
|
118
|
-
// DOMException when an error occurs in the DOM.
|
|
119
|
-
export var resolvers = [];
|
|
120
|
-
export function create(init) {
|
|
121
|
-
return new DetailHolder(init);
|
|
122
|
-
}
|
|
123
|
-
export function ok(res, o) {
|
|
124
|
-
if (res.ok) {
|
|
125
|
-
return res;
|
|
126
|
-
}
|
|
127
|
-
throw create(_object_spread_props(_object_spread({
|
|
128
|
-
description: res.statusText,
|
|
129
|
-
status: res.status
|
|
130
|
-
}, o), {
|
|
131
|
-
metadata: _object_spread_props(_object_spread({}, o === null || o === void 0 ? void 0 : o.metadata), {
|
|
132
|
-
response: res
|
|
133
|
-
})
|
|
134
|
-
})).asError();
|
|
135
|
-
}
|
|
136
|
-
export function resolve(e) {
|
|
137
|
-
if (_instanceof(e, DetailHolder)) {
|
|
138
|
-
return e;
|
|
139
|
-
}
|
|
140
|
-
if (_instanceof(e, DetailError)) {
|
|
141
|
-
return e.detail;
|
|
142
|
-
}
|
|
143
|
-
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
144
|
-
try {
|
|
145
|
-
for (var _iterator = resolvers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
146
|
-
var resolver = _step.value;
|
|
147
|
-
var r = resolver(e);
|
|
148
|
-
if (r) {
|
|
149
|
-
return r;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
catch (err) {
|
|
154
|
-
_didIteratorError = true;
|
|
155
|
-
_iteratorError = err;
|
|
156
|
-
}
|
|
157
|
-
finally {
|
|
158
|
-
try {
|
|
159
|
-
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
160
|
-
_iterator.return();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
finally {
|
|
164
|
-
if (_didIteratorError) {
|
|
165
|
-
throw _iteratorError;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (isError(e)) {
|
|
170
|
-
var message = e.message, code = e.code, status = e.status;
|
|
171
|
-
// can get status from NestJS HttpException
|
|
172
|
-
return new DetailHolder({
|
|
173
|
-
message: message,
|
|
174
|
-
status: parseInt(status) || 500,
|
|
175
|
-
code: code,
|
|
176
|
-
cause: e
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
return new DetailHolder({
|
|
180
|
-
message: e.message,
|
|
181
|
-
status: 500,
|
|
182
|
-
cause: e
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Check if the given value is an Error
|
|
187
|
-
* @see https://github.com/tc39/proposal-is-error
|
|
188
|
-
*/ export function isError(e) {
|
|
189
|
-
if ("isError" in Error) {
|
|
190
|
-
// will handle cross-realm
|
|
191
|
-
return Error.isError(e);
|
|
192
|
-
}
|
|
193
|
-
return _instanceof(e, Error);
|
|
194
|
-
}
|
|
195
|
-
export function isAbort(e) {
|
|
196
|
-
if (!isError(e)) {
|
|
197
|
-
return false;
|
|
198
|
-
}
|
|
199
|
-
return e.name === "AbortError";
|
|
200
|
-
}
|
|
201
|
-
export function isTimeout(e) {
|
|
202
|
-
if (!isError(e)) {
|
|
203
|
-
return false;
|
|
204
|
-
}
|
|
205
|
-
return e.name === "TimeoutError" || e.name === "Timeout";
|
|
206
|
-
}
|