@cascateer/core 2.3.6 → 2.3.7
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/package.json +1 -1
- package/src/serializable.ts +8 -10
package/package.json
CHANGED
package/src/serializable.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Dictionary, get, isObject, isString } from "lodash";
|
|
2
2
|
import { Brand, identity } from "ts-brand";
|
|
3
3
|
import { v4 } from "uuid";
|
|
4
4
|
|
|
@@ -17,12 +17,12 @@ interface SerializableConstructor<T, O> {
|
|
|
17
17
|
fromObject(obj: O): T;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export abstract class Serializable<O> {
|
|
20
|
+
export abstract class Serializable<O extends object> {
|
|
21
21
|
static readonly importMap: Dictionary<
|
|
22
22
|
SerializableConstructor<unknown, unknown>
|
|
23
23
|
> = {};
|
|
24
24
|
|
|
25
|
-
static async fromJSON<T, O>(value: string): Promise<T> {
|
|
25
|
+
static async fromJSON<T, O extends object>(value: string): Promise<T> {
|
|
26
26
|
const obj: O = JSON.parse(value);
|
|
27
27
|
|
|
28
28
|
if (isObject(obj) && "$ref" in obj && isString(obj.$ref)) {
|
|
@@ -40,7 +40,7 @@ export abstract class Serializable<O> {
|
|
|
40
40
|
throw new Error(`${value} deserialization failed`);
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
static toJSON<T, O>(
|
|
43
|
+
static toJSON<T, O extends object>(
|
|
44
44
|
ctor: SerializableConstructor<T, O>,
|
|
45
45
|
value: Serializable<O>,
|
|
46
46
|
): BrandedSerializer<O> {
|
|
@@ -49,12 +49,10 @@ export abstract class Serializable<O> {
|
|
|
49
49
|
|
|
50
50
|
this[importMap][id] = ctor;
|
|
51
51
|
|
|
52
|
-
return identity<BrandedSerializer<O>>(
|
|
53
|
-
()
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}) as SerializerResult<O>,
|
|
57
|
-
);
|
|
52
|
+
return identity<BrandedSerializer<O>>(() => ({
|
|
53
|
+
...value.toObject(),
|
|
54
|
+
$ref: [`${import.meta.url}#`, this.name, importMap, id].join("/"),
|
|
55
|
+
}));
|
|
58
56
|
}
|
|
59
57
|
|
|
60
58
|
abstract toObject(): O;
|