yayson 4.0.0 → 4.2.0

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 (39) hide show
  1. package/README.md +272 -82
  2. package/build/legacy.cjs +88 -35
  3. package/build/legacy.d.cts +44 -8
  4. package/build/legacy.d.cts.map +1 -1
  5. package/build/legacy.d.mts +44 -8
  6. package/build/legacy.d.mts.map +1 -1
  7. package/build/legacy.mjs +88 -35
  8. package/build/legacy.mjs.map +1 -1
  9. package/build/{symbols-nFs99aEX.cjs → symbols-B--FS78o.cjs} +3 -0
  10. package/build/{symbols-DSjKJ8vh.mjs → symbols-BfU4k1el.mjs} +4 -1
  11. package/build/symbols-BfU4k1el.mjs.map +1 -0
  12. package/build/{types-DbMIe8E2.d.cts → types-KZiF6x7A.d.mts} +21 -2
  13. package/build/types-KZiF6x7A.d.mts.map +1 -0
  14. package/build/{types-BsfPiPEw.d.mts → types-iC38_iCI.d.cts} +21 -2
  15. package/build/types-iC38_iCI.d.cts.map +1 -0
  16. package/build/utils.cjs +1 -1
  17. package/build/utils.d.cts +1 -1
  18. package/build/utils.d.mts +1 -1
  19. package/build/utils.mjs +1 -1
  20. package/build/{yayson-BKEyEcGk.d.cts → yayson-BvwMr4Ad.d.mts} +18 -4
  21. package/build/yayson-BvwMr4Ad.d.mts.map +1 -0
  22. package/build/{yayson-CUfrxK9d.cjs → yayson-C4P8J4sn.cjs} +127 -51
  23. package/build/{yayson-BJv2Z0Z6.mjs → yayson-DIQ_olLX.mjs} +128 -52
  24. package/build/yayson-DIQ_olLX.mjs.map +1 -0
  25. package/build/{yayson-CRbukIqr.d.mts → yayson-DTMLeA5k.d.cts} +18 -4
  26. package/build/yayson-DTMLeA5k.d.cts.map +1 -0
  27. package/build/yayson.cjs +1 -1
  28. package/build/yayson.d.cts +2 -2
  29. package/build/yayson.d.mts +2 -2
  30. package/build/yayson.mjs +1 -1
  31. package/package.json +1 -1
  32. package/skill/yayson/SKILL.md +20 -0
  33. package/skill/yayson/references/api.md +27 -2
  34. package/build/symbols-DSjKJ8vh.mjs.map +0 -1
  35. package/build/types-BsfPiPEw.d.mts.map +0 -1
  36. package/build/types-DbMIe8E2.d.cts.map +0 -1
  37. package/build/yayson-BJv2Z0Z6.mjs.map +0 -1
  38. package/build/yayson-BKEyEcGk.d.cts.map +0 -1
  39. package/build/yayson-CRbukIqr.d.mts.map +0 -1
@@ -1,5 +1,5 @@
1
- import { T as ModelLike, _ as ValidationError, a as JsonApiRelationship, c as LegacyPresenterOptions, d as SchemaRegistry, f as StoreModel, g as StoreResult, i as JsonApiLinks, l as LegacyStoreOptions, n as JsonApiDocument, o as JsonApiRelationships, p as StoreModels, r as JsonApiLink, s as JsonApiResource, t as InferModelType, u as PresenterOptions, w as Adapter } from "./types-DbMIe8E2.cjs";
2
- import { i as Presenter, r as yayson$1, t as YaysonOptions } from "./yayson-BKEyEcGk.cjs";
1
+ import { D as ModelLike, E as Adapter, a as JsonApiRelationship, c as LegacyPresenterOptions, d as RelationshipConfig, f as SchemaRegistry, h as StoreModels, i as JsonApiLinks, l as LegacyStoreOptions, m as StoreModelWithOptionalId, n as JsonApiDocument, o as JsonApiRelationships, p as StoreModel, r as JsonApiLink, s as JsonApiResource, t as InferModelType, u as PresenterOptions, v as StoreResult, y as ValidationError } from "./types-iC38_iCI.cjs";
2
+ import { i as Presenter, r as yayson$1, t as YaysonOptions } from "./yayson-DTMLeA5k.cjs";
3
3
 
4
4
  //#region src/yayson/legacy-presenter.d.ts
5
5
  interface LegacyJsonApiDocument extends JsonApiDocument {
@@ -13,6 +13,7 @@ declare function createLegacyPresenter(Presenter: Presenter): {
13
13
  attributes(instance: ModelLike | null): Record<string, unknown>;
14
14
  includeRelationships(scope: LegacyJsonApiDocument, instance: ModelLike): unknown[];
15
15
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null | undefined, options?: LegacyPresenterOptions): LegacyJsonApiDocument;
16
+ payload(instance: ModelLike): LegacyJsonApiDocument;
16
17
  render(instanceOrCollection: ModelLike | ModelLike[] | null): LegacyJsonApiDocument;
17
18
  id(instance: ModelLike): string | undefined;
18
19
  selfLinks(_instance: ModelLike): JsonApiLink | string | undefined;
@@ -24,12 +25,15 @@ declare function createLegacyPresenter(Presenter: Presenter): {
24
25
  id(instance: ModelLike): string | undefined;
25
26
  selfLinks(_instance: ModelLike): JsonApiLink | string | undefined;
26
27
  links(_instance?: ModelLike): JsonApiLinks | undefined;
27
- relationships(): Record<string, /*elided*/any>;
28
+ relationships(): Record<string, /*elided*/any | RelationshipConfig< /*elided*/any>>;
28
29
  attributes(instance: ModelLike | null): Record<string, unknown>;
29
30
  includeRelationships(scope: JsonApiDocument, instance: ModelLike): unknown[];
30
- buildRelationships(instance: ModelLike | null): JsonApiRelationships | null;
31
+ buildRelationships(instance: ModelLike | null, options?: {
32
+ payload?: boolean;
33
+ }): JsonApiRelationships | null;
31
34
  buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
32
35
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null | undefined, options?: PresenterOptions): JsonApiDocument;
36
+ payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
33
37
  render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
34
38
  };
35
39
  adapter: typeof Adapter;
@@ -37,15 +41,43 @@ declare function createLegacyPresenter(Presenter: Presenter): {
37
41
  fields?: string[];
38
42
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
39
43
  render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
40
- }>;
41
- buildRelationships(instance: ModelLike | null): JsonApiRelationships | null;
44
+ payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
45
+ } | RelationshipConfig<{
46
+ new (scope?: JsonApiDocument): {
47
+ constructor: /*elided*/any;
48
+ scope: JsonApiDocument;
49
+ id(instance: ModelLike): string | undefined;
50
+ selfLinks(_instance: ModelLike): JsonApiLink | string | undefined;
51
+ links(_instance?: ModelLike): JsonApiLinks | undefined;
52
+ relationships(): Record<string, /*elided*/any | RelationshipConfig< /*elided*/any>>;
53
+ attributes(instance: ModelLike | null): Record<string, unknown>;
54
+ includeRelationships(scope: JsonApiDocument, instance: ModelLike): unknown[];
55
+ buildRelationships(instance: ModelLike | null, options?: {
56
+ payload?: boolean;
57
+ }): JsonApiRelationships | null;
58
+ buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
59
+ toJSON(instanceOrCollection: ModelLike | ModelLike[] | null | undefined, options?: PresenterOptions): JsonApiDocument;
60
+ payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
61
+ render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
62
+ };
63
+ adapter: typeof Adapter;
64
+ type: string;
65
+ fields?: string[];
66
+ toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
67
+ render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
68
+ payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
69
+ }>>;
70
+ buildRelationships(instance: ModelLike | null, options?: {
71
+ payload?: boolean;
72
+ }): JsonApiRelationships | null;
42
73
  buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
43
74
  };
44
75
  type: string;
45
76
  plural?: string;
46
77
  fields?: string[];
47
- toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: LegacyPresenterOptions): JsonApiDocument;
48
- render(instanceOrCollection: ModelLike | ModelLike[] | null, _options?: LegacyPresenterOptions): JsonApiDocument;
78
+ toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: LegacyPresenterOptions): LegacyJsonApiDocument;
79
+ render(instanceOrCollection: ModelLike | ModelLike[] | null, _options?: LegacyPresenterOptions): LegacyJsonApiDocument;
80
+ payload(instance: ModelLike): LegacyJsonApiDocument;
49
81
  adapter: typeof Adapter;
50
82
  };
51
83
  //#endregion
