@cascateer/core 2.3.6 → 2.3.8
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 +19 -20
package/package.json
CHANGED
package/src/serializable.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Dictionary, get } from "lodash";
|
|
2
2
|
import { Brand, identity } from "ts-brand";
|
|
3
3
|
import { v4 } from "uuid";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
interface SerializerResult<O> {
|
|
6
|
+
value: O;
|
|
7
|
+
$ref: string;
|
|
8
|
+
}
|
|
6
9
|
|
|
7
10
|
export interface Serializer<O> {
|
|
8
11
|
(): SerializerResult<O>;
|
|
@@ -22,22 +25,20 @@ export abstract class Serializable<O> {
|
|
|
22
25
|
SerializableConstructor<unknown, unknown>
|
|
23
26
|
> = {};
|
|
24
27
|
|
|
25
|
-
static async fromJSON<T, O>(
|
|
26
|
-
const
|
|
28
|
+
static async fromJSON<T, O>(json: string): Promise<T> {
|
|
29
|
+
const { $ref, value }: SerializerResult<O> = JSON.parse(json);
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
const [url, path] = obj.$ref.split(/#\/?/);
|
|
31
|
+
const [url, path] = $ref.split(/#\/?/);
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
33
|
+
if (url != null && path != null) {
|
|
34
|
+
return import(url).then((module) =>
|
|
35
|
+
(
|
|
36
|
+
get(module, path.split("/")) as SerializableConstructor<T, O>
|
|
37
|
+
).fromObject(value),
|
|
38
|
+
);
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
throw new Error(`${
|
|
41
|
+
throw new Error(`${json} deserialization failed`);
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
static toJSON<T, O>(
|
|
@@ -49,12 +50,10 @@ export abstract class Serializable<O> {
|
|
|
49
50
|
|
|
50
51
|
this[importMap][id] = ctor;
|
|
51
52
|
|
|
52
|
-
return identity<BrandedSerializer<O>>(
|
|
53
|
-
()
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}) as SerializerResult<O>,
|
|
57
|
-
);
|
|
53
|
+
return identity<BrandedSerializer<O>>(() => ({
|
|
54
|
+
value: value.toObject(),
|
|
55
|
+
$ref: [`${import.meta.url}#`, this.name, importMap, id].join("/"),
|
|
56
|
+
}));
|
|
58
57
|
}
|
|
59
58
|
|
|
60
59
|
abstract toObject(): O;
|