@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/serializable.ts +19 -20
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cascateer/core",
3
- "version": "2.3.6",
3
+ "version": "2.3.8",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cascateer/core.git"
@@ -1,8 +1,11 @@
1
- import { assign, 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>;
@@ -22,22 +25,20 @@ export abstract class Serializable<O> {
22
25
  SerializableConstructor<unknown, unknown>
23
26
  > = {};
24
27
 
25
- static async fromJSON<T, O>(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);
27
30
 
28
- if (isObject(obj) && "$ref" in obj && isString(obj.$ref)) {
29
- const [url, path] = obj.$ref.split(/#\/?/);
31
+ const [url, path] = $ref.split(/#\/?/);
30
32
 
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
- }
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(`${value} deserialization failed`);
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
- assign(value.toObject(), {
55
- $ref: [`${import.meta.url}#`, this.name, importMap, id].join("/"),
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;