@@ -53,6 +85,7 @@ declare function createLegacyPresenter(Presenter: Presenter): {
53
85
  interface LegacyStoreRecordType {
54
86
  type: string;
55
87
  data: Record<string, unknown> & {
88
+ id?: string | number;
56
89
  meta?: Record<string, unknown>;
57
90
  links?: Record<string, unknown>;
58
91
  };
@@ -69,6 +102,7 @@ interface LegacyData {
69
102
  [key: string]: LegacyDataValue | LegacyLinks | Record<string, unknown> | undefined;
70
103
  }
71
104
  declare class LegacyStore<S extends SchemaRegistry = SchemaRegistry> {
105
+ #private;
72
106
  types: Record<string, string>;
73
107
  records: LegacyStoreRecordType[];
74
108
  relations: Record<string, Record<string, string>>;
@@ -82,6 +116,8 @@ declare class LegacyStore<S extends SchemaRegistry = SchemaRegistry> {
82
116
  setupRelations(links: LegacyLinks): void;
83
117
  findRecord(type: string, id: string): LegacyStoreRecordType | undefined;
84
118
  findRecords(type: string): LegacyStoreRecordType[];
119
+ static build(data: LegacyData): StoreModelWithOptionalId;
120
+ build(data: LegacyData): StoreModelWithOptionalId;
85
121
  /** @deprecated Use retrieve() instead. */
86
122
  retrive<T extends string>(type: T, data: LegacyData): InferModelType<S, T> | null;
87
123
  retrieve<T extends string>(type: T, data: LegacyData): InferModelType<S, T> | null;
@@ -1 +1 @@
1
- {"version":3,"file":"legacy.d.cts","names":[],"sources":["../../../../../../yayson/legacy-presenter.ts","../../../../../../yayson/legacy-store.ts","../../../../../../legacy.ts"],"mappings":";;;;UASU,qBAAA,SAA8B,eAAA;EAAA,CACrC,GAAA;AAAA;AAAA,iBAIqB,qBAAA,CAAsB,SAAA,EAAW,SAAA;EAAA,aASjC,eAAA;iBAbV;WAOK,qBAAA;;yBAsBM,SAAA,UAAmB,MAAA;gCA8BZ,qBAAA,EAAqB,QAAA,EAAY,SAAA;iCAqCrC,SAAA,GAAY,SAAA,uBAA8B,OAAA,GACtD,sBAAA,GACT,qBAAA;iCA6D0B,SAAA,GAAY,SAAA,YAAqB,qBAAA;;;;;;qBA/D1B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,CAAA;EAYzE,IAAA,kBAAA,CAAuB,IAAA,EAAM,CAAA,EAAG,EAAA,mBAAqB,MAAA,GAAS,WAAA,GAAc,cAAA,CAAe,CAAA,EAAG,CAAA;EAc9F,OAAA,kBAAA,CAA0B,IAAA,EAAM,CAAA,EAAG,MAAA,GAAS,WAAA,GAAc,cAAA,CAAe,CAAA,EAAG,CAAA;EAgB5E,MAAA,CAAO,IAAA,UAAc,EAAA;EAsBrB,OAAA,CAAQ,IAAA,EAAM,UAAA,GAAa,WAAA;EA8C3B,IAAA,CAAK,IAAA,EAAM,UAAA,GAAa,UAAA,GAAa,WAAA;EAYrC,WAAA,kBAAA,CAA8B,IAAA,EAAM,CAAA,EAAG,IAAA,EAAM,UAAA,GAAa,WAAA,CAAY,cAAA,CAAe,CAAA,EAAG,CAAA;AAAA;;;UCzPhF,kBAAA;EACR,KAAA,SAAc,WAAA;EACd,SAAA,EAAW,UAAA,QAAkB,qBAAA;EAC7B,OAAA,EAAS,UAAA,QAAkB,QAAA;AAAA;AAAA,iBAGL,MAAA,CAAO,OAAA,GAAU,aAAA,GAAgB,kBAAA"}
1
+ {"version":3,"file":"legacy.d.cts","names":[],"sources":["../../../../../../yayson/legacy-presenter.ts","../../../../../../yayson/legacy-store.ts","../../../../../../legacy.ts"],"mappings":";;;;UASU,qBAAA,SAA8B,eAAA;EAAA,CACrC,GAAA;AAAA;AAAA,iBAIqB,qBAAA,CAAsB,SAAA,EAAW,SAAA;EAAA,aASjC,eAAA;iBAbV;WAOK,qBAAA;;yBAqBM,SAAA,UAAmB,MAAA;gCA8BZ,qBAAA,EAAqB,QAAA,EAAY,SAAA;iCAsCrC,SAAA,GAAY,SAAA,uBAA8B,OAAA,GACtD,sBAAA,GACT,qBAAA;sBA6De,SAAA,GAAY,qBAAA;iCAWD,SAAA,GAAY,SAAA,YAAqB,qBAAA;;;;;;qBAzElD;;;;;wCAyE6B,oCAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,cAAA,CAAe,CAAA,EAAG,CAAA;EAIxE,QAAA,kBAAA,CAA2B,IAAA,EAAM,CAAA,EAAG,IAAA,EAAM,UAAA,GAAa,cAAA,CAAe,CAAA,EAAG,CAAA;EAYzE,IAAA,kBAAA,CAAuB,IAAA,EAAM,CAAA,EAAG,EAAA,mBAAqB,MAAA,GAAS,WAAA,GAAc,cAAA,CAAe,CAAA,EAAG,CAAA;EAM9F,OAAA,kBAAA,CAA0B,IAAA,EAAM,CAAA,EAAG,MAAA,GAAS,WAAA,GAAc,cAAA,CAAe,CAAA,EAAG,CAAA;EAgB5E,MAAA,CAAO,IAAA,UAAc,EAAA;EAsBrB,OAAA,CAAQ,IAAA,EAAM,UAAA,GAAa,WAAA;EA4C3B,IAAA,CAAK,IAAA,EAAM,UAAA,GAAa,UAAA,GAAa,WAAA;EAYrC,WAAA,kBAAA,CAA8B,IAAA,EAAM,CAAA,EAAG,IAAA,EAAM,UAAA,GAAa,WAAA,CAAY,cAAA,CAAe,CAAA,EAAG,CAAA;AAAA;;;UCjUhF,kBAAA;EACR,KAAA,SAAc,WAAA;EACd,SAAA,EAAW,UAAA,QAAkB,qBAAA;EAC7B,OAAA,EAAS,UAAA,QAAkB,QAAA;AAAA;AAAA,iBAGL,MAAA,CAAO,OAAA,GAAU,aAAA,GAAgB,kBAAA"}
@@ -1,5 +1,5 @@
1
- import { T as ModelLike, _ as ValidationError, a as JsonApiRelationship, c as LegacyPresenterOptions, d as SchemaRegistry, f as StoreModel, g as StoreResult, i as JsonApiLinks, l as LegacyStoreOptions, n as JsonApiDocument, o as JsonApiRelationships, p as StoreModels, r as JsonApiLink, s as JsonApiResource, t as InferModelType, u as PresenterOptions, w as Adapter } from "./types-BsfPiPEw.mjs";
2
- import { i as Presenter, r as yayson$1, t as YaysonOptions } from "./yayson-CRbukIqr.mjs";
1
+ import { D as ModelLike, E as Adapter, a as JsonApiRelationship, c as LegacyPresenterOptions, d as RelationshipConfig, f as SchemaRegistry, h as StoreModels, i as JsonApiLinks, l as LegacyStoreOptions, m as StoreModelWithOptionalId, n as JsonApiDocument, o as JsonApiRelationships, p as StoreModel, r as JsonApiLink, s as JsonApiResource, t as InferModelType, u as PresenterOptions, v as StoreResult, y as ValidationError } from "./types-KZiF6x7A.mjs";
2
+ import { i as Presenter, r as yayson$1, t as YaysonOptions } from "./yayson-BvwMr4Ad.mjs";
3
3
 
4
4
  //#region src/yayson/legacy-presenter.d.ts
5
5
  interface LegacyJsonApiDocument extends JsonApiDocument {
@@ -13,6 +13,7 @@ declare function createLegacyPresenter(Presenter: Presenter): {
13
13
  attributes(instance: ModelLike | null): Record<string, unknown>;
14
14
  includeRelationships(scope: LegacyJsonApiDocument, instance: ModelLike): unknown[];
15
15
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null | undefined, options?: LegacyPresenterOptions): LegacyJsonApiDocument;
16
+ payload(instance: ModelLike): LegacyJsonApiDocument;
16
17
  render(instanceOrCollection: ModelLike | ModelLike[] | null): LegacyJsonApiDocument;
17
18
  id(instance: ModelLike): string | undefined;
18
19
  selfLinks(_instance: ModelLike): JsonApiLink | string | undefined;
@@ -24,12 +25,15 @@ declare function createLegacyPresenter(Presenter: Presenter): {
24
25
  id(instance: ModelLike): string | undefined;
25
26
  selfLinks(_instance: ModelLike): JsonApiLink | string | undefined;
26
27
  links(_instance?: ModelLike): JsonApiLinks | undefined;
27
- relationships(): Record<string, /*elided*/any>;
28
+ relationships(): Record<string, /*elided*/any | RelationshipConfig< /*elided*/any>>;
28
29
  attributes(instance: ModelLike | null): Record<string, unknown>;
29
30
  includeRelationships(scope: JsonApiDocument, instance: ModelLike): unknown[];
30
- buildRelationships(instance: ModelLike | null): JsonApiRelationships | null;
31
+ buildRelationships(instance: ModelLike | null, options?: {
32
+ payload?: boolean;
33
+ }): JsonApiRelationships | null;
31
34
  buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
32
35
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null | undefined, options?: PresenterOptions): JsonApiDocument;
36
+ payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
33
37
  render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
34
38
  };
35
39
  adapter: typeof Adapter;
@@ -37,15 +41,43 @@ declare function createLegacyPresenter(Presenter: Presenter): {
37
41
  fields?: string[];
38
42
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
39
43
  render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
40
- }>;
41
- buildRelationships(instance: ModelLike | null): JsonApiRelationships | null;
44
+ payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
45
+ } | RelationshipConfig<{
46
+ new (scope?: JsonApiDocument): {
47
+ constructor: /*elided*/any;
48
+ scope: JsonApiDocument;
49
+ id(instance: ModelLike): string | undefined;
50
+ selfLinks(_instance: ModelLike): JsonApiLink | string | undefined;
51
+ links(_instance?: ModelLike): JsonApiLinks | undefined;
52
+ relationships(): Record<string, /*elided*/any | RelationshipConfig< /*elided*/any>>;
53
+ attributes(instance: ModelLike | null): Record<string, unknown>;
54
+ includeRelationships(scope: JsonApiDocument, instance: ModelLike): unknown[];
55
+ buildRelationships(instance: ModelLike | null, options?: {
56
+ payload?: boolean;
57
+ }): JsonApiRelationships | null;
58
+ buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
59
+ toJSON(instanceOrCollection: ModelLike | ModelLike[] | null | undefined, options?: PresenterOptions): JsonApiDocument;
60
+ payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
61
+ render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
62
+ };
63
+ adapter: typeof Adapter;
64
+ type: string;
65
+ fields?: string[];
66
+ toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
67
+ render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
68
+ payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
69
+ }>>;
70
+ buildRelationships(instance: ModelLike | null, options?: {
71
+ payload?: boolean;
72
+ }): JsonApiRelationships | null;
42
73
  buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
43
74
  };
44
75
  type: string;
45
76
  plural?: string;
46
77
  fields?: string[];
47
- toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: LegacyPresenterOptions): JsonApiDocument;
48
- render(instanceOrCollection: ModelLike | ModelLike[] | null, _options?: LegacyPresenterOptions): JsonApiDocument;
78
+ toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: LegacyPresenterOptions): LegacyJsonApiDocument;
79
+ render(instanceOrCollection: ModelLike | ModelLike[] | null, _options?: LegacyPresenterOptions): LegacyJsonApiDocument;
80
+ payload(instance: ModelLike): LegacyJsonApiDocument;
49
81
  adapter: typeof Adapter;
