@cascateer/core 2.3.7 → 2.3.9

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