50
82
  };
51
83
  //#endregion
@@ -53,6 +85,7 @@ declare function createLegacyPresenter(Presenter: Presenter): {
53
85
  interface LegacyStoreRecordType {
54
86
  type: string;
55
87
  data: Record<string, unknown> & {
88
+ id?: string | number;
56
89
  meta?: Record<string, unknown>;
57
90
  links?: Record<string, unknown>;
58
91
  };
@@ -69,6 +102,7 @@ interface LegacyData {
69
102
  [key: string]: LegacyDataValue | LegacyLinks | Record<string, unknown> | undefined;
70
103
  }
71
104
  declare class LegacyStore<S extends SchemaRegistry = SchemaRegistry> {
105
+ #private;
72
106
  types: Record<string, string>;
73
107
  records: LegacyStoreRecordType[];
74
108
  relations: Record<string, Record<string, string>>;
@@ -82,6 +116,8 @@ declare class LegacyStore<S extends SchemaRegistry = SchemaRegistry> {
82
116
  setupRelations(links: LegacyLinks): void;
83
117
  findRecord(type: string, id: string): LegacyStoreRecordType | undefined;
84
118
  findRecords(type: string): LegacyStoreRecordType[];
119
+ static build(data: LegacyData): StoreModelWithOptionalId;
120
+ build(data: LegacyData): StoreModelWithOptionalId;
85
121
  /** @deprecated Use retrieve() instead. */
86
122
  retrive<T extends string>(type: T, data: LegacyData): InferModelType<S, T> | null;
87
123
  retrieve<T extends string>(type: T, data: LegacyData): InferModelType<S, T> | null;
@@ -1 +1 @@
1
- {"version":3,"file":"legacy.d.mts","names":[],"sources":["../../../../../../yayson/legacy-presenter.ts","../../../../../../yayson/legacy-store.ts","../../../../../../legacy.ts"],"mappings":";;;;UASU,qBAAA,SAA8B,eAAA;EAAA,CACrC,GAAA;AAAA;AAAA,iBAIqB,qBAAA,CAAsB,SAAA,EAAW,SAAA;EAAA,aASjC,eAAA;iBAbV;WAOK,qBAAA;;yBAsBM,SAAA,UAAmB,MAAA;gCA8BZ,qBAAA,EAAqB,QAAA,EAAY,SAAA;iCAqCrC,SAAA,GAAY,SAAA,uBAA8B,OAAA,GACtD,sBAAA,GACT,qBAAA;iCA6D0B,SAAA,GAAY,SAAA,YAAqB,qBAAA;;;;;;qBA/D1B;;;;;wCA+D0B;;;;;;;;;;;;;;;;;;;;+BAKtC,SAAA,GAAY,SAAA,WAAkB,OAAA,GAC1C,sBAAA,GACT,eAAA;+BAKqB,SAAA,GAAY,SAAA,WAAkB,QAAA,GACzC,sBAAA,GACV,eAAA;kBAAe,OAAA;AAAA;;;UC3KZ,qBAAA;EACR,IAAA;EACA,IAAA,EAAM,MAAA;IACJ,IAAA,GAAO,MAAA;IACP,KAAA,GAAQ,MAAA;EAAA;AAAA;AAAA,UAIF,WAAA;EAAA,CACP,GAAA;IACC,IAAA;EAAA;AAAA;AAAA,KAIC,eAAA,GAAkB,MAAA,oBAA0B,KAAA,CAAM,MAAA;AAAA,UAEtC,UAAA;EACf,KAAA,GAAQ,WAAA;EACR,IAAA,GAAO,MAAA;EAAA,CACN,GAAA,WAAc,eAAA,GAAkB,WAAA,GAAc,MAAA;AAAA;AAAA,cAG5B,WAAA,WAAsB,cAAA,GAAiB,cAAA;EAC1D,KAAA,EAAO,MAAA;EACP,OAAA,EAAS,qBAAA;EACT,SAAA,EAAW,MAAA,SAAe,M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AAU,aAAA,GAAgB,kBAAA"}
1
+ {"version":3,"file":"legacy.d.mts","names":[],"sources":["../../../../../../yayson/legacy-presenter.ts","../../../../../../yayson/legacy-store.ts","../../../../../../legacy.ts"],"mappings":";;;;UASU,qBAAA,SAA8B,eAAA;EAAA,CACrC,GAAA;AAAA;AAAA,iBAIqB,qBAAA,CAAsB,SAAA,EAAW,SAAA;EAAA,aASjC,eAAA;iBAbV;WAOK,qBAAA;;yBAqBM,SAAA,UAAmB,MAAA;gCA8BZ,qBAAA,EAAqB,QAAA,EAAY,SAAA;iCAsCrC,SAAA,GAAY,SAAA,uBAA8B,OAAA,GACtD,sBAAA,GACT,qBAAA;sBA6De,SAAA,GAAY,qBAAA;iCAWD,SAAA,GAAY,SAAA,YAAqB,qBAAA;;;;;;qBAzElD;;;;;wCAyE6B,oCAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;+BAKjB,SAAA,GAAY,SAAA,WAAkB,OAAA,GAC1C,sBAAA,GACT,qBAAA;+BAKqB,SAAA,GAAY,SAAA,WAAkB,QAAA,GACzC,sBAAA,GACV,qBAAA;oBAIsB,SAAA,GAAY,qBAAA;kBAAqB,OAAA;AAAA;;;UCzLpD,qBAAA;EACR,IAAA;EACA,IAAA,EAAM,MAAA;IACJ,EAAA;IACA,IAAA,GAAO,MAAA;IACP,KAAA,GAAQ,MAAA;EAAA;AAAA;AAAA,UAIF,WAAA;EAAA,CACP,GAAA;IACC,IAAA;EAAA;AAAA;AAAA,KAIC,eAAA,GAAkB,MAAA,oBAA0B,KAAA,CAAM,MAAA;AAAA,UAEtC,UAAA;EACf,KAAA,GAAQ,WAAA;EACR,IAAA,GAAO,MAAA;EAAA,CACN,GAAA,WAAc,eAAA,GAAkB,WAAA,GAAc,MAAA;AAAA;AAAA,cAO5B,WAAA,WAAsB,cAAA,GAAiB,cAAA;EAAA;EAC1D,KAAA,EAAO,MAAA;EACP,OAAA,EAAS,qBAAA;EACT,SAAA,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AA,SAAc,WAAA;EACd,SAAA,EAAW,UAAA,QAAkB,qBAAA;EAC7B,OAAA,EAAS,UAAA,QAAkB,QAAA;AAAA;AAAA,iBAGL,MAAA,CAAO,OAAA,GAAU,aAAA,GAAgB,kBAAA"}
package/build/legacy.mjs CHANGED
@@ -1,8 +1,8 @@
1
- import { a as TYPE, n as META, t as LINKS } from "./symbols-DSjKJ8vh.mjs";
2
- import { n as validate, r as filterByFields, t as yayson_default } from "./yayson-BJv2Z0Z6.mjs";
1
+ import { a as TYPE, n as META, t as LINKS } from "./symbols-BfU4k1el.mjs";
2
+ import { n as validate, r as filterByFields, t as yayson_default } from "./yayson-DIQ_olLX.mjs";
3
3
 
4
4
  //#region src/yayson/legacy-presenter.ts
5
- function hasId(value) {
5
+ function hasId$1(value) {
6
6
  return typeof value === "object" && value !== null && "id" in value;
7
7
  }
8
8
  function createLegacyPresenter(Presenter) {
@@ -27,8 +27,8 @@ function createLegacyPresenter(Presenter) {
27
27
  if (data == null) {
28
28
  id = attributes[key + "Id"];
29
29
  if (id != null) attributes[key] = id;
30
- } else if (Array.isArray(data)) attributes[key] = data.map((obj) => hasId(obj) ? obj.id : obj);
31
- else if (hasId(data)) attributes[key] = data.id;
30
+ } else if (Array.isArray(data)) attributes[key] = data.map((obj) => hasId$1(obj) ? obj.id : obj);
31
+ else if (hasId$1(data)) attributes[key] = data.id;
32
32
  }
33
33
  const relationshipKeys = relationships ? Object.keys(relationships) : [];
34
34
  return filterByFields(attributes, this.constructor.fields ? [...this.constructor.fields, ...relationshipKeys] : void 0);
@@ -39,9 +39,9 @@ function createLegacyPresenter(Presenter) {
39
39
  const result = [];
40
40
  if (!relationships) return result;
41
41
  for (const key in relationships) {
42
- const factory = relationships[key];
43
- if (!factory) throw new Error(`Presenter for ${key} in ${this.constructor.type} is not defined`);
44
- const presenter = new factory(scope);
42
+ const entry = relationships[key];
43
+ if (!entry) throw new Error(`Presenter for ${key} in ${this.constructor.type} is not defined`);
44
+ const presenter = new (typeof entry === "function" ? entry : entry.presenter)(scope);
45
45
  const data = this.constructor.adapter.get(instance, key);
46
46
  if (data != null) presenter.toJSON(data, { defaultPlural: true });
47
47
  const type = scope[this.pluralType()] != null ? this.pluralType() : this.constructor.type;
@@ -72,7 +72,7 @@ function createLegacyPresenter(Presenter) {
72
72
  }
73
73
  if (this.scope[this.constructor.type] && !this.scope[this.pluralType()]) {
74
74
  const existingValue = this.scope[this.constructor.type];
75
- if (attrs && hasId(existingValue) && existingValue.id !== attrs.id) {
75
+ if (attrs && hasId$1(existingValue) && existingValue.id !== attrs.id) {
76
76
  this.scope[this.pluralType()] = [this.scope[this.constructor.type]];
77
77
  delete this.scope[this.constructor.type];
78
78
  const pluralArray = this.scope[this.pluralType()];
@@ -80,7 +80,7 @@ function createLegacyPresenter(Presenter) {
80
80
  } else added = false;
81
81
  } else if (this.scope[this.pluralType()]) {
82
82
  const existing = this.scope[this.pluralType()];
83
- if (Array.isArray(existing)) if (attrs && !existing.some((i) => hasId(i) && i.id === attrs.id)) existing.push(attrs);
83
+ if (Array.isArray(existing)) if (attrs && !existing.some((i) => hasId$1(i) && i.id === attrs.id)) existing.push(attrs);
84
84
  else added = false;
85
85
  } else if (opts.defaultPlural) this.scope[this.pluralType()] = attrs ? [attrs] : [];
86
86
  else this.scope[this.constructor.type] = attrs;
@@ -88,6 +88,11 @@ function createLegacyPresenter(Presenter) {
88
88
  }
89
89
  return this.scope;
90
90
  }
91
+ payload(instance) {
92
+ if (Array.isArray(instance)) throw new Error("payload() expects a single resource, not an array");
93
+ if (instance == null) throw new Error("payload() requires a resource, got null");
94
+ return { [this.constructor.type]: this.attributes(instance) };
95
+ }
91
96
  render(instanceOrCollection) {
92
97
  return this.toJSON(instanceOrCollection);
93
98
  }
@@ -97,12 +102,18 @@ function createLegacyPresenter(Presenter) {
97
102
  static render(instanceOrCollection, _options) {
98
103
  return new this().render(instanceOrCollection);
99
104
  }
105
+ static payload(instance) {
106
+ return new this().payload(instance);
107
+ }
100
108
  };
101
109
  }
102
110
 
103
111
  //#endregion
104
112
  //#region src/yayson/legacy-store.ts
105
- var LegacyStore = class {
113
+ function hasId(model) {
114
+ return model.id != null;
115
+ }
116
+ var LegacyStore = class LegacyStore {
106
117
  types;
107
118
  records;
108
119
  relations;
@@ -125,33 +136,56 @@ var LegacyStore = class {
125
136
  this.validationErrors = [];
126
137
  this.models = {};
127
138
  }
128
- toModel(rec, type, models) {
129
- const idStr = String(rec.data.id);
130
- if (!models[type]) models[type] = {};
131
- if (models[type][idStr]) return models[type][idStr];
132
- const model = {
133
- ...rec.data,
134
- id: rec.data.id
135
- };
136
- model[TYPE] = type;
137
- if (rec.data.meta != null) model[META] = rec.data.meta;
138
- if (rec.data.links != null) model[LINKS] = rec.data.links;
139
- models[type][idStr] = model;
139
+ #createStub(type, id) {
140
+ const stub = { id };
141
+ stub[TYPE] = type;
142
+ return stub;
143
+ }
144
+ #resolveRelationships(model, type, resolver) {
140
145
  const relations = this.relations[type];
141
- if (relations) for (const attribute in relations) {
146
+ if (!relations) return;
147
+ for (const attribute in relations) {
142
148
  const relationType = relations[attribute];
143
149
  const value = model[attribute];
144
- model[attribute] = Array.isArray(value) ? value.map((id) => this.find(relationType, String(id), models)) : this.find(relationType, String(value), models);
150
+ if (Array.isArray(value)) model[attribute] = value.filter((id) => id != null).map((id) => resolver(relationType, String(id)));
151
+ else if (value != null) model[attribute] = resolver(relationType, String(value));
152
+ else model[attribute] = null;
153
+ }
154
+ }
155
+ #createModel(rec, type, options) {
156
+ const models = options?.models;
157
+ const model = { ...rec.data };
158
+ if (rec.data.id != null) model.id = rec.data.id;
159
+ model[TYPE] = type;
160
+ if (rec.data.meta != null) model[META] = rec.data.meta;
161
+ if (rec.data.links != null) model[LINKS] = rec.data.links;
162
+ if (models && hasId(model)) {
163
+ const idStr = String(model.id);
164
+ if (!models[type]) models[type] = {};
165
+ models[type][idStr] = model;
145
166
  }
167
+ const resolver = (relationType, id) => {
168
+ return this.#findModel(relationType, id, models ?? {}) ?? this.#createStub(relationType, id);
169
+ };
170
+ this.#resolveRelationships(model, type, resolver);
171
+ return model;
172
+ }
173
+ toModel(rec, type, models) {
174
+ const idStr = String(rec.data.id);
175
+ if (!models[type]) models[type] = {};
176
+ if (models[type][idStr]) return models[type][idStr];
177
+ const result = this.#createModel(rec, type, { models });
178
+ if (!hasId(result)) throw new Error(`Expected model of type ${type} to have an id`);
179
+ const model = result;
146
180
  if (this.schemas && this.schemas[type]) {
147
181
  const schema = this.schemas[type];
148
- const result = validate(schema, model, this.strict);
149
- if (!result.valid) this.validationErrors.push({
182
+ const result$1 = validate(schema, model, this.strict);
183
+ if (!result$1.valid) this.validationErrors.push({
150
184
  type,
151
185
  id: idStr,
152
- error: result.error
186
+ error: result$1.error
153
187
  });
154
- const validatedModel = result.data;
188
+ const validatedModel = result$1.data;
155
189
  validatedModel[TYPE] = model[TYPE];
156
190
  validatedModel[LINKS] = model[LINKS];
157
191
  validatedModel[META] = model[META];
@@ -177,6 +211,30 @@ var LegacyStore = class {
177
211
  findRecords(type) {
178
212
  return this.records.filter((r) => r.type === type);
179
213
  }
214
+ #findModel(type, id, models) {
215
+ const rec = this.findRecord(type, id);
216
+ if (rec == null) return null;
217
+ return models[type]?.[id] ?? this.toModel(rec, type, models);
218
+ }
219
+ static build(data) {
220
+ return new LegacyStore().build(data);
221
+ }
222
+ build(data) {
223
+ if (data.links) this.setupRelations(data.links);
224
+ let name;
225
+ for (const key in data) if (key !== "meta" && key !== "links") {
226
+ name = key;
227
+ break;
228
+ }
229
+ if (name == null) throw new Error("build() expects a single resource, not an array");
230
+ const value = data[name];
231
+ if (value == null || Array.isArray(value)) throw new Error("build() expects a single resource, not an array");
232
+ const type = this.types[name] || name;
233
+ return this.#createModel({
234
+ type,
235
+ data: value
236
+ }, type);
237
+ }
180
238
  /** @deprecated Use retrieve() instead. */
181
239
  retrive(type, data) {
182
240
  return this.retrieve(type, data);
@@ -190,12 +248,7 @@ var LegacyStore = class {
190
248
  return model;
191
249
  }
192
250
  find(type, id, models) {
193
- const modelsObj = models ?? this.models;
194
- const idStr = String(id);
195
- const rec = this.findRecord(type, idStr);
196
- if (rec == null) return null;
197
- if (!modelsObj[type]) modelsObj[type] = {};
198
- return modelsObj[type][idStr] || this.toModel(rec, type, modelsObj);
251
+ return this.#findModel(type, String(id), models ?? this.models);
199
252
  }
200
253
  findAll(type, models) {
201
254
  const modelsObj = models ?? this.models;
@@ -1 +1 @@
1
- {"version":3,"file":"legacy.mjs","names":["yaysonFactory"],"sources":["../src/yayson/legacy-presenter.ts","../src/yayson/legacy-store.ts","../src/legacy.ts"],"sourcesContent":["import type { ModelLike } from './adapter.js'\nimport type { Presenter } from './presenter.js'\nimport type { JsonApiDocument, JsonApiLinks, LegacyPresenterOptions } from './types.js'\nimport { filterByFields } from './utils.js'\n\nfunction hasId(value: unknown): value is { id: unknown } {\n return typeof value === 'object' && value !== null && 'id' in value\n}\n\ninterface LegacyJsonApiDocument extends JsonApiDocument {\n [key: string]: unknown\n}\n\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type -- Return type is inferred from class\nexport default function createLegacyPresenter(Presenter: Presenter) {\n return class LegacyPresenter extends Presenter {\n declare ['constructor']: typeof LegacyPresenter\n declare scope: LegacyJsonApiDocument\n\n static type = 'object'\n static plural?: string\n static fields?: string[]\n\n constructor(scope?: JsonApiDocument) {\n // LegacyPresenter doesn't use the 'data' property, so pass an empty scope\n const emptyScope: JsonApiDocument = { data: null }\n super(scope || emptyScope)\n // Remove the 'data' property that the parent constructor adds\n if (!scope) {\n // Legacy format doesn't include 'data' property\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Need Partial to allow delete\n delete (this.scope as Partial<JsonApiDocument>).data\n }\n }\n\n pluralType(): string {\n return this.constructor.plural || this.constructor.type + 's'\n }\n\n attributes(instance: ModelLike | null): Record<string, unknown> {\n if (!instance) {\n return {}\n }\n const attributes = { ...this.constructor.adapter.get(instance) }\n const relationships = this.relationships()\n if (relationships) {\n for (const key in relationships) {\n let id: unknown\n const data = attributes[key]\n if (data == null) {\n id = attributes[key + 'Id']\n if (id != null) {\n attributes[key] = id\n }\n } else if (Array.isArray(data)) {\n attributes[key] = data.map((obj: unknown) => (hasId(obj) ? obj.id : obj))\n } else if (hasId(data)) {\n attributes[key] = data.id\n }\n }\n }\n\n // Include relationship keys in allowed fields (so users don't need to list them twice)\n const relationshipKeys = relationships ? Object.keys(relationships) : []\n const allowedFields = this.constructor.fields ? [...this.constructor.fields, ...relationshipKeys] : undefined\n\n return filterByFields(attributes, allowedFields)\n }\n\n includeRelationships(scope: LegacyJsonApiDocument, instance: ModelLike): unknown[] {\n if (!scope.links) {\n scope.links = {}\n }\n const relationships = this.relationships()\n const result: unknown[] = []\n\n if (!relationships) {\n return result\n }\n\n for (const key in relationships) {\n const factory = relationships[key]\n if (!factory) throw new Error(`Presenter for ${key} in ${this.constructor.type} is not defined`)\n\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Factory returns PresenterInstance, we know it's LegacyPresenter\n const presenter = new factory(scope) as LegacyPresenter\n\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- unknown from adapter.get\n const data = this.constructor.adapter.get(instance, key) as ModelLike | ModelLike[] | null\n if (data != null) {\n presenter.toJSON(data, { defaultPlural: true })\n }\n\n const type = scope[this.pluralType()] != null ? this.pluralType() : this.constructor.type\n const presenterType = presenter.constructor.type\n const keyName = scope[presenter.pluralType()] != null ? presenter.pluralType() : presenterType\n const link = { type: keyName }\n if (scope.links) {\n scope.links[`${type}.${key}`] = link\n }\n result.push(link)\n }\n return result\n }\n\n toJSON(\n instanceOrCollection: ModelLike | ModelLike[] | null | undefined,\n options?: LegacyPresenterOptions,\n ): LegacyJsonApiDocument {\n const opts = options ?? {}\n if (!this.scope.links) {\n this.scope.links = {}\n }\n if (Array.isArray(instanceOrCollection)) {\n const collection = instanceOrCollection\n const type = this.pluralType()\n if (!this.scope[type]) {\n this.scope[type] = []\n }\n collection.forEach((instance: ModelLike) => {\n return this.toJSON(instance)\n })\n } else {\n let links: JsonApiLinks | undefined\n const instance = instanceOrCollection\n let added = true\n const attrs = instance ? this.attributes(instance) : null\n if ((links = this.links())) {\n if (attrs) {\n attrs.links = links\n }\n }\n // If eg x.image already exists\n if (this.scope[this.constructor.type] && !this.scope[this.pluralType()]) {\n const existingValue = this.scope[this.constructor.type]\n if (attrs && hasId(existingValue) && existingValue.id !== attrs.id) {\n this.scope[this.pluralType()] = [this.scope[this.constructor.type]]\n delete this.scope[this.constructor.type]\n const pluralArray = this.scope[this.pluralType()]\n if (Array.isArray(pluralArray)) {\n pluralArray.push(attrs)\n }\n } else {\n added = false\n }\n\n // If eg x.images already exists\n } else if (this.scope[this.pluralType()]) {\n const existing = this.scope[this.pluralType()]\n if (Array.isArray(existing)) {\n if (attrs && !existing.some((i) => hasId(i) && i.id === attrs.id)) {\n existing.push(attrs)\n } else {\n added = false\n }\n }\n } else if (opts.defaultPlural) {\n this.scope[this.pluralType()] = attrs ? [attrs] : []\n } else {\n this.scope[this.constructor.type] = attrs\n }\n\n if (added && instance) {\n this.includeRelationships(this.scope, instance)\n }\n }\n return this.scope\n }\n\n render(instanceOrCollection: ModelLike | ModelLike[] | null): LegacyJsonApiDocument {\n return this.toJSON(instanceOrCollection)\n }\n\n static toJSON(\n instanceOrCollection: ModelLike | ModelLike[] | null,\n options?: LegacyPresenterOptions,\n ): JsonApiDocument {\n return new this().toJSON(instanceOrCollection, options)\n }\n\n static render(\n instanceOrCollection: ModelLike | ModelLike[] | null,\n _options?: LegacyPresenterOptions,\n ): JsonApiDocument {\n return new this().render(instanceOrCollection)\n }\n }\n}\n","import type {\n StoreModel,\n StoreModels,\n StoreResult,\n SchemaRegistry,\n ValidationError,\n InferModelType,\n LegacyStoreOptions,\n} from './types.js'\nimport { TYPE, LINKS, META } from './symbols.js'\nimport { validate } from './schema.js'\n\ninterface LegacyStoreRecordType {\n type: string\n data: Record<string, unknown> & {\n meta?: Record<string, unknown>\n links?: Record<string, unknown>\n }\n}\n\ninterface LegacyLinks {\n [key: string]: {\n type: string\n }\n}\n\ntype LegacyDataValue = Record<string, unknown> | Array<Record<string, unknown>>\n\nexport interface LegacyData {\n links?: LegacyLinks\n meta?: Record<string, unknown>\n [key: string]: LegacyDataValue | LegacyLinks | Record<string, unknown> | undefined\n}\n\nexport default class LegacyStore<S extends SchemaRegistry = SchemaRegistry> {\n types: Record<string, string>\n records: LegacyStoreRecordType[]\n relations: Record<string, Record<string, string>>\n schemas?: S\n strict: boolean\n validationErrors: ValidationError[]\n models: StoreModels\n\n constructor(options?: LegacyStoreOptions<S>) {\n this.types = options?.types || {}\n this.schemas = options?.schemas\n this.strict = options?.strict ?? false\n this.records = []\n this.relations = {}\n this.validationErrors = []\n this.models = {}\n }\n\n reset(): void {\n this.records = []\n this.relations = {}\n this.validationErrors = []\n this.models = {}\n }\n\n toModel(rec: LegacyStoreRecordType, type: string, models: StoreModels): StoreModel {\n // Keep original id type from data, but ensure string key for models lookup\n const idStr = String(rec.data.id)\n\n if (!models[type]) {\n models[type] = {}\n }\n\n // Return cached model if already built (prevents double validation)\n if (models[type]![idStr]) {\n return models[type]![idStr]!\n }\n\n // Don't add 'type' to model - preserve original data shape for backwards compatibility\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Legacy format: rec.data has id, spread creates StoreModel-compatible object\n const model: StoreModel = { ...rec.data, id: rec.data.id as string }\n model[TYPE] = type\n if (rec.data.meta != null) {\n model[META] = rec.data.meta\n }\n if (rec.data.links != null) {\n model[LINKS] = rec.data.links\n }\n\n // Store placeholder to handle circular relations\n models[type]![idStr] = model\n\n const relations = this.relations[type]\n if (relations) {\n for (const attribute in relations) {\n const relationType = relations[attribute]!\n const value = model[attribute]\n model[attribute] = Array.isArray(value)\n ? value.map((id: unknown) => this.find(relationType, String(id), models))\n : this.find(relationType, String(value), models)\n }\n }\n\n // Validate with schema if provided\n if (this.schemas && this.schemas[type]) {\n const schema = this.schemas[type]\n const result = validate(schema, model, this.strict)\n\n if (!result.valid) {\n this.validationErrors.push({\n type,\n id: idStr,\n error: result.error,\n })\n }\n\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Schema validation returns unknown, cast to StoreModel after validation\n const validatedModel = result.data as StoreModel\n\n // Preserve symbol keys from original model (schema validation may not preserve them)\n validatedModel[TYPE] = model[TYPE]\n validatedModel[LINKS] = model[LINKS]\n validatedModel[META] = model[META]\n\n models[type]![idStr] = validatedModel\n return validatedModel\n }\n\n return model\n }\n\n setupRelations(links: LegacyLinks): void {\n for (const key in links) {\n const value = links[key]!\n const parts = key.split('.')\n const typeRaw = parts[0]!\n const attribute = parts[1]!\n const type = this.types[typeRaw] || typeRaw\n if (!this.relations[type]) {\n this.relations[type] = {}\n }\n this.relations[type]![attribute] = this.types[value.type] || value.type\n }\n }\n\n findRecord(type: string, id: string): LegacyStoreRecordType | undefined {\n return this.records.find((r) => r.type === type && String(r.data.id) === id)\n }\n\n findRecords(type: string): LegacyStoreRecordType[] {\n return this.records.filter((r) => r.type === type)\n }\n\n /** @deprecated Use retrieve() instead. */\n retrive<T extends string>(type: T, data: LegacyData): InferModelType<S, T> | null {\n return this.retrieve(type, data)\n }\n\n retrieve<T extends string>(type: T, data: LegacyData): InferModelType<S, T> | null {\n const synced = this.syncAll(data)\n const normalizedType = this.types[type] || type\n const model = synced.find((m) => m[TYPE] === normalizedType)\n if (!model) return null\n if (synced[META]) {\n model[META] = synced[META]\n }\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Enable type inference from type parameter\n return model as InferModelType<S, T>\n }\n\n find<T extends string>(type: T, id: string | number, models?: StoreModels): InferModelType<S, T> | null {\n const modelsObj = models ?? this.models\n const idStr = String(id)\n const rec = this.findRecord(type, idStr)\n if (rec == null) {\n return null\n }\n if (!modelsObj[type]) {\n modelsObj[type] = {}\n }\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Type inference: maps string literal type parameter to schema type\n return (modelsObj[type]![idStr] || this.toModel(rec, type, modelsObj)) as InferModelType<S, T>\n }\n\n findAll<T extends string>(type: T, models?: StoreModels): InferModelType<S, T>[] {\n const modelsObj = models ?? this.models\n const recs = this.findRecords(type)\n if (recs.length === 0) {\n return []\n }\n recs.forEach((rec) => {\n if (!modelsObj[type]) {\n modelsObj[type] = {}\n }\n return this.toModel(rec, type, modelsObj)\n })\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Type inference: maps string literal type parameter to schema type\n return Object.values(modelsObj[type] || {}) as InferModelType<S, T>[]\n }\n\n remove(type: string, id?: string | number): void {\n const normalizedType = this.types[type] || type\n\n const removeOne = (record: LegacyStoreRecordType): void => {\n const index = this.records.indexOf(record)\n if (!(index < 0)) {\n this.records.splice(index, 1)\n }\n }\n\n if (id != null) {\n const idStr = String(id)\n const record = this.findRecord(normalizedType, idStr)\n if (record) {\n removeOne(record)\n }\n } else {\n const records = this.findRecords(normalizedType)\n records.forEach(removeOne)\n }\n }\n\n syncAll(data: LegacyData): StoreResult {\n // Clear validation errors and models cache from previous sync\n this.validationErrors = []\n this.models = {}\n\n if (data.links) {\n this.setupRelations(data.links)\n }\n\n // Track records added in this sync\n const syncedRecords: LegacyStoreRecordType[] = []\n\n for (const name in data) {\n if (name === 'meta' || name === 'links') {\n continue\n }\n\n const value = data[name]!\n const type = this.types[name] || name\n\n const add = (d: Record<string, unknown>): void => {\n this.remove(type, String(d.id))\n const rec: LegacyStoreRecordType = { type, data: d }\n this.records.push(rec)\n syncedRecords.push(rec)\n }\n\n if (Array.isArray(value)) {\n value.forEach(add)\n } else if (typeof value === 'object' && value !== null) {\n add(value)\n }\n }\n\n // Build models for all synced records\n for (const rec of syncedRecords) {\n this.toModel(rec, rec.type, this.models)\n }\n\n const result: StoreResult = syncedRecords.map((rec) => this.models[rec.type]![String(rec.data.id)]!)\n if (data.meta != null) {\n result[META] = data.meta\n }\n return result\n }\n\n sync(data: LegacyData): StoreModel | StoreResult {\n const result = this.syncAll(data)\n if (result.length === 1) {\n const model = result[0]\n if (result[META]) {\n model[META] = result[META]\n }\n return model\n }\n return result\n }\n\n retrieveAll<T extends string>(type: T, data: LegacyData): StoreResult<InferModelType<S, T>> {\n const normalizedType = this.types[type] || type\n const synced = this.syncAll(data)\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Enable type inference from type parameter\n const result: StoreResult<InferModelType<S, T>> = synced.filter(\n (model) => model[TYPE] === normalizedType,\n ) as StoreResult<InferModelType<S, T>>\n result[META] = synced[META]\n return result\n }\n}\n","import yaysonFactory from './yayson.js'\nimport createLegacyPresenter from './yayson/legacy-presenter.js'\nimport LegacyStore from './yayson/legacy-store.js'\nimport type { LegacyData } from './yayson/legacy-store.js'\nimport Adapter from './yayson/adapter.js'\nimport type {\n JsonApiDocument,\n JsonApiLink,\n JsonApiLinks,\n JsonApiRelationship,\n JsonApiRelationships,\n JsonApiResource,\n} from './yayson/types.js'\nimport type { YaysonOptions } from './yayson.js'\n\nexport type {\n Adapter,\n JsonApiDocument,\n JsonApiLink,\n JsonApiLinks,\n JsonApiRelationship,\n JsonApiRelationships,\n JsonApiResource,\n LegacyData,\n}\n\ninterface LegacyYaysonResult {\n Store: typeof LegacyStore\n Presenter: ReturnType<typeof createLegacyPresenter>\n Adapter: ReturnType<typeof yaysonFactory>['Adapter']\n}\n\nexport default function yayson(options?: YaysonOptions): LegacyYaysonResult {\n const { Presenter, Adapter } = yaysonFactory(options)\n return {\n Store: LegacyStore,\n Presenter: createLegacyPresenter(Presenter),\n Adapter,\n }\n}\n"],"mappings":";;;;AAKA,SAAS,MAAM,OAA0C;AACvD,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,QAAQ;;AAQhE,SAAwB,sBAAsB,WAAsB;AAClE,QAAO,MAAM,wBAAwB,UAAU;EAI7C,OAAO,OAAO;EACd,OAAO;EACP,OAAO;EAEP,YAAY,OAAyB;AAGnC,SAAM,SAD8B,EAAE,MAAM,MAAM,CACxB;AAE1B,OAAI,CAAC,MAGH,QAAQ,KAAK,MAAmC;;EAIp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,UAAU,MAAM,UAAU,eAC5B;AACD,SAAO,QAAQ,OAAO;AACtB,SAAO;;;;;;AC3PX,SAAwB,OAAO,SAA6C;CAC1E,MAAM,EAAE,WAAW,YAAYA,eAAc,QAAQ;AACrD,QAAO;EACL,OAAO;EACP,WAAW,sBAAsB,UAAU;EAC3C;EACD"}
1
+ {"version":3,"file":"legacy.mjs","names":["hasId","#findModel","#createStub","#resolveRelationships","#createModel","result","yaysonFactory"],"sources":["../src/yayson/legacy-presenter.ts","../src/yayson/legacy-store.ts","../src/legacy.ts"],"sourcesContent":["import type { ModelLike } from './adapter.js'\nimport type { Presenter } from './presenter.js'\nimport type { JsonApiDocument, JsonApiLinks, LegacyPresenterOptions } from './types.js'\nimport { filterByFields } from './utils.js'\n\nfunction hasId(value: unknown): value is { id: unknown } {\n return typeof value === 'object' && value !== null && 'id' in value\n}\n\ninterface LegacyJsonApiDocument extends JsonApiDocument {\n [key: string]: unknown\n}\n\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type -- Return type is inferred from class\nexport default function createLegacyPresenter(Presenter: Presenter) {\n return class LegacyPresenter extends Presenter {\n declare ['constructor']: typeof LegacyPresenter\n declare scope: LegacyJsonApiDocument\n\n static type = 'object'\n static plural?: string\n static fields?: string[]\n\n constructor(scope?: JsonApiDocument) {\n // LegacyPresenter doesn't use the 'data' property, so pass an empty scope\n const emptyScope: JsonApiDocument = { data: null }\n super(scope || emptyScope)\n // Remove the 'data' property that the parent constructor adds\n if (!scope) {\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Need Partial to allow delete\n delete (this.scope as Partial<JsonApiDocument>).data\n }\n }\n\n pluralType(): string {\n return this.constructor.plural || this.constructor.type + 's'\n }\n\n attributes(instance: ModelLike | null): Record<string, unknown> {\n if (!instance) {\n return {}\n }\n const attributes = { ...this.constructor.adapter.get(instance) }\n const relationships = this.relationships()\n if (relationships) {\n for (const key in relationships) {\n let id: unknown\n const data = attributes[key]\n if (data == null) {\n id = attributes[key + 'Id']\n if (id != null) {\n attributes[key] = id\n }\n } else if (Array.isArray(data)) {\n attributes[key] = data.map((obj: unknown) => (hasId(obj) ? obj.id : obj))\n } else if (hasId(data)) {\n attributes[key] = data.id\n }\n }\n }\n\n // Include relationship keys in allowed fields (so users don't need to list them twice)\n const relationshipKeys = relationships ? Object.keys(relationships) : []\n const allowedFields = this.constructor.fields ? [...this.constructor.fields, ...relationshipKeys] : undefined\n\n return filterByFields(attributes, allowedFields)\n }\n\n includeRelationships(scope: LegacyJsonApiDocument, instance: ModelLike): unknown[] {\n if (!scope.links) {\n scope.links = {}\n }\n const relationships = this.relationships()\n const result: unknown[] = []\n\n if (!relationships) {\n return result\n }\n\n for (const key in relationships) {\n const entry = relationships[key]\n if (!entry) throw new Error(`Presenter for ${key} in ${this.constructor.type} is not defined`)\n const factory = typeof entry === 'function' ? entry : entry.presenter\n\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Factory returns PresenterInstance, we know it's LegacyPresenter\n const presenter = new factory(scope) as LegacyPresenter\n\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- unknown from adapter.get\n const data = this.constructor.adapter.get(instance, key) as ModelLike | ModelLike[] | null\n if (data != null) {\n presenter.toJSON(data, { defaultPlural: true })\n }\n\n const type = scope[this.pluralType()] != null ? this.pluralType() : this.constructor.type\n const presenterType = presenter.constructor.type\n const keyName = scope[presenter.pluralType()] != null ? presenter.pluralType() : presenterType\n const link = { type: keyName }\n if (scope.links) {\n scope.links[`${type}.${key}`] = link\n }\n result.push(link)\n }\n return result\n }\n\n toJSON(\n instanceOrCollection: ModelLike | ModelLike[] | null | undefined,\n options?: LegacyPresenterOptions,\n ): LegacyJsonApiDocument {\n const opts = options ?? {}\n if (!this.scope.links) {\n this.scope.links = {}\n }\n if (Array.isArray(instanceOrCollection)) {\n const collection = instanceOrCollection\n const type = this.pluralType()\n if (!this.scope[type]) {\n this.scope[type] = []\n }\n collection.forEach((instance: ModelLike) => {\n return this.toJSON(instance)\n })\n } else {\n let links: JsonApiLinks | undefined\n const instance = instanceOrCollection\n let added = true\n const attrs = instance ? this.attributes(instance) : null\n if ((links = this.links())) {\n if (attrs) {\n attrs.links = links\n }\n }\n // If eg x.image already exists\n if (this.scope[this.constructor.type] && !this.scope[this.pluralType()]) {\n const existingValue = this.scope[this.constructor.type]\n if (attrs && hasId(existingValue) && existingValue.id !== attrs.id) {\n this.scope[this.pluralType()] = [this.scope[this.constructor.type]]\n delete this.scope[this.constructor.type]\n const pluralArray = this.scope[this.pluralType()]\n if (Array.isArray(pluralArray)) {\n pluralArray.push(attrs)\n }\n } else {\n added = false\n }\n\n // If eg x.images already exists\n } else if (this.scope[this.pluralType()]) {\n const existing = this.scope[this.pluralType()]\n if (Array.isArray(existing)) {\n if (attrs && !existing.some((i) => hasId(i) && i.id === attrs.id)) {\n existing.push(attrs)\n } else {\n added = false\n }\n }\n } else if (opts.defaultPlural) {\n this.scope[this.pluralType()] = attrs ? [attrs] : []\n } else {\n this.scope[this.constructor.type] = attrs\n }\n\n if (added && instance) {\n this.includeRelationships(this.scope, instance)\n }\n }\n return this.scope\n }\n\n payload(instance: ModelLike): LegacyJsonApiDocument {\n if (Array.isArray(instance)) {\n throw new Error('payload() expects a single resource, not an array')\n }\n if (instance == null) {\n throw new Error('payload() requires a resource, got null')\n }\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Legacy payload intentionally omits `data`\n return { [this.constructor.type]: this.attributes(instance) } as LegacyJsonApiDocument\n }\n\n render(instanceOrCollection: ModelLike | ModelLike[] | null): LegacyJsonApiDocument {\n return this.toJSON(instanceOrCollection)\n }\n\n static toJSON(\n instanceOrCollection: ModelLike | ModelLike[] | null,\n options?: LegacyPresenterOptions,\n ): LegacyJsonApiDocument {\n return new this().toJSON(instanceOrCollection, options)\n }\n\n static render(\n instanceOrCollection: ModelLike | ModelLike[] | null,\n _options?: LegacyPresenterOptions,\n ): LegacyJsonApiDocument {\n return new this().render(instanceOrCollection)\n }\n\n static payload(instance: ModelLike): LegacyJsonApiDocument {\n return new this().payload(instance)\n }\n }\n}\n","import type {\n StoreModel,\n StoreModelWithOptionalId,\n StoreModels,\n StoreResult,\n SchemaRegistry,\n ValidationError,\n InferModelType,\n LegacyStoreOptions,\n} from './types.js'\nimport { TYPE, LINKS, META } from './symbols.js'\nimport { validate } from './schema.js'\n\ninterface LegacyStoreRecordType {\n type: string\n data: Record<string, unknown> & {\n id?: string | number\n meta?: Record<string, unknown>\n links?: Record<string, unknown>\n }\n}\n\ninterface LegacyLinks {\n [key: string]: {\n type: string\n }\n}\n\ntype LegacyDataValue = Record<string, unknown> | Array<Record<string, unknown>>\n\nexport interface LegacyData {\n links?: LegacyLinks\n meta?: Record<string, unknown>\n [key: string]: LegacyDataValue | LegacyLinks | Record<string, unknown> | undefined\n}\n\nfunction hasId<T extends StoreModelWithOptionalId>(model: T): model is T & { id: string | number } {\n return model.id != null\n}\n\nexport default class LegacyStore<S extends SchemaRegistry = SchemaRegistry> {\n types: Record<string, string>\n records: LegacyStoreRecordType[]\n relations: Record<string, Record<string, string>>\n schemas?: S\n strict: boolean\n validationErrors: ValidationError[]\n models: StoreModels\n\n constructor(options?: LegacyStoreOptions<S>) {\n this.types = options?.types || {}\n this.schemas = options?.schemas\n this.strict = options?.strict ?? false\n this.records = []\n this.relations = {}\n this.validationErrors = []\n this.models = {}\n }\n\n reset(): void {\n this.records = []\n this.relations = {}\n this.validationErrors = []\n this.models = {}\n }\n\n #createStub(type: string, id: string): StoreModel {\n const stub: StoreModel = { id }\n stub[TYPE] = type\n return stub\n }\n\n #resolveRelationships(\n model: StoreModel | StoreModelWithOptionalId,\n type: string,\n resolver: (relationType: string, id: string) => StoreModel,\n ): void {\n const relations = this.relations[type]\n if (!relations) return\n\n for (const attribute in relations) {\n const relationType = relations[attribute]!\n const value = model[attribute]\n if (Array.isArray(value)) {\n model[attribute] = value\n .filter((id: unknown) => id != null)\n .map((id: unknown) => resolver(relationType, String(id)))\n } else if (value != null) {\n model[attribute] = resolver(relationType, String(value))\n } else {\n model[attribute] = null\n }\n }\n }\n\n #createModel(rec: LegacyStoreRecordType, type: string, options?: { models?: StoreModels }): StoreModelWithOptionalId {\n const models = options?.models\n\n const model: StoreModelWithOptionalId = { ...rec.data }\n if (rec.data.id != null) {\n model.id = rec.data.id\n }\n model[TYPE] = type\n if (rec.data.meta != null) {\n model[META] = rec.data.meta\n }\n if (rec.data.links != null) {\n model[LINKS] = rec.data.links\n }\n\n if (models && hasId(model)) {\n const idStr = String(model.id)\n if (!models[type]) {\n models[type] = {}\n }\n models[type]![idStr] = model\n }\n\n const resolver = (relationType: string, id: string): StoreModel => {\n return this.#findModel(relationType, id, models ?? {}) ?? this.#createStub(relationType, id)\n }\n this.#resolveRelationships(model, type, resolver)\n\n return model\n }\n\n toModel(rec: LegacyStoreRecordType, type: string, models: StoreModels): StoreModel {\n const idStr = String(rec.data.id)\n\n if (!models[type]) {\n models[type] = {}\n }\n\n if (models[type]![idStr]) {\n return models[type]![idStr]!\n }\n\n const result = this.#createModel(rec, type, { models })\n if (!hasId(result)) {\n throw new Error(`Expected model of type ${type} to have an id`)\n }\n const model = result\n\n if (this.schemas && this.schemas[type]) {\n const schema = this.schemas[type]\n const result = validate(schema, model, this.strict)\n\n if (!result.valid) {\n this.validationErrors.push({\n type,\n id: idStr,\n error: result.error,\n })\n }\n\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Schema validation returns unknown, cast to StoreModel after validation\n const validatedModel = result.data as StoreModel\n\n // Preserve symbol keys from original model (schema validation may not preserve them)\n validatedModel[TYPE] = model[TYPE]\n validatedModel[LINKS] = model[LINKS]\n validatedModel[META] = model[META]\n\n models[type]![idStr] = validatedModel\n return validatedModel\n }\n\n return model\n }\n\n setupRelations(links: LegacyLinks): void {\n for (const key in links) {\n const value = links[key]!\n const parts = key.split('.')\n const typeRaw = parts[0]!\n const attribute = parts[1]!\n const type = this.types[typeRaw] || typeRaw\n if (!this.relations[type]) {\n this.relations[type] = {}\n }\n this.relations[type]![attribute] = this.types[value.type] || value.type\n }\n }\n\n findRecord(type: string, id: string): LegacyStoreRecordType | undefined {\n return this.records.find((r) => r.type === type && String(r.data.id) === id)\n }\n\n findRecords(type: string): LegacyStoreRecordType[] {\n return this.records.filter((r) => r.type === type)\n }\n\n #findModel(type: string, id: string, models: StoreModels): StoreModel | null {\n const rec = this.findRecord(type, id)\n if (rec == null) {\n return null\n }\n return models[type]?.[id] ?? this.toModel(rec, type, models)\n }\n\n static build(data: LegacyData): StoreModelWithOptionalId {\n return new LegacyStore().build(data)\n }\n\n build(data: LegacyData): StoreModelWithOptionalId {\n if (data.links) {\n this.setupRelations(data.links)\n }\n\n let name: string | undefined\n for (const key in data) {\n if (key !== 'meta' && key !== 'links') {\n name = key\n break\n }\n }\n\n if (name == null) {\n throw new Error('build() expects a single resource, not an array')\n }\n\n const value = data[name]\n if (value == null || Array.isArray(value)) {\n throw new Error('build() expects a single resource, not an array')\n }\n\n const type = this.types[name] || name\n return this.#createModel({ type, data: value }, type)\n }\n\n /** @deprecated Use retrieve() instead. */\n retrive<T extends string>(type: T, data: LegacyData): InferModelType<S, T> | null {\n return this.retrieve(type, data)\n }\n\n retrieve<T extends string>(type: T, data: LegacyData): InferModelType<S, T> | null {\n const synced = this.syncAll(data)\n const normalizedType = this.types[type] || type\n const model = synced.find((m) => m[TYPE] === normalizedType)\n if (!model) return null\n if (synced[META]) {\n model[META] = synced[META]\n }\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Enable type inference from type parameter\n return model as InferModelType<S, T>\n }\n\n find<T extends string>(type: T, id: string | number, models?: StoreModels): InferModelType<S, T> | null {\n const result = this.#findModel(type, String(id), models ?? this.models)\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Type inference: maps string literal type parameter to schema type\n return result as InferModelType<S, T> | null\n }\n\n findAll<T extends string>(type: T, models?: StoreModels): InferModelType<S, T>[] {\n const modelsObj = models ?? this.models\n const recs = this.findRecords(type)\n if (recs.length === 0) {\n return []\n }\n recs.forEach((rec) => {\n if (!modelsObj[type]) {\n modelsObj[type] = {}\n }\n return this.toModel(rec, type, modelsObj)\n })\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Type inference: maps string literal type parameter to schema type\n return Object.values(modelsObj[type] || {}) as InferModelType<S, T>[]\n }\n\n remove(type: string, id?: string | number): void {\n const normalizedType = this.types[type] || type\n\n const removeOne = (record: LegacyStoreRecordType): void => {\n const index = this.records.indexOf(record)\n if (!(index < 0)) {\n this.records.splice(index, 1)\n }\n }\n\n if (id != null) {\n const idStr = String(id)\n const record = this.findRecord(normalizedType, idStr)\n if (record) {\n removeOne(record)\n }\n } else {\n const records = this.findRecords(normalizedType)\n records.forEach(removeOne)\n }\n }\n\n syncAll(data: LegacyData): StoreResult {\n // Clear validation errors and models cache from previous sync\n this.validationErrors = []\n this.models = {}\n\n if (data.links) {\n this.setupRelations(data.links)\n }\n\n const syncedRecords: LegacyStoreRecordType[] = []\n\n for (const name in data) {\n if (name === 'meta' || name === 'links') {\n continue\n }\n\n const value = data[name]!\n const type = this.types[name] || name\n\n const add = (d: Record<string, unknown>): void => {\n this.remove(type, String(d.id))\n const rec: LegacyStoreRecordType = { type, data: d }\n this.records.push(rec)\n syncedRecords.push(rec)\n }\n\n if (Array.isArray(value)) {\n value.forEach(add)\n } else if (typeof value === 'object' && value !== null) {\n add(value)\n }\n }\n\n for (const rec of syncedRecords) {\n this.toModel(rec, rec.type, this.models)\n }\n\n const result: StoreResult = syncedRecords.map((rec) => this.models[rec.type]![String(rec.data.id)]!)\n if (data.meta != null) {\n result[META] = data.meta\n }\n return result\n }\n\n sync(data: LegacyData): StoreModel | StoreResult {\n const result = this.syncAll(data)\n if (result.length === 1) {\n const model = result[0]\n if (result[META]) {\n model[META] = result[META]\n }\n return model\n }\n return result\n }\n\n retrieveAll<T extends string>(type: T, data: LegacyData): StoreResult<InferModelType<S, T>> {\n const normalizedType = this.types[type] || type\n const synced = this.syncAll(data)\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Enable type inference from type parameter\n const result: StoreResult<InferModelType<S, T>> = synced.filter(\n (model) => model[TYPE] === normalizedType,\n ) as StoreResult<InferModelType<S, T>>\n result[META] = synced[META]\n return result\n }\n}\n","import yaysonFactory from './yayson.js'\nimport createLegacyPresenter from './yayson/legacy-presenter.js'\nimport LegacyStore from './yayson/legacy-store.js'\nimport type { LegacyData } from './yayson/legacy-store.js'\nimport Adapter from './yayson/adapter.js'\nimport type {\n JsonApiDocument,\n JsonApiLink,\n JsonApiLinks,\n JsonApiRelationship,\n JsonApiRelationships,\n JsonApiResource,\n} from './yayson/types.js'\nimport type { YaysonOptions } from './yayson.js'\n\nexport type {\n Adapter,\n JsonApiDocument,\n JsonApiLink,\n JsonApiLinks,\n JsonApiRelationship,\n JsonApiRelationships,\n JsonApiResource,\n LegacyData,\n}\n\ninterface LegacyYaysonResult {\n Store: typeof LegacyStore\n Presenter: ReturnType<typeof createLegacyPresenter>\n Adapter: ReturnType<typeof yaysonFactory>['Adapter']\n}\n\nexport default function yayson(options?: YaysonOptions): LegacyYaysonResult {\n const { Presenter, Adapter } = yaysonFactory(options)\n return {\n Store: LegacyStore,\n Presenter: createLegacyPresenter(Presenter),\n Adapter,\n }\n}\n"],"mappings":";;;;AAKA,SAASA,QAAM,OAA0C;AACvD,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,QAAQ;;AAQhE,SAAwB,sBAAsB,WAAsB;AAClE,QAAO,MAAM,wBAAwB,UAAU;EAI7C,OAAO,OAAO;EACd,OAAO;EACP,OAAO;EAEP,YAAY,OAAyB;AAGnC,SAAM,SAD8B,EAAE,MAAM,MAAM,CACxB;AAE1B,OAAI,CAAC,MAEH,QAAQ,KAAK,MAAmC;;EAIpD,aAAqB;AACnB,UAAO,KAAK,YAAY,UAAU,KAAK,YAAY,OAAO;;EAG5D,WAAW,UAAqD;AAC9D,OAAI,CAAC,SACH,QAAO,EAAE;GAEX,MAAM,aAAa,EAAE,GAAG,K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,OAAO;AACrB,OAAI,OAAO,MACT,OAAM,QAAQ,OAAO;AAEvB,UAAO;;AAET,SAAO;;CAGT,YAA8B,MAAS,MAAqD;EAC1F,MAAM,iBAAiB,KAAK,MAAM,SAAS;EAC3C,MAAM,SAAS,KAAK,QAAQ,KAAK;EAEjC,MAAM,SAA4C,OAAO,QACtD,UAAU,MAAM,UAAU,eAC5B;AACD,SAAO,QAAQ,OAAO;AACtB,SAAO;;;;;;ACnUX,SAAwB,OAAO,SAA6C;CAC1E,MAAM,EAAE,WAAW,YAAYK,eAAc,QAAQ;AACrD,QAAO;EACL,OAAO;EACP,WAAW,sBAAsB,UAAU;EAC3C;EACD"}
@@ -11,6 +11,9 @@ var Adapter = class {
11
11
  if (id == null) return;
12
12
  return `${id}`;
13
13
  }
14
+ static has(model, key) {
15
+ return key in model;
16
+ }
14
17
  };
15
18
  var adapter_default = Adapter;
16
19
 
@@ -10,6 +10,9 @@ var Adapter = class {
10
10
  if (id == null) return;
11
11
  return `${id}`;
12
12
  }
13
+ static has(model, key) {
14
+ return key in model;
15
+ }
13
16
  };
14
17
  var adapter_default = Adapter;
15
18
 
@@ -23,4 +26,4 @@ const REL_META = Symbol("yayson.rel-meta");
23
26
 
24
27
  //#endregion
25
28
  export { TYPE as a, REL_META as i, META as n, adapter_default as o, REL_LINKS as r, LINKS as t };
26
- //# sourceMappingURL=symbols-DSjKJ8vh.mjs.map
29
+ //# sourceMappingURL=symbols-BfU4k1el.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"symbols-BfU4k1el.mjs","names":[],"sources":["../src/yayson/adapter.ts","../src/yayson/symbols.ts"],"sourcesContent":["export type ModelLike = object\n\nclass Adapter {\n static get(model: ModelLike): Record<string, unknown>\n static get(model: ModelLike, key: string): unknown\n static get(model: ModelLike, key?: string): unknown {\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- ModelLike is object for flexibility, cast for dynamic access\n const obj = model as Record<string, unknown>\n if (key) {\n return obj[key]\n }\n return obj\n }\n\n static id(model: ModelLike): string | undefined {\n const id = this.get(model, 'id')\n if (id == null) {\n return undefined\n }\n return `${id}`\n }\n\n static has(model: ModelLike, key: string): boolean {\n return key in model\n }\n}\n\nexport default Adapter\n","export const TYPE: unique symbol = Symbol('yayson.type')\nexport const LINKS: unique symbol = Symbol('yayson.links')\nexport const META: unique symbol = Symbol('yayson.meta')\nexport const REL_LINKS: unique symbol = Symbol('yayson.rel-links')\nexport const REL_META: unique symbol = Symbol('yayson.rel-meta')\n"],"mappings":";AAEA,IAAM,UAAN,MAAc;CAGZ,OAAO,IAAI,OAAkB,KAAuB;EAElD,MAAM,MAAM;AACZ,MAAI,IACF,QAAO,IAAI;AAEb,SAAO;;CAGT,OAAO,GAAG,OAAsC;EAC9C,MAAM,KAAK,KAAK,IAAI,OAAO,KAAK;AAChC,MAAI,MAAM,KACR;AAEF,SAAO,GAAG;;CAGZ,OAAO,IAAI,OAAkB,KAAsB;AACjD,SAAO,OAAO;;;AAIlB,sBAAe;;;;AC3Bf,MAAa,OAAsB,OAAO,cAAc;AACxD,MAAa,QAAuB,OAAO,eAAe;AAC1D,MAAa,OAAsB,OAAO,cAAc;AACxD,MAAa,YAA2B,OAAO,mBAAmB;AAClE,MAAa,WAA0B,OAAO,kBAAkB"}