yayson 4.1.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 +43 -0
  2. package/build/legacy.cjs +5 -5
  3. package/build/legacy.d.cts +34 -6
  4. package/build/legacy.d.cts.map +1 -1
  5. package/build/legacy.d.mts +34 -6
  6. package/build/legacy.d.mts.map +1 -1
  7. package/build/legacy.mjs +5 -5
  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-NiKV-lj-.d.cts → types-KZiF6x7A.d.mts} +14 -2
  13. package/build/types-KZiF6x7A.d.mts.map +1 -0
  14. package/build/{types-Do2flKZX.d.mts → types-iC38_iCI.d.cts} +14 -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-3UYKq2H5.d.cts → yayson-BvwMr4Ad.d.mts} +6 -4
  21. package/build/yayson-BvwMr4Ad.d.mts.map +1 -0
  22. package/build/{yayson-l2JKseMH.cjs → yayson-C4P8J4sn.cjs} +34 -19
  23. package/build/{yayson-CwZg5FNt.mjs → yayson-DIQ_olLX.mjs} +35 -20
  24. package/build/yayson-DIQ_olLX.mjs.map +1 -0
  25. package/build/{yayson-Ce5uGpgU.d.mts → yayson-DTMLeA5k.d.cts} +6 -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-Do2flKZX.d.mts.map +0 -1
  36. package/build/types-NiKV-lj-.d.cts.map +0 -1
  37. package/build/yayson-3UYKq2H5.d.cts.map +0 -1
  38. package/build/yayson-Ce5uGpgU.d.mts.map +0 -1
  39. package/build/yayson-CwZg5FNt.mjs.map +0 -1
package/README.md CHANGED
@@ -81,6 +81,49 @@ class BikePresenter extends Presenter {
81
81
  }
82
82
  ```
83
83
 
84
+ ### Declaring cardinality and optional relationships
85
+
86
+ By default a missing relationship renders as `data: null`. This isn't valid JSON:API for to-many relationships (the spec requires `[]`) and can mislead clients when a relationship simply wasn't loaded. Use the config form to declare cardinality and optional semantics:
87
+
88
+ ```javascript
89
+ class BikePresenter extends Presenter {
90
+ static type = 'bikes'
91
+
92
+ relationships() {
93
+ return {
94
+ wheels: { presenter: WheelPresenter, hasMany: true },
95
+ manufacturer: { presenter: ManufacturerPresenter, optional: true },
96
+ accessories: { presenter: AccessoryPresenter, hasMany: true, optional: true },
97
+ }
98
+ }
99
+ }
100
+
101
+ BikePresenter.render({ id: 1 })
102
+ ```
103
+
104
+ This produces:
105
+
106
+ ```javascript
107
+ {
108
+ data: {
109
+ id: '1',
110
+ type: 'bikes',
111
+ attributes: {},
112
+ relationships: {
113
+ wheels: { data: [] } // hasMany + no data → empty array, not null
114
+ // manufacturer and accessories omitted entirely — they weren't loaded
115
+ }
116
+ }
117
+ }
118
+ ```
119
+
120
+ - **`hasMany: true`** — declares a to-many relationship. Empty or missing data renders as `data: []` instead of `data: null`.
121
+ - **`optional: true`** — when the relationship key is absent from the instance, the relationship is omitted from the output (or rendered with just `links` if `links()` configures one for that key). An explicit `null` on the instance still renders as `data: null` — `optional` distinguishes "not loaded" from "explicitly empty".
122
+
123
+ In `payload()` output, `optional` omission is disabled (a write request asserts state, so dropping a relationship would be misleading), but `hasMany` still applies so a client can correctly clear a to-many with `data: []`.
124
+
125
+ The bare-class form (`relationships() { return { wheels: WheelPresenter } }`) is unchanged.
126
+
84
127
  ### Filtering attributes with fields
85
128
 
86
129
  Use the static `fields` property to limit which attributes are included in the output:
package/build/legacy.cjs CHANGED
@@ -1,5 +1,5 @@
1
- const require_symbols = require('./symbols-nFs99aEX.cjs');
2
- const require_yayson = require('./yayson-l2JKseMH.cjs');
1
+ const require_symbols = require('./symbols-B--FS78o.cjs');
2
+ const require_yayson = require('./yayson-C4P8J4sn.cjs');
3
3
 
4
4
  //#region src/yayson/legacy-presenter.ts
5
5
  function hasId$1(value) {
@@ -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;
@@ -1,5 +1,5 @@
1
- import { E as ModelLike, T as Adapter, _ as StoreResult, a as JsonApiRelationship, c as LegacyPresenterOptions, d as SchemaRegistry, f as StoreModel, i as JsonApiLinks, l as LegacyStoreOptions, m as StoreModels, n as JsonApiDocument, o as JsonApiRelationships, p as StoreModelWithOptionalId, r as JsonApiLink, s as JsonApiResource, t as InferModelType, u as PresenterOptions, v as ValidationError } from "./types-NiKV-lj-.cjs";
2
- import { i as Presenter, r as yayson$1, t as YaysonOptions } from "./yayson-3UYKq2H5.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 {
@@ -25,10 +25,12 @@ declare function createLegacyPresenter(Presenter: Presenter): {
25
25
  id(instance: ModelLike): string | undefined;
26
26
  selfLinks(_instance: ModelLike): JsonApiLink | string | undefined;
27
27
  links(_instance?: ModelLike): JsonApiLinks | undefined;
28
- relationships(): Record<string, /*elided*/any>;
28
+ relationships(): Record<string, /*elided*/any | RelationshipConfig< /*elided*/any>>;
29
29
  attributes(instance: ModelLike | null): Record<string, unknown>;
30
30
  includeRelationships(scope: JsonApiDocument, instance: ModelLike): unknown[];
31
- buildRelationships(instance: ModelLike | null): JsonApiRelationships | null;
31
+ buildRelationships(instance: ModelLike | null, options?: {
32
+ payload?: boolean;
33
+ }): JsonApiRelationships | null;
32
34
  buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
33
35
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null | undefined, options?: PresenterOptions): JsonApiDocument;
34
36
  payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
@@ -40,8 +42,34 @@ declare function createLegacyPresenter(Presenter: Presenter): {
40
42
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
41
43
  render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
42
44
  payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
43
- }>;
44
- buildRelationships(instance: ModelLike | null): JsonApiRelationships | null;
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;
45
73
  buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
46
74
  };
47
75
  type: string;
@@ -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;;yBAqBM,SAAA,UAAmB,MAAA;gCA8BZ,qBAAA,EAAqB,QAAA,EAAY,SAAA;iC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YpB,KAAA,CAAM,IAAA,EAAM,UAAA,GAAa,wBAAA;EAIhC,KAAA,CAAM,IAAA,EAAM,UAAA,GAAa,wBAAA;;EA2BzB,OAAA,kBAAA,CAA0B,IAAA,EAAM,CAAA,EAAG,IAAA,EAAM,UAAA,GAAa,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
+ {"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;iBAb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reModel, i as JsonApiLinks, l as LegacyStoreOptions, m as StoreModels, n as JsonApiDocument, o as JsonApiRelationships, p as StoreModelWithOptionalId, r as JsonApiLink, s as JsonApiResource, t as InferModelType, u as PresenterOptions, v as ValidationError } from "./types-Do2flKZX.mjs";
2
- import { i as Presenter, r as yayson$1, t as YaysonOptions } from "./yayson-Ce5uGpgU.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 {
@@ -25,10 +25,12 @@ declare function createLegacyPresenter(Presenter: Presenter): {
25
25
  id(instance: ModelLike): string | undefined;
26
26
  selfLinks(_instance: ModelLike): JsonApiLink | string | undefined;
27
27
  links(_instance?: ModelLike): JsonApiLinks | undefined;
28
- relationships(): Record<string, /*elided*/any>;
28
+ relationships(): Record<string, /*elided*/any | RelationshipConfig< /*elided*/any>>;
29
29
  attributes(instance: ModelLike | null): Record<string, unknown>;
30
30
  includeRelationships(scope: JsonApiDocument, instance: ModelLike): unknown[];
31
- buildRelationships(instance: ModelLike | null): JsonApiRelationships | null;
31
+ buildRelationships(instance: ModelLike | null, options?: {
32
+ payload?: boolean;
33
+ }): JsonApiRelationships | null;
32
34
  buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
33
35
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null | undefined, options?: PresenterOptions): JsonApiDocument;
34
36
  payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
@@ -40,8 +42,34 @@ declare function createLegacyPresenter(Presenter: Presenter): {
40
42
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
41
43
  render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument;
42
44
  payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
43
- }>;
44
- buildRelationships(instance: ModelLike | null): JsonApiRelationships | null;
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;
45
73
  buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
46
74
  };
47
75
  type: string;
@@ -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;;yBAqBM,SAAA,UAAmB,MAAA;gCA8BZ,qBAAA,EAAqB,QAAA,EAAY,SAAA;iCAqCrC,SAAA,GAAY,SAAA,uBAA8B,OAAA,GACtD,sBAAA,GACT,qBAAA;sBA6De,SAAA,GAAY,qBAAA;iCAWD,SAAA,GAAY,SAAA,YAAqB,qBAAA;;;;;;qBAzElD;;;;;wCAyE6B;;;;;;;;;;;;;;;;;;;;;;+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;;;UCxLpD,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,EAAW,MAAA,SAAe,MAAA;EAC1B,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,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.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,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,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"}
package/build/legacy.mjs CHANGED
@@ -1,5 +1,5 @@
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-CwZg5FNt.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
5
  function hasId$1(value) {
@@ -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;
@@ -1 +1 @@
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 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 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,KAAK,YAAY,QAAQ,IAAI,SAAS,EAAE;GAChE,MAAM,gBAAgB,KAAK,eAAe;AAC1C,OAAI,cACF,MAAK,MAAM,OAAO,eAAe;IAC/B,IAAI;IACJ,MAAM,OAAO,WAAW;AACxB,QAAI,QAAQ,MAAM;AAChB,UAAK,WAAW,MAAM;AACtB,SAAI,MAAM,KACR,YAAW,OAAO;eAEX,MAAM,QAAQ,KAAK,CAC5B,YAAW,OAAO,KAAK,KAAK,QAAkBA,QAAM,IAAI,GAAG,IAAI,KAAK,IAAK;aAChEA,QAAM,KAAK,CACpB,YAAW,OAAO,KAAK;;GAM7B,MAAM,mBAAmB,gBAAgB,OAAO,KAAK,cAAc,GAAG,EAAE;AAGxE,UAAO,eAAe,YAFA,KAA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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,KAAK,YAAY,QAAQ,IAAI,SAAS,EAAE;GAChE,MAAM,gBAAgB,KAAK,eAAe;AAC1C,OAAI,cACF,MAAK,MAAM,OAAO,eAAe;IAC/B,IAAI;IACJ,MAAM,OAAO,WAAW;AACxB,QAAI,QAAQ,MAAM;AAChB,UAAK,WAAW,MAAM;AACtB,SAAI,MAAM,KACR,YAAW,OAAO;eAEX,MAAM,QAAQ,KAAK,CAC5B,YAAW,OAAO,KAAK,KAAK,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AAwB,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"}
@@ -4,6 +4,7 @@ declare class Adapter {
4
4
  static get(model: ModelLike): Record<string, unknown>;
5
5
  static get(model: ModelLike, key: string): unknown;
6
6
  static id(model: ModelLike): string | undefined;
7
+ static has(model: ModelLike, key: string): boolean;
7
8
  }
8
9
  //#endregion
9
10
  //#region src/yayson/symbols.d.ts
@@ -46,6 +47,17 @@ interface JsonApiRelationship {
46
47
  links?: JsonApiLink;
47
48
  meta?: Record<string, unknown>;
48
49
  }
50
+ /**
51
+ * Extended relationship descriptor for `relationships()` return values.
52
+ * `hasMany: true` renders empty/missing as `data: []` (spec-compliant to-many).
53
+ * `optional: true` omits the relationship when its key is absent on the instance
54
+ * (or renders `links` only); explicit `null`/`[]` still render normally.
55
+ */
56
+ interface RelationshipConfig<P> {
57
+ presenter: P;
58
+ hasMany?: boolean;
59
+ optional?: boolean;
60
+ }
49
61
  interface JsonApiRelationships {
50
62
  [key: string]: JsonApiRelationship;
51
63
  }
@@ -125,5 +137,5 @@ interface LegacyStoreOptions<S extends SchemaRegistry = SchemaRegistry> {
125
137
  strict?: boolean;
126
138
  }
127
139
  //#endregion
128
- export { REL_META as C, ModelLike as E, REL_LINKS as S, Adapter as T, StoreResult as _, JsonApiRelationship as a, LINKS as b, LegacyPresenterOptions as c, SchemaRegistry as d, StoreModel as f, StoreRecord as g, StoreOptions as h, JsonApiLinks as i, LegacyStoreOptions as l, StoreModels as m, JsonApiDocument as n, JsonApiRelationships as o, StoreModelWithOptionalId as p, JsonApiLink as r, JsonApiResource as s, InferModelType as t, PresenterOptions as u, ValidationError as v, TYPE as w, META as x, ZodLikeSchema as y };
129
- //# sourceMappingURL=types-NiKV-lj-.d.cts.map
140
+ export { REL_LINKS as C, ModelLike as D, Adapter as E, META as S, TYPE as T, StoreRecord as _, JsonApiRelationship as a, ZodLikeSchema as b, LegacyPresenterOptions as c, RelationshipConfig as d, SchemaRegistry as f, StoreOptions as g, StoreModels as h, JsonApiLinks as i, LegacyStoreOptions as l, StoreModelWithOptionalId as m, JsonApiDocument as n, JsonApiRelationships as o, StoreModel as p, JsonApiLink as r, JsonApiResource as s, InferModelType as t, PresenterOptions as u, StoreResult as v, REL_META as w, LINKS as x, ValidationError as y };
141
+ //# sourceMappingURL=types-KZiF6x7A.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types-KZiF6x7A.d.mts","names":[],"sources":["../../../../../../yayson/adapter.ts","../../../../../../yayson/symbols.ts","../../../../../../yayson/schema.ts","../../../../../../yayson/types.ts"],"mappings":";KAAY,SAAA;AAAA,cAEN,OAAA;EAAA,OACG,GAAA,CAAI,KAAA,EAAO,SAAA,GAAY,MAAA;EAAA,OACvB,GAAA,CAAI,KAAA,EAAO,SAAA,EAAW,GAAA;EAAA,OAUtB,EAAA,CAAG,KAAA,EAAO,SAAA;EAAA,OAQV,GAAA,CAAI,KAAA,EAAO,SAAA,EAAW,GAAA;AAAA;;;cCtBlB,IAAA;AAAA,cACA,KAAA;AAAA,cACA,IAAA;AAAA,cACA,SAAA;AAAA,cACA,QAAA;;;;ADJb;;;UEIiB,aAAA;EACf,KAAA,GAAQ,IAAA;EACR,SAAA,GAAY,IAAA;IAAoB,OAAA;IAAe,IAA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AAA;EAAA,CACA,KAAA,IAAS,WAAA;EAAA,CACT,IAAA,IAAQ,MAAA;AAAA;AAAA,UAGM,WAAA;EAAA,CACd,IAAA;IAAA,CACE,EAAA,WAAa,UAAA;EAAA;AAAA;AAAA,UAID,cAAA;EAAA,CACd,IAAA,WAAe,aAAA;AAAA;AAAA,KAIN,eAAA,WAA0B,MAAA;EACpC,KAAA,GAAQ,IAAA;EACR,SAAA,GAAY,IAAA;AAAA,IAEV,UAAA;AAAA,KAGQ,cAAA,sCAAoD,QAAA,SAAiB,cAAA,GAC7E,QAAA,eAAuB,QAAA,GACrB,eAAA,CAAgB,QAAA,CAAS,QAAA,KACzB,UAAA,GACF,UAAA;AAAA,UAEa,WAAA,KAAgB,UAAA,UAAoB,KAAA,CAAM,CAAA;EAAA,CACxD,IAAA,IAAQ,MAAA;AAAA;AAAA,UAGM,YAAA,WAAuB,cAAA,GAAiB,cAAA;EACvD,OAAA,GAAU,CAAA;EACV,MAAA;AAAA;AAAA,UAGe,eAAA;EACf,IAAA;EACA,EAAA;EACA,KAAA;AAAA;AAAA,UAGe,kBAAA,WAA6B,cAAA,GAAiB,cAAA;EAC7D,KAAA,GAAQ,MAAA;EACR,OAAA,GAAU,CAAA;EACV,MAAA;AAAA"}
@@ -4,6 +4,7 @@ declare class Adapter {
4
4
  static get(model: ModelLike): Record<string, unknown>;
5
5
  static get(model: ModelLike, key: string): unknown;
6
6
  static id(model: ModelLike): string | undefined;
7
+ static has(model: ModelLike, key: string): boolean;
7
8
  }
8
9
  //#endregion
9
10
  //#region src/yayson/symbols.d.ts
@@ -46,6 +47,17 @@ interface JsonApiRelationship {
46
47
  links?: JsonApiLink;
47
48
  meta?: Record<string, unknown>;
48
49
  }
50
+ /**
51
+ * Extended relationship descriptor for `relationships()` return values.
52
+ * `hasMany: true` renders empty/missing as `data: []` (spec-compliant to-many).
53
+ * `optional: true` omits the relationship when its key is absent on the instance
54
+ * (or renders `links` only); explicit `null`/`[]` still render normally.
55
+ */
56
+ interface RelationshipConfig<P> {
57
+ presenter: P;
58
+ hasMany?: boolean;
59
+ optional?: boolean;
60
+ }
49
61
  interface JsonApiRelationships {
50
62
  [key: string]: JsonApiRelationship;
51
63
  }
@@ -125,5 +137,5 @@ interface LegacyStoreOptions<S extends SchemaRegistry = SchemaRegistry> {
125
137
  strict?: boolean;
126
138
  }
127
139
  //#endregion
128
- export { REL_META as C, ModelLike as E, REL_LINKS as S, Adapter as T, StoreResult as _, JsonApiRelationship as a, LINKS as b, LegacyPresenterOptions as c, SchemaRegistry as d, StoreModel as f, StoreRecord as g, StoreOptions as h, JsonApiLinks as i, LegacyStoreOptions as l, StoreModels as m, JsonApiDocument as n, JsonApiRelationships as o, StoreModelWithOptionalId as p, JsonApiLink as r, JsonApiResource as s, InferModelType as t, PresenterOptions as u, ValidationError as v, TYPE as w, META as x, ZodLikeSchema as y };
129
- //# sourceMappingURL=types-Do2flKZX.d.mts.map
140
+ export { REL_LINKS as C, ModelLike as D, Adapter as E, META as S, TYPE as T, StoreRecord as _, JsonApiRelationship as a, ZodLikeSchema as b, LegacyPresenterOptions as c, RelationshipConfig as d, SchemaRegistry as f, StoreOptions as g, StoreModels as h, JsonApiLinks as i, LegacyStoreOptions as l, StoreModelWithOptionalId as m, JsonApiDocument as n, JsonApiRelationships as o, StoreModel as p, JsonApiLink as r, JsonApiResource as s, InferModelType as t, PresenterOptions as u, StoreResult as v, REL_META as w, LINKS as x, ValidationError as y };
141
+ //# sourceMappingURL=types-iC38_iCI.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types-iC38_iCI.d.cts","names":[],"sources":["../../../../../../yayson/adapter.ts","../../../../../../yayson/symbols.ts","../../../../../../yayson/schema.ts","../../../../../../yayson/types.ts"],"mappings":";KAAY,SAAA;AAAA,cAEN,OAAA;EAAA,OACG,GAAA,CAAI,KAAA,EAAO,SAAA,GAAY,MAAA;EAAA,OACvB,GAAA,CAAI,KAAA,EAAO,SAAA,EAAW,GAAA;EAAA,OAUt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,WAAA;EACR,IAAA,GAAO,MAAA;AAAA;AAAA,UAGQ,UAAA,SAAmB,MAAA;EAClC,EAAA;EAAA,CACC,IAAA;EAAA,CACA,KAAA,IAAS,WAAA;EAAA,CACT,IAAA,IAAQ,MAAA;EAAA,CACR,SAAA,IAAa,WAAA;EAAA,CACb,QAAA,IAAY,MAAA;AAAA;;UAIE,wBAAA,SAAiC,MAAA;EAChD,EAAA;EAAA,CACC,IAAA;EAAA,CACA,KAAA,IAAS,WAAA;EAAA,CACT,IAAA,IAAQ,MAAA;AAAA;AAAA,UAGM,WAAA;EAAA,CACd,IAAA;IAAA,CACE,EAAA,WAAa,UAAA;EAAA;AAAA;AAAA,UAID,cAAA;EAAA,CACd,IAAA,WAAe,aAAA;AAAA;AAAA,KAIN,eAAA,WAA0B,MAAA;EACpC,KAAA,GAAQ,IAAA;EACR,SAAA,GAAY,IAAA;AAAA,IAEV,UAAA;AAAA,KAGQ,cAAA,sCAAoD,QAAA,SAAiB,cAAA,GAC7E,QAAA,eAAuB,QAAA,GACrB,eAAA,CAAgB,QAAA,CAAS,QAAA,KACzB,UAAA,GACF,UAAA;AAAA,UAEa,WAAA,KAAgB,UAAA,UAAoB,KAAA,CAAM,CAAA;EAAA,CACxD,IAAA,IAAQ,MAAA;AAAA;AAAA,UAGM,YAAA,WAAuB,cAAA,GAAiB,cAAA;EACvD,OAAA,GAAU,CAAA;EACV,MAAA;AAAA;AAAA,UAGe,eAAA;EACf,IAAA;EACA,EAAA;EACA,KAAA;AAAA;AAAA,UAGe,kBAAA,WAA6B,cAAA,GAAiB,cAAA;EAC7D,KAAA,GAAQ,MAAA;EACR,OAAA,GAAU,CAAA;EACV,MAAA;AAAA"}
package/build/utils.cjs CHANGED
@@ -1,4 +1,4 @@
1
- const require_symbols = require('./symbols-nFs99aEX.cjs');
1
+ const require_symbols = require('./symbols-B--FS78o.cjs');
2
2
 
3
3
  //#region src/utils.ts
4
4
  function getType(model) {
package/build/utils.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as REL_META, E as ModelLike, S as REL_LINKS, T as Adapter, b as LINKS, f as StoreModel, r as JsonApiLink, w as TYPE, x as META } from "./types-NiKV-lj-.cjs";
1
+ import { C as REL_LINKS, D as ModelLike, E as Adapter, S as META, T as TYPE, p as StoreModel, r as JsonApiLink, w as REL_META, x as LINKS } from "./types-iC38_iCI.cjs";
2
2
 
3
3
  //#region src/utils.d.ts
4
4
  declare function getType(model: StoreModel): string | undefined;
package/build/utils.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as REL_META, E as ModelLike, S as REL_LINKS, T as Adapter, b as LINKS, f as StoreModel, r as JsonApiLink, w as TYPE, x as META } from "./types-Do2flKZX.mjs";
1
+ import { C as REL_LINKS, D as ModelLike, E as Adapter, S as META, T as TYPE, p as StoreModel, r as JsonApiLink, w as REL_META, x as LINKS } from "./types-KZiF6x7A.mjs";
2
2
 
3
3
  //#region src/utils.d.ts
4
4
  declare function getType(model: StoreModel): string | undefined;
package/build/utils.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as TYPE, i as REL_META, n as META, o as adapter_default, r as REL_LINKS, t as LINKS } from "./symbols-DSjKJ8vh.mjs";
1
+ import { a as TYPE, i as REL_META, n as META, o as adapter_default, r as REL_LINKS, t as LINKS } from "./symbols-BfU4k1el.mjs";
2
2
 
3
3
  //#region src/utils.ts
4
4
  function getType(model) {
@@ -1,4 +1,4 @@
1
- import { E as ModelLike, T as Adapter, _ as StoreResult, d as SchemaRegistry, f as StoreModel, g as StoreRecord$1, h as StoreOptions, i as JsonApiLinks, m as StoreModels, n as JsonApiDocument, o as JsonApiRelationships, p as StoreModelWithOptionalId, r as JsonApiLink, t as InferModelType, u as PresenterOptions, v as ValidationError } from "./types-NiKV-lj-.cjs";
1
+ import { D as ModelLike, E as Adapter, _ as StoreRecord$1, d as RelationshipConfig, f as SchemaRegistry, g as StoreOptions, h as StoreModels, i as JsonApiLinks, m as StoreModelWithOptionalId, n as JsonApiDocument, o as JsonApiRelationships, p as StoreModel, r as JsonApiLink, t as InferModelType, u as PresenterOptions, v as StoreResult, y as ValidationError } from "./types-KZiF6x7A.mjs";
2
2
 
3
3
  //#region src/yayson/presenter.d.ts
4
4
  declare function createPresenter(adapter: typeof Adapter): {
@@ -8,10 +8,12 @@ declare function createPresenter(adapter: typeof Adapter): {
8
8
  id(instance: ModelLike): string | undefined;
9
9
  selfLinks(_instance: ModelLike): JsonApiLink | string | undefined;
10
10
  links(_instance?: ModelLike): JsonApiLinks | undefined;
11
- relationships(): Record<string, /*elided*/any>;
11
+ relationships(): Record<string, /*elided*/any | RelationshipConfig< /*elided*/any>>;
12
12
  attributes(instance: ModelLike | null): Record<string, unknown>;
13
13
  includeRelationships(scope: JsonApiDocument, instance: ModelLike): unknown[];
14
- buildRelationships(instance: ModelLike | null): JsonApiRelationships | null;
14
+ buildRelationships(instance: ModelLike | null, options?: {
15
+ payload?: boolean;
16
+ }): JsonApiRelationships | null;
15
17
  buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
16
18
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null | undefined, options?: PresenterOptions): JsonApiDocument;
17
19
  payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
@@ -78,4 +80,4 @@ interface YaysonResult {
78
80
  declare function yayson(options?: YaysonOptions): YaysonResult;
79
81
  //#endregion
80
82
  export { Presenter as i, YaysonResult as n, yayson as r, YaysonOptions as t };
81
- //# sourceMappingURL=yayson-3UYKq2H5.d.cts.map
83
+ //# sourceMappingURL=yayson-BvwMr4Ad.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"yayson-BvwMr4Ad.d.mts","names":[],"sources":["../../../../../../yayson/presenter.ts","../../../../../../yayson/store.ts","../../../../../../yayson.ts"],"mappings":";;;iBA0BwB,eAAA,CAAgB,OAAA,SAAgB,OAAA;EAAA,aAUhC,eAAA;iBAVe;WAQ5B,eAAA;iBAMM,SAAA;yBAIQ,SAAA,GAAY,WAAA;sBAIf,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AAM,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;EAkBrB,OAAA,CAAQ,IAAA,EAAM,eAAA,GAAkB,WAAA;EA8DhC,IAAA,CAAK,IAAA,EAAM,eAAA,GAAkB,UAAA,GAAa,WAAA;EDvI5B;;;;;;;EAAA,OC0JP,KAAA,CAAM,IAAA,EAAM,eAAA,GAAkB,wBAAA;EAIrC,KAAA,CAAM,IAAA,EAAM,eAAA,GAAkB,wBAAA;EAQ9B,QAAA,kBAAA,CAA2B,IAAA,EAAM,CAAA,EAAG,IAAA,EAAM,eAAA,GAAkB,cAAA,CAAe,CAAA,EAAG,CAAA;EAW9E,WAAA,kBAAA,CAA8B,IAAA,EAAM,CAAA,EAAG,IAAA,EAAM,eAAA,GAAkB,WAAA,CAAY,cAAA,CAAe,CAAA,EAAG,CAAA;AAAA;;;KCzU1F,aAAA,mBAAgC,OAAA;AAAA,UAE3B,aAAA;EACR,OAAA,GAAU,aAAA;AAAA;AAAA,UAGF,YAAA;EACR,KAAA,SAAc,KAAA;EACd,SAAA,EAAW,SAAA;EACX,OAAA,SAAgB,OAAA;AAAA;AAAA,iBAgBT,MAAA,CAAO,OAAA,GAAU,aAAA,GAAgB,YAAA"}
@@ -1,4 +1,4 @@
1
- const require_symbols = require('./symbols-nFs99aEX.cjs');
1
+ const require_symbols = require('./symbols-B--FS78o.cjs');
2
2
 
3
3
  //#region src/yayson/adapters/sequelize.ts
4
4
  function isSequelizeModel(model) {
@@ -8,6 +8,10 @@ var SequelizeAdapter = class extends require_symbols.adapter_default {
8
8
  static get(model, key) {
9
9
  if (isSequelizeModel(model)) return model.get(key);
10
10
  }
11
+ static has(model, key) {
12
+ if (isSequelizeModel(model)) return model.get(key) !== void 0;
13
+ return false;
14
+ }
11
15
  static id(model) {
12
16
  const pkFields = model.constructor && "primaryKeys" in model.constructor ? Object.keys(model.constructor.primaryKeys) : ["id"];
13
17
  if (pkFields.length > 1) throw new Error("YAYSON does not support Sequelize models with composite primary keys. You can only use one column for your primary key. Currently using: " + pkFields.join(","));
@@ -69,23 +73,32 @@ function createPresenter(adapter) {
69
73
  const result = [];
70
74
  if (!relationships) return result;
71
75
  for (const key in relationships) {
72
- const factory = relationships[key];
73
- if (!factory) throw new Error(`Presenter for ${key} in ${this.constructor.type} is not defined`);
74
- const presenter = new factory(scope);
76
+ const entry = relationships[key];
77
+ if (!entry) throw new Error(`Presenter for ${key} in ${this.constructor.type} is not defined`);
78
+ const presenter = new (typeof entry === "function" ? entry : entry.presenter)(scope);
75
79
  const data = this.constructor.adapter.get(instance, key);
76
80
  result.push(presenter.toJSON(data, { include: true }));
77
81
  }
78
82
  return result;
79
83
  }
80
- buildRelationships(instance) {
84
+ buildRelationships(instance, options = {}) {
81
85
  if (instance == null) return null;
82
86
  const rels = this.relationships();
83
87
  const links = this.links(instance) || {};
88
+ const isPayload = options.payload === true;
84
89
  let relationships = null;
85
90
  if (!rels) return null;
86
91
  for (const key in rels) {
92
+ const entry = rels[key];
93
+ if (!entry) continue;
94
+ const isConfig = typeof entry !== "function";
95
+ const presenter = isConfig ? entry.presenter : entry;
96
+ const hasMany = isConfig ? entry.hasMany : void 0;
97
+ const optional = isConfig ? entry.optional ?? false : false;
98
+ const linkValue = links[key];
99
+ const hasLinks = linkValue != null;
87
100
  const data = this.constructor.adapter.get(instance, key);
88
- const presenter = rels[key];
101
+ const keyPresent = this.constructor.adapter.has(instance, key);
89
102
  const buildData = (d) => {
90
103
  const id = this.constructor.adapter.id(d);
91
104
  if (!id) throw new Error(`Model of type ${presenter.type} is missing an id (relationship '${key}' of ${this.constructor.type})`);
@@ -94,19 +107,21 @@ function createPresenter(adapter) {
94
107
  type: presenter.type
95
108
  };
96
109
  };
97
- const build = (d) => {
98
- const rel = {};
99
- if (d != null) rel.data = buildData(d);
100
- if (links[key] != null) rel.links = buildLinks(links[key]);
101
- else if (d == null) rel.data = null;
102
- return rel;
103
- };
110
+ if (optional && !keyPresent && !isPayload) {
111
+ if (hasLinks) {
112
+ if (!relationships) relationships = {};
113
+ relationships[key] = { links: buildLinks(linkValue) };
114
+ }
115
+ continue;
116
+ }
104
117
  if (!relationships) relationships = {};
105
- if (!relationships[key]) relationships[key] = {};
106
- if (Array.isArray(data)) {
107
- relationships[key].data = data.map(buildData);
108
- if (links[key] != null) relationships[key].links = buildLinks(links[key]);
109
- } else relationships[key] = build(data);
118
+ const rel = {};
119
+ if (Array.isArray(data)) rel.data = data.map(buildData);
120
+ else if (data != null) rel.data = buildData(data);
121
+ else if (hasMany === true) rel.data = [];
122
+ else if (!hasLinks) rel.data = null;
123
+ if (hasLinks) rel.links = buildLinks(linkValue);
124
+ relationships[key] = rel;
110
125
  }
111
126
  return relationships;
112
127
  }
@@ -159,7 +174,7 @@ function createPresenter(adapter) {
159
174
  };
160
175
  const id = this.id(instance);
161
176
  if (id != null) model.id = id;
162
- const relationships = this.buildRelationships(instance);
177
+ const relationships = this.buildRelationships(instance, { payload: true });
163
178
  if (relationships != null) model.relationships = relationships;
164
179
  const result = { data: model };
165
180
  const opts = options ?? {};
@@ -1,4 +1,4 @@
1
- import { a as TYPE, i as REL_META, n as META, o as adapter_default, r as REL_LINKS, t as LINKS } from "./symbols-DSjKJ8vh.mjs";
1
+ import { a as TYPE, i as REL_META, n as META, o as adapter_default, r as REL_LINKS, t as LINKS } from "./symbols-BfU4k1el.mjs";
2
2
 
3
3
  //#region src/yayson/adapters/sequelize.ts
4
4
  function isSequelizeModel(model) {
@@ -8,6 +8,10 @@ var SequelizeAdapter = class extends adapter_default {
8
8
  static get(model, key) {
9
9
  if (isSequelizeModel(model)) return model.get(key);
10
10
  }
11
+ static has(model, key) {
12
+ if (isSequelizeModel(model)) return model.get(key) !== void 0;
13
+ return false;
14
+ }
11
15
  static id(model) {
12
16
  const pkFields = model.constructor && "primaryKeys" in model.constructor ? Object.keys(model.constructor.primaryKeys) : ["id"];
13
17
  if (pkFields.length > 1) throw new Error("YAYSON does not support Sequelize models with composite primary keys. You can only use one column for your primary key. Currently using: " + pkFields.join(","));
@@ -69,23 +73,32 @@ function createPresenter(adapter) {
69
73
  const result = [];
70
74
  if (!relationships) return result;
71
75
  for (const key in relationships) {
72
- const factory = relationships[key];
73
- if (!factory) throw new Error(`Presenter for ${key} in ${this.constructor.type} is not defined`);
74
- const presenter = new factory(scope);
76
+ const entry = relationships[key];
77
+ if (!entry) throw new Error(`Presenter for ${key} in ${this.constructor.type} is not defined`);
78
+ const presenter = new (typeof entry === "function" ? entry : entry.presenter)(scope);
75
79
  const data = this.constructor.adapter.get(instance, key);
76
80
  result.push(presenter.toJSON(data, { include: true }));
77
81
  }
78
82
  return result;
79
83
  }
80
- buildRelationships(instance) {
84
+ buildRelationships(instance, options = {}) {
81
85
  if (instance == null) return null;
82
86
  const rels = this.relationships();
83
87
  const links = this.links(instance) || {};
88
+ const isPayload = options.payload === true;
84
89
  let relationships = null;
85
90
  if (!rels) return null;
86
91
  for (const key in rels) {
92
+ const entry = rels[key];
93
+ if (!entry) continue;
94
+ const isConfig = typeof entry !== "function";
95
+ const presenter = isConfig ? entry.presenter : entry;
96
+ const hasMany = isConfig ? entry.hasMany : void 0;
97
+ const optional = isConfig ? entry.optional ?? false : false;
98
+ const linkValue = links[key];
99
+ const hasLinks = linkValue != null;
87
100
  const data = this.constructor.adapter.get(instance, key);
88
- const presenter = rels[key];
101
+ const keyPresent = this.constructor.adapter.has(instance, key);
89
102
  const buildData = (d) => {
90
103
  const id = this.constructor.adapter.id(d);
91
104
  if (!id) throw new Error(`Model of type ${presenter.type} is missing an id (relationship '${key}' of ${this.constructor.type})`);
@@ -94,19 +107,21 @@ function createPresenter(adapter) {
94
107
  type: presenter.type
95
108
  };
96
109
  };
97
- const build = (d) => {
98
- const rel = {};
99
- if (d != null) rel.data = buildData(d);
100
- if (links[key] != null) rel.links = buildLinks(links[key]);
101
- else if (d == null) rel.data = null;
102
- return rel;
103
- };
110
+ if (optional && !keyPresent && !isPayload) {
111
+ if (hasLinks) {
112
+ if (!relationships) relationships = {};
113
+ relationships[key] = { links: buildLinks(linkValue) };
114
+ }
115
+ continue;
116
+ }
104
117
  if (!relationships) relationships = {};
105
- if (!relationships[key]) relationships[key] = {};
106
- if (Array.isArray(data)) {
107
- relationships[key].data = data.map(buildData);
108
- if (links[key] != null) relationships[key].links = buildLinks(links[key]);
109
- } else relationships[key] = build(data);
118
+ const rel = {};
119
+ if (Array.isArray(data)) rel.data = data.map(buildData);
120
+ else if (data != null) rel.data = buildData(data);
121
+ else if (hasMany === true) rel.data = [];
122
+ else if (!hasLinks) rel.data = null;
123
+ if (hasLinks) rel.links = buildLinks(linkValue);
124
+ relationships[key] = rel;
110
125
  }
111
126
  return relationships;
112
127
  }
@@ -159,7 +174,7 @@ function createPresenter(adapter) {
159
174
  };
160
175
  const id = this.id(instance);
161
176
  if (id != null) model.id = id;
162
- const relationships = this.buildRelationships(instance);
177
+ const relationships = this.buildRelationships(instance, { payload: true });
163
178
  if (relationships != null) model.relationships = relationships;
164
179
  const result = { data: model };
165
180
  const opts = options ?? {};
@@ -449,4 +464,4 @@ var yayson_default = yayson;
449
464
 
450
465
  //#endregion
451
466
  export { validate as n, filterByFields as r, yayson_default as t };
452
- //# sourceMappingURL=yayson-CwZg5FNt.mjs.map
467
+ //# sourceMappingURL=yayson-DIQ_olLX.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"yayson-DIQ_olLX.mjs","names":["Adapter","#findModel","#createStub","#resolveRelationships","#createModel","Adapter","adapters.sequelize"],"sources":["../src/yayson/adapters/sequelize.ts","../src/yayson/utils.ts","../src/yayson/presenter.ts","../src/yayson/schema.ts","../src/yayson/store.ts","../src/yayson.ts"],"sourcesContent":["import Adapter, { type ModelLike } from '../adapter.js'\n\ninterface SequelizeModel {\n get(key?: string): unknown\n constructor?: {\n primaryKeys?: Record<string, unknown>\n }\n}\nfunction isSequelizeModel(model: unknown): model is SequelizeModel {\n return model != null && typeof model === 'object' && 'get' in model && typeof model.get === 'function'\n}\n\nclass SequelizeAdapter extends Adapter {\n static override get(model: ModelLike): Record<string, unknown>\n static override get(model: ModelLike, key: string): unknown\n static override get(model: ModelLike, key?: string): unknown {\n if (isSequelizeModel(model)) {\n return model.get(key)\n }\n return undefined\n }\n\n static override has(model: ModelLike, key: string): boolean {\n if (isSequelizeModel(model)) {\n // `key in model` doesn't work across Sequelize versions; .get() returning\n // undefined is the proxy for \"unloaded association or undeclared key\".\n return model.get(key) !== undefined\n }\n return false\n }\n\n static override id(model: ModelLike): string | undefined {\n // Retain backwards compatibility with older sequelize versions\n const hasPrimaryKeys = model.constructor && 'primaryKeys' in model.constructor\n const pkFields = hasPrimaryKeys\n ? // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Access primaryKeys for Sequelize v3/v4 compatibility\n Object.keys((model.constructor as unknown as { primaryKeys: Record<string, unknown> }).primaryKeys)\n : ['id']\n\n if (pkFields.length > 1) {\n throw new Error(\n 'YAYSON does not support Sequelize models with composite primary keys. You can only use one column for your primary key. Currently using: ' +\n pkFields.join(','),\n )\n } else if (pkFields.length < 1) {\n throw new Error(\n 'YAYSON can only serialize Sequelize models which have a primary key. This is used for the JSON:API model id.',\n )\n }\n\n const id = this.get(model, pkFields[0])\n if (id == null) {\n return undefined\n }\n return `${id}`\n }\n}\n\nexport default SequelizeAdapter\n","/**\n * Filters an attributes object to only include keys in the fields array.\n * Returns the original attributes if fields is undefined.\n */\nexport function filterByFields(\n attributes: Record<string, unknown>,\n fields: string[] | undefined,\n): Record<string, unknown> {\n if (!fields) {\n return attributes\n }\n const filtered: Record<string, unknown> = {}\n for (const key of fields) {\n if (key in attributes) {\n filtered[key] = attributes[key]\n }\n }\n return filtered\n}\n","import Adapter, { ModelLike } from './adapter.js'\nimport type {\n JsonApiDocument,\n JsonApiLink,\n JsonApiLinks,\n JsonApiRelationship,\n JsonApiRelationships,\n JsonApiResource,\n JsonApiResourceIdentifier,\n PresenterOptions,\n RelationshipConfig,\n} from './types.js'\nimport { filterByFields } from './utils.js'\n\nfunction buildLinks(link: JsonApiLink | string | null | undefined): JsonApiLink | undefined {\n if (link == null) {\n return\n }\n if (typeof link === 'object' && (link.self != null || link.related != null)) {\n return link\n } else {\n return { self: String(link) }\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type -- Return type is inferred from class\nexport default function createPresenter(adapter: typeof Adapter) {\n class Presenter {\n declare ['constructor']: typeof Presenter\n\n static adapter = adapter\n static type = 'objects'\n static fields?: string[]\n\n scope: JsonApiDocument\n\n constructor(scope?: JsonApiDocument) {\n this.scope = scope ?? { data: null }\n }\n\n id(instance: ModelLike): string | undefined {\n return this.constructor.adapter.id(instance)\n }\n\n selfLinks(_instance: ModelLike): JsonApiLink | string | undefined {\n return undefined\n }\n\n links(_instance?: ModelLike): JsonApiLinks | undefined {\n return undefined\n }\n\n relationships(): Record<string, typeof Presenter | RelationshipConfig<typeof Presenter>> {\n return {}\n }\n\n attributes(instance: ModelLike | null): Record<string, unknown> {\n if (instance == null) {\n return {}\n }\n const attributes = { ...this.constructor.adapter.get(instance) }\n delete attributes['id']\n\n const relationships = this.relationships()\n if (relationships) {\n for (const key in relationships) {\n delete attributes[key]\n }\n }\n\n return filterByFields(attributes, this.constructor.fields)\n }\n\n includeRelationships(scope: JsonApiDocument, instance: ModelLike): unknown[] {\n const relationships = this.relationships()\n const result: unknown[] = []\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 const presenter = new factory(scope)\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 result.push(presenter.toJSON(data, { include: true }))\n }\n return result\n }\n\n buildRelationships(instance: ModelLike | null, options: { payload?: boolean } = {}): JsonApiRelationships | null {\n if (instance == null) {\n return null\n }\n const rels = this.relationships()\n const links = this.links(instance) || {}\n const isPayload = options.payload === true\n let relationships: JsonApiRelationships | null = null\n\n if (!rels) {\n return null\n }\n\n for (const key in rels) {\n const entry = rels[key]\n if (!entry) continue\n\n const isConfig = typeof entry !== 'function'\n const presenter = isConfig ? entry.presenter : entry\n const hasMany = isConfig ? entry.hasMany : undefined\n const optional = isConfig ? (entry.optional ?? false) : false\n\n const linkValue = links[key]\n const hasLinks = linkValue != null\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 | undefined\n const keyPresent = this.constructor.adapter.has(instance, key)\n\n const buildData = (d: ModelLike): JsonApiResourceIdentifier => {\n const id = this.constructor.adapter.id(d)\n if (!id) {\n throw new Error(\n `Model of type ${presenter.type} is missing an id (relationship '${key}' of ${this.constructor.type})`,\n )\n }\n return { id, type: presenter.type }\n }\n\n // Skip in payload mode: an omitted relationship in a write must not be\n // confused with the model simply not having loaded it.\n if (optional && !keyPresent && !isPayload) {\n if (hasLinks) {\n if (!relationships) relationships = {}\n relationships[key] = { links: buildLinks(linkValue) }\n }\n continue\n }\n\n if (!relationships) relationships = {}\n const rel: JsonApiRelationship = {}\n\n if (Array.isArray(data)) {\n rel.data = data.map(buildData)\n } else if (data != null) {\n rel.data = buildData(data)\n } else if (hasMany === true) {\n rel.data = []\n } else if (!hasLinks) {\n rel.data = null\n }\n\n if (hasLinks) {\n rel.links = buildLinks(linkValue)\n }\n\n relationships[key] = rel\n }\n return relationships\n }\n\n buildSelfLink(instance: ModelLike): JsonApiLink | undefined {\n return buildLinks(this.selfLinks(instance))\n }\n\n toJSON(\n instanceOrCollection: ModelLike | ModelLike[] | null | undefined,\n options?: PresenterOptions,\n ): JsonApiDocument {\n const opts = options ?? {}\n if (opts.meta != null) {\n this.scope.meta = opts.meta\n }\n if (opts.links != null) {\n this.scope.links = opts.links\n }\n if (!this.scope.data) {\n this.scope.data = null\n }\n\n if (instanceOrCollection == null) {\n return this.scope\n }\n\n if (Array.isArray(instanceOrCollection)) {\n const collection = instanceOrCollection\n if (!this.scope.data) {\n this.scope.data = []\n }\n collection.forEach((instance: ModelLike) => {\n return this.toJSON(instance, options)\n })\n } else {\n const instance = instanceOrCollection\n let added = true\n const model: JsonApiResource = {\n id: this.id(instance),\n type: this.constructor.type,\n attributes: this.attributes(instance),\n }\n if (model.id === undefined) {\n delete model.id\n }\n const relationships = this.buildRelationships(instance)\n if (relationships != null) {\n model.relationships = relationships\n }\n const links = this.buildSelfLink(instance)\n if (links != null) {\n model.links = links\n }\n\n if (opts.include) {\n if (!this.scope.included) {\n this.scope.included = []\n }\n const allResources = (this.scope.included || []).concat(\n Array.isArray(this.scope.data) ? this.scope.data : this.scope.data ? [this.scope.data] : [],\n )\n if (!allResources.some((i) => i.id === model.id && i.type === model.type)) {\n this.scope.included.push(model)\n } else {\n added = false\n }\n } else if (this.scope.data != null) {\n if (Array.isArray(this.scope.data)) {\n if (!this.scope.data.some((i) => i.id === model.id && i.type === model.type)) {\n this.scope.data.push(model)\n } else {\n added = false\n }\n } else {\n // data is a single object, convert to array if needed\n this.scope.data = model\n }\n } else {\n this.scope.data = model\n }\n\n if (added) {\n this.includeRelationships(this.scope, instance)\n }\n }\n return this.scope\n }\n\n payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument {\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 const model: JsonApiResource = {\n type: this.constructor.type,\n attributes: this.attributes(instance),\n }\n const id = this.id(instance)\n if (id != null) {\n model.id = id\n }\n const relationships = this.buildRelationships(instance, { payload: true })\n if (relationships != null) {\n model.relationships = relationships\n }\n const result: JsonApiDocument = { data: model }\n const opts = options ?? {}\n if (opts.meta != null) result.meta = opts.meta\n if (opts.links != null) result.links = opts.links\n return result\n }\n\n render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument {\n return this.toJSON(instanceOrCollection, options)\n }\n\n static toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument {\n return new this().toJSON(instanceOrCollection, options)\n }\n\n static render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument {\n return new this().render(instanceOrCollection, options)\n }\n\n static payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument {\n return new this().payload(instance, options)\n }\n }\n\n return Presenter\n}\n\nexport type Presenter = ReturnType<typeof createPresenter>\n","/**\n * Interface for Zod-like schema objects.\n * Any schema library that implements `parse` and `safeParse` methods will work.\n */\nexport interface ZodLikeSchema {\n parse: (data: unknown) => unknown\n safeParse: (data: unknown) => { success: true; data: unknown } | { success: false; error: unknown }\n}\n\nexport function isZodLikeSchema(schema: unknown): schema is ZodLikeSchema {\n return (\n schema != null &&\n typeof schema === 'object' &&\n 'parse' in schema &&\n typeof schema.parse === 'function' &&\n 'safeParse' in schema &&\n typeof schema.safeParse === 'function'\n )\n}\n\nexport interface ValidationResult {\n valid: boolean\n data: unknown\n error?: unknown\n}\n\nexport function validate(schema: unknown, data: unknown, strict: boolean): ValidationResult {\n if (!isZodLikeSchema(schema)) {\n throw new Error('Invalid schema: must have parse and safeParse methods')\n }\n\n if (strict) {\n const validData = schema.parse(data)\n return { valid: true, data: validData }\n } else {\n const result = schema.safeParse(data)\n if (result.success) {\n return { valid: true, data: result.data }\n } else {\n return { valid: false, data, error: result.error }\n }\n }\n}\n","import type {\n InferModelType,\n JsonApiDocument,\n JsonApiLink,\n JsonApiRelationships,\n JsonApiResourceIdentifier,\n SchemaRegistry,\n StoreModel,\n StoreModelWithOptionalId,\n StoreModels,\n StoreOptions,\n StoreRecord as StoreRecordType,\n StoreResult,\n ValidationError,\n} from './types.js'\nimport { TYPE, LINKS, META, REL_LINKS, REL_META } from './symbols.js'\nimport { validate } from './schema.js'\n\nfunction hasId<T extends StoreModelWithOptionalId>(model: T): model is T & { id: string | number } {\n return model.id != null\n}\n\nclass StoreRecord implements StoreRecordType {\n id: string | number\n type: string\n attributes?: Record<string, unknown>\n relationships?: JsonApiRelationships\n links?: JsonApiLink\n meta?: Record<string, unknown>\n\n constructor(options: StoreRecordType) {\n this.id = options.id\n this.type = options.type\n this.attributes = options.attributes\n this.relationships = options.relationships\n this.links = options.links\n this.meta = options.meta\n }\n}\n\nexport default class Store<S extends SchemaRegistry = SchemaRegistry> {\n records: StoreRecord[] = []\n schemas?: S\n strict: boolean\n validationErrors: ValidationError[] = []\n\n constructor(options?: StoreOptions<S>) {\n this.schemas = options?.schemas\n this.strict = options?.strict ?? false\n this.reset()\n }\n\n reset(): void {\n this.records = []\n this.validationErrors = []\n }\n\n #createStub(type: string, id: string | number): StoreModel {\n const stub: StoreModel = { id }\n stub[TYPE] = type\n return stub\n }\n\n #createModel(\n resource: {\n type: string\n id?: string | number | null\n attributes?: Record<string, unknown> | null\n relationships?: JsonApiRelationships | null\n meta?: Record<string, unknown> | null\n links?: JsonApiLink | null\n },\n options?: {\n models?: StoreModels\n includeRelMeta?: boolean\n },\n ): StoreModelWithOptionalId {\n const models = options?.models ?? {}\n\n const model: StoreModelWithOptionalId = { ...(resource.attributes || {}) }\n if (resource.id != null) {\n model.id = resource.id\n }\n const type = resource.type\n model[TYPE] = type\n if (resource.meta != null) {\n model[META] = resource.meta\n }\n if (resource.links != null) {\n model[LINKS] = resource.links\n }\n\n // Cache before resolving relationships (for circular refs)\n if (hasId(model)) {\n const idStr = String(model.id)\n if (!models[type]) {\n models[type] = {}\n }\n if (!models[type][idStr]) {\n models[type][idStr] = model\n }\n }\n\n if (resource.relationships != null) {\n const resolver = (ref: JsonApiResourceIdentifier): StoreModel => {\n return this.#findModel(ref.type, ref.id, models) ?? this.#createStub(ref.type, ref.id)\n }\n this.#resolveRelationships(model, resource.relationships, resolver, {\n includeRelMeta: options?.includeRelMeta,\n })\n }\n return model\n }\n\n #resolveRelationships(\n model: StoreModel | StoreModelWithOptionalId,\n relationships: JsonApiRelationships,\n resolver: (ref: JsonApiResourceIdentifier) => StoreModel | StoreModelWithOptionalId,\n options?: { includeRelMeta?: boolean },\n ): void {\n const includeRelMeta = options?.includeRelMeta ?? true\n\n for (const key in relationships) {\n const rel = relationships[key]\n const { data, links, meta } = rel\n\n model[key] = null\n if (data == null && links == null) {\n continue\n }\n\n if (Array.isArray(data)) {\n model[key] = data.filter((item) => item.id != null).map(resolver)\n } else if (data != null && data.id != null) {\n const relModel = resolver(data)\n if (includeRelMeta) {\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- REL_LINKS/REL_META only used when resolver returns StoreModel\n const modelWithMeta = relModel as StoreModel\n modelWithMeta[REL_LINKS] = links || {}\n modelWithMeta[REL_META] = meta || {}\n }\n model[key] = relModel\n } else if (data == null && (links != null || meta != null) && includeRelMeta) {\n const relModel: StoreModel = { id: '' }\n relModel[REL_LINKS] = links || {}\n relModel[REL_META] = meta || {}\n model[key] = relModel\n }\n }\n }\n\n toModel(rec: StoreRecord, type: string, models: StoreModels): StoreModel {\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- StoreRecord always has id\n const model = this.#createModel(rec, { models }) as StoreModel\n\n // Validate with schema if provided\n if (this.schemas && this.schemas[rec.type]) {\n const schema = this.schemas[rec.type]\n const result = validate(schema, model, this.strict)\n\n if (!result.valid) {\n this.validationErrors.push({\n type: rec.type,\n id: rec.id,\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 return validatedModel\n }\n\n return model\n }\n\n findRecord(type: string, id: string | number): StoreRecord | undefined {\n const idStr = String(id)\n return this.records.find((r) => r.type === type && String(r.id) === idStr)\n }\n\n findRecords(type: string): StoreRecord[] {\n return this.records.filter((r) => r.type === type)\n }\n\n #findModel(type: string, id: string | number, models: StoreModels): StoreModel | null {\n const idStr = String(id)\n const cached = models[type]?.[idStr]\n if (cached) {\n return cached\n }\n const rec = this.findRecord(type, id)\n if (rec == null) {\n return null\n }\n return this.toModel(rec, type, models)\n }\n\n find<T extends string>(type: T, id: string | number, models?: StoreModels): InferModelType<S, T> | null {\n const result = this.#findModel(type, id, models ?? {})\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Enable type inference from schema registry\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 ?? {}\n const recs = this.findRecords(type)\n if (recs == null) {\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 -- Enable type inference from schema registry\n return Object.values(modelsObj[type] || {}) as InferModelType<S, T>[]\n }\n\n remove(type: string, id?: string | number): void {\n const removeOne = (record: StoreRecord): 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 record = this.findRecord(type, String(id))\n if (record) {\n removeOne(record)\n }\n } else {\n this.findRecords(type).forEach(removeOne)\n }\n }\n\n syncAll(body: JsonApiDocument): StoreResult {\n // Clear previous validation errors\n this.validationErrors = []\n\n // Snapshot for rollback if strict validation throws\n const previousRecords = [...this.records]\n\n const syncData = (data: JsonApiDocument['data'] | JsonApiDocument['included']): StoreRecord[] => {\n if (data == null) {\n return []\n }\n const add = (obj: StoreRecordType): StoreRecord => {\n const { type, id } = obj\n this.remove(type, id)\n const rec = new StoreRecord(obj)\n this.records.push(rec)\n return rec\n }\n\n if (Array.isArray(data)) {\n return data.map((item) => {\n if (!item.id) {\n throw new Error(`Resource of type ${item.type} is missing an id`)\n }\n return add({\n ...item,\n attributes: item.attributes ?? undefined,\n relationships: item.relationships ?? undefined,\n id: item.id,\n })\n })\n } else {\n if (!data.id) {\n throw new Error(`Resource of type ${data.type} is missing an id`)\n }\n return [\n add({\n ...data,\n attributes: data.attributes ?? undefined,\n relationships: data.relationships ?? undefined,\n id: data.id,\n }),\n ]\n }\n }\n\n try {\n syncData(body.included)\n const recs = syncData(body.data)\n\n const models: StoreModels = {}\n const result: StoreResult = recs.map((rec) => this.toModel(rec, rec.type, models))\n if (body.meta != null) {\n result[META] = body.meta\n }\n return result\n } catch (e) {\n this.records = previousRecords\n throw e\n }\n }\n\n sync(body: JsonApiDocument): StoreModel | StoreResult {\n const result = this.syncAll(body)\n if (!Array.isArray(body.data) && body.data != null) {\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 /**\n * Build a model from a JSON:API document without storing it.\n * Useful for create payloads where id may be absent.\n *\n * Per JSON:API spec: \"The id member is not required when the resource object\n * originates at the client and represents a new resource to be created on the server.\"\n */\n static build(body: JsonApiDocument): StoreModelWithOptionalId {\n return new Store().build(body)\n }\n\n build(body: JsonApiDocument): StoreModelWithOptionalId {\n const { data } = body\n if (data == null || Array.isArray(data)) {\n throw new Error('build() expects a single resource in data, not null or an array')\n }\n return this.#createModel(data)\n }\n\n retrieve<T extends string>(type: T, body: JsonApiDocument): InferModelType<S, T> | null {\n const synced = this.syncAll(body)\n const model = synced.find((m) => m[TYPE] === type)\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 retrieveAll<T extends string>(type: T, body: JsonApiDocument): StoreResult<InferModelType<S, T>> {\n const synced = this.syncAll(body)\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((model) => model[TYPE] === type) as StoreResult<\n InferModelType<S, T>\n >\n result[META] = synced[META]\n return result\n }\n}\n","import Adapter from './yayson/adapter.js'\nimport * as adapters from './yayson/adapters/index.js'\nimport createPresenter, { Presenter } from './yayson/presenter.js'\nimport Store from './yayson/store.js'\nimport type {\n JsonApiDocument,\n JsonApiLink,\n JsonApiLinks,\n JsonApiRelationship,\n JsonApiRelationships,\n JsonApiResource,\n LegacyStoreOptions,\n PresenterOptions,\n SchemaRegistry,\n StoreOptions,\n ValidationError,\n} from './yayson/types.js'\nimport type { ZodLikeSchema } from './yayson/schema.js'\n\ntype AdapterOption = string | typeof Adapter\n\ninterface YaysonOptions {\n adapter?: AdapterOption\n}\n\ninterface YaysonResult {\n Store: typeof Store\n Presenter: Presenter\n Adapter: typeof Adapter\n}\n\nfunction lookupAdapter(nameOrAdapter?: AdapterOption): typeof Adapter {\n if (nameOrAdapter === 'default' || !nameOrAdapter) {\n return Adapter\n } else if (typeof nameOrAdapter === 'string') {\n if (nameOrAdapter === 'sequelize') {\n return adapters.sequelize\n } else {\n throw new Error('Adapter not found: ' + nameOrAdapter)\n }\n }\n return nameOrAdapter\n}\n\nfunction yayson(options?: YaysonOptions): YaysonResult {\n const adapter = lookupAdapter(options?.adapter)\n const Presenter = createPresenter(adapter)\n\n return {\n Store,\n Presenter,\n Adapter,\n }\n}\n\nexport default yayson\nexport type {\n Adapter,\n JsonApiDocument,\n JsonApiLink,\n JsonApiLinks,\n JsonApiRelationship,\n JsonApiRelationships,\n JsonApiResource,\n LegacyStoreOptions,\n PresenterOptions,\n SchemaRegistry,\n StoreOptions,\n ValidationError,\n YaysonOptions,\n YaysonResult,\n ZodLikeSchema,\n}\n"],"mappings":";;;AAQA,SAAS,iBAAiB,OAAyC;AACjE,QAAO,SAAS,QAAQ,OAAO,UAAU,YAAY,SAAS,SAAS,OAAO,MAAM,QAAQ;;AAG9F,IAAM,mBAAN,cAA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K,QAAQ,KAAK;AACjC,MAAI,CAAC,MAAM,QAAQ,KAAK,KAAK,IAAI,KAAK,QAAQ,MAAM;GAClD,MAAM,QAAQ,OAAO;AACrB,OAAI,OAAO,MACT,OAAM,QAAQ,OAAO;AAEvB,UAAO;;AAET,SAAO;;;;;;;;;CAUT,OAAO,MAAM,MAAiD;AAC5D,SAAO,IAAI,OAAO,CAAC,MAAM,KAAK;;CAGhC,MAAM,MAAiD;EACrD,MAAM,EAAE,SAAS;AACjB,MAAI,QAAQ,QAAQ,MAAM,QAAQ,KAAK,CACrC,OAAM,IAAI,MAAM,kEAAkE;AAEpF,SAAO,MAAKG,YAAa,KAAK;;CAGhC,SAA2B,MAAS,MAAoD;EACtF,MAAM,SAAS,KAAK,QAAQ,KAAK;EACjC,MAAM,QAAQ,OAAO,MAAM,MAAM,EAAE,UAAU,KAAK;AAClD,MAAI,CAAC,MAAO,QAAO;AACnB,MAAI,OAAO,MACT,OAAM,QAAQ,OAAO;AAGvB,SAAO;;CAGT,YAA8B,MAAS,MAA0D;EAC/F,MAAM,SAAS,KAAK,QAAQ,KAAK;EAEjC,MAAM,SAA4C,OAAO,QAAQ,UAAU,MAAM,UAAU,KAAK;AAGhG,SAAO,QAAQ,OAAO;AACtB,SAAO;;;;;;ACpUX,SAAS,cAAc,eAA+C;AACpE,KAAI,kBAAkB,aAAa,CAAC,cAClC,QAAOC;UACE,OAAO,kBAAkB,SAClC,KAAI,kBAAkB,YACpB,QAAOC;KAEP,OAAM,IAAI,MAAM,wBAAwB,cAAc;AAG1D,QAAO;;AAGT,SAAS,OAAO,SAAuC;AAIrD,QAAO;EACL;EACA,WAJgB,gBADF,cAAc,SAAS,QAAQ,CACL;EAKxC;EACD;;AAGH,qBAAe"}
@@ -1,4 +1,4 @@
1
- import { E as ModelLike, T as Adapter, _ as StoreResult, d as SchemaRegistry, f as StoreModel, g as StoreRecord$1, h as StoreOptions, i as JsonApiLinks, m as StoreModels, n as JsonApiDocument, o as JsonApiRelationships, p as StoreModelWithOptionalId, r as JsonApiLink, t as InferModelType, u as PresenterOptions, v as ValidationError } from "./types-Do2flKZX.mjs";
1
+ import { D as ModelLike, E as Adapter, _ as StoreRecord$1, d as RelationshipConfig, f as SchemaRegistry, g as StoreOptions, h as StoreModels, i as JsonApiLinks, m as StoreModelWithOptionalId, n as JsonApiDocument, o as JsonApiRelationships, p as StoreModel, r as JsonApiLink, t as InferModelType, u as PresenterOptions, v as StoreResult, y as ValidationError } from "./types-iC38_iCI.cjs";
2
2
 
3
3
  //#region src/yayson/presenter.d.ts
4
4
  declare function createPresenter(adapter: typeof Adapter): {
@@ -8,10 +8,12 @@ declare function createPresenter(adapter: typeof Adapter): {
8
8
  id(instance: ModelLike): string | undefined;
9
9
  selfLinks(_instance: ModelLike): JsonApiLink | string | undefined;
10
10
  links(_instance?: ModelLike): JsonApiLinks | undefined;
11
- relationships(): Record<string, /*elided*/any>;
11
+ relationships(): Record<string, /*elided*/any | RelationshipConfig< /*elided*/any>>;
12
12
  attributes(instance: ModelLike | null): Record<string, unknown>;
13
13
  includeRelationships(scope: JsonApiDocument, instance: ModelLike): unknown[];
14
- buildRelationships(instance: ModelLike | null): JsonApiRelationships | null;
14
+ buildRelationships(instance: ModelLike | null, options?: {
15
+ payload?: boolean;
16
+ }): JsonApiRelationships | null;
15
17
  buildSelfLink(instance: ModelLike): JsonApiLink | undefined;
16
18
  toJSON(instanceOrCollection: ModelLike | ModelLike[] | null | undefined, options?: PresenterOptions): JsonApiDocument;
17
19
  payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument;
@@ -78,4 +80,4 @@ interface YaysonResult {
78
80
  declare function yayson(options?: YaysonOptions): YaysonResult;
79
81
  //#endregion
80
82
  export { Presenter as i, YaysonResult as n, yayson as r, YaysonOptions as t };
81
- //# sourceMappingURL=yayson-Ce5uGpgU.d.mts.map
83
+ //# sourceMappingURL=yayson-DTMLeA5k.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"yayson-DTMLeA5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 const require_yayson = require('./yayson-C4P8J4sn.cjs');
2
2
 
3
3
  module.exports = require_yayson.yayson_default;
@@ -1,3 +1,3 @@
1
- import { T as Adapter, a as JsonApiRelationship, d as SchemaRegistry, h as StoreOptions, i as JsonApiLinks, l as LegacyStoreOptions, n as JsonApiDocument, o as JsonApiRelationships, r as JsonApiLink, s as JsonApiResource, u as PresenterOptions, v as ValidationError, y as ZodLikeSchema } from "./types-NiKV-lj-.cjs";
2
- import { n as YaysonResult, r as yayson, t as YaysonOptions } from "./yayson-3UYKq2H5.cjs";
1
+ import { E as Adapter, a as JsonApiRelationship, b as ZodLikeSchema, f as SchemaRegistry, g as StoreOptions, i as JsonApiLinks, l as LegacyStoreOptions, n as JsonApiDocument, o as JsonApiRelationships, r as JsonApiLink, s as JsonApiResource, u as PresenterOptions, y as ValidationError } from "./types-iC38_iCI.cjs";
2
+ import { n as YaysonResult, r as yayson, t as YaysonOptions } from "./yayson-DTMLeA5k.cjs";
3
3
  export { Adapter, JsonApiDocument, JsonApiLink, JsonApiLinks, JsonApiRelationship, JsonApiRelationships, JsonApiResource, LegacyStoreOptions, PresenterOptions, SchemaRegistry, StoreOptions, ValidationError, YaysonOptions, YaysonResult, ZodLikeSchema, yayson as default };
@@ -1,3 +1,3 @@
1
- import { T as Adapter, a as JsonApiRelationship, d as SchemaRegistry, h as StoreOptions, i as JsonApiLinks, l as LegacyStoreOptions, n as JsonApiDocument, o as JsonApiRelationships, r as JsonApiLink, s as JsonApiResource, u as PresenterOptions, v as ValidationError, y as ZodLikeSchema } from "./types-Do2flKZX.mjs";
2
- import { n as YaysonResult, r as yayson, t as YaysonOptions } from "./yayson-Ce5uGpgU.mjs";
1
+ import { E as Adapter, a as JsonApiRelationship, b as ZodLikeSchema, f as SchemaRegistry, g as StoreOptions, i as JsonApiLinks, l as LegacyStoreOptions, n as JsonApiDocument, o as JsonApiRelationships, r as JsonApiLink, s as JsonApiResource, u as PresenterOptions, y as ValidationError } from "./types-KZiF6x7A.mjs";
2
+ import { n as YaysonResult, r as yayson, t as YaysonOptions } from "./yayson-BvwMr4Ad.mjs";
3
3
  export { Adapter, JsonApiDocument, JsonApiLink, JsonApiLinks, JsonApiRelationship, JsonApiRelationships, JsonApiResource, LegacyStoreOptions, PresenterOptions, SchemaRegistry, StoreOptions, ValidationError, YaysonOptions, YaysonResult, ZodLikeSchema, yayson as default };
package/build/yayson.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { t as yayson_default } from "./yayson-CwZg5FNt.mjs";
1
+ import { t as yayson_default } from "./yayson-DIQ_olLX.mjs";
2
2
 
3
3
  export { yayson_default as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yayson",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "description": "A library for serializing and reading JSON API standardized data in JavaScript.",
5
5
  "type": "module",
6
6
  "main": "./build/yayson.cjs",
@@ -68,6 +68,26 @@ BikePresenter.render({ id: 1, wheels: [{ id: 10 }, { id: 11 }] })
68
68
  // included = [{ type: 'wheels', id: '10', ... }, ...]
69
69
  ```
70
70
 
71
+ For to-many relationships and conditionally loaded (`?include=`) responses, declare cardinality and/or optional semantics via the config form:
72
+
73
+ ```typescript
74
+ class TicketPresenter extends Presenter {
75
+ static type = 'tickets'
76
+ relationships() {
77
+ return {
78
+ addons: { presenter: AddonPresenter, hasMany: true }, // empty → data: []
79
+ parentTicket: { presenter: TicketPresenter, optional: true }, // key absent → omitted
80
+ guestTickets: { presenter: TicketPresenter, hasMany: true, optional: true },
81
+ }
82
+ }
83
+ }
84
+ ```
85
+
86
+ - `hasMany: true` → empty/missing data renders as `data: []` instead of `data: null` (spec-compliant for to-many).
87
+ - `optional: true` → when the relationship key is absent from the instance, the relationship is omitted from output entirely (or rendered as `{ links }` only if `links()` configures one for that key). An explicit `null` on the instance still renders as `data: null` — `optional` distinguishes "not loaded" from "explicitly empty".
88
+
89
+ The bare-class form is unchanged.
90
+
71
91
  ### Custom attributes
72
92
 
73
93
  Override `attributes()` to transform or compute attributes.
@@ -35,9 +35,9 @@ function yayson(options?: { adapter?: 'default' | 'sequelize' | AdapterClass }):
35
35
 
36
36
  ### Instance Methods
37
37
 
38
- #### `relationships(): Record<string, typeof Presenter>`
38
+ #### `relationships(): Record<string, typeof Presenter | RelationshipConfig>`
39
39
 
40
- Return map of property names to their Presenter classes. Relationship keys are automatically excluded from attributes.
40
+ Return map of property names to their Presenter classes (or a config object). Relationship keys are automatically excluded from attributes.
41
41
 
42
42
  ```typescript
43
43
  relationships() {
@@ -45,6 +45,27 @@ relationships() {
45
45
  }
46
46
  ```
47
47
 
48
+ Use the config form to declare cardinality or conditional-include semantics:
49
+
50
+ ```typescript
51
+ relationships() {
52
+ return {
53
+ addons: { presenter: AddonPresenter, hasMany: true },
54
+ parentTicket: { presenter: TicketPresenter, optional: true },
55
+ guestTickets: { presenter: TicketPresenter, hasMany: true, optional: true },
56
+ }
57
+ }
58
+ ```
59
+
60
+ | Flag | Type | Effect |
61
+ | ---------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
62
+ | `hasMany` | `boolean` | Declares a to-many relationship. Empty/missing data renders as `data: []` (JSON:API requires `[]`, not `null`, for empty to-many). |
63
+ | `optional` | `boolean` | When the key is absent from the instance, the relationship is omitted entirely (or rendered with only `links` if `links()` provides them for the key). Explicit `null`/`[]` values on the instance still render normally — the distinction is "absent" vs "empty". |
64
+
65
+ The bare-class form (`Presenter` directly) is unchanged: missing data renders as `data: null` regardless of cardinality.
66
+
67
+ In `payload()` output, `optional` omission is disabled (a write request asserts state, so dropping a relationship would be misleading), but `hasMany: true` still applies so a client can correctly clear a to-many relationship with `data: []`.
68
+
48
69
  #### `attributes(instance): Record<string, unknown>`
49
70
 
50
71
  Return attributes for the resource. Default strips `id` and relationship keys, then applies `fields` filter. Override to customize.
@@ -221,6 +242,7 @@ Works with plain JS objects.
221
242
  Adapter.get(model) // returns shallow copy of all properties
222
243
  Adapter.get(model, 'key') // returns single property value
223
244
  Adapter.id(model) // returns model.id as string
245
+ Adapter.has(model, 'key') // returns true if key is present on the model (used by optional relationships)
224
246
  ```
225
247
 
226
248
  ### Sequelize Adapter
@@ -240,10 +262,13 @@ const { Presenter } = yayson({
240
262
  adapter: {
241
263
  id: (model) => String(model.pk),
242
264
  get: (model, key) => (key ? model.attrs[key] : model.attrs),
265
+ has: (model, key) => key in model.attrs,
243
266
  },
244
267
  })
245
268
  ```
246
269
 
270
+ Implementing `has(model, key)` is only required when using `optional: true` relationships. It tells the presenter whether a relationship key is actually present on the model (so an unloaded relationship can be omitted) versus loaded-but-null. The default adapter uses `key in model`; custom adapters that store data on a sub-property (like `model.attrs` above) need to override it.
271
+
247
272
  ## Type Inference
248
273
 
249
274
  With schema registry, store methods infer TypeScript types automatically:
@@ -1 +0,0 @@
1
- {"version":3,"file":"symbols-DSjKJ8vh.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\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;;;AAId,sBAAe;;;;ACvBf,MAAa,OAAsB,OAAO,cAAc;AACxD,MAAa,QAAuB,OAAO,eAAe;AAC1D,MAAa,OAAsB,OAAO,cAAc;AACxD,MAAa,YAA2B,OAAO,mBAAmB;AAClE,MAAa,WAA0B,OAAO,kBAAkB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"types-Do2flKZX.d.mts","names":[],"sources":["../../../../../../yayson/adapter.ts","../../../../../../yayson/symbols.ts","../../../../../../yayson/schema.ts","../../../../../../yayson/types.ts"],"mappings":";KAAY,SAAA;AAAA,cAEN,OAAA;EAAA,OACG,GAAA,CAAI,KAAA,EAAO,SAAA,GAAY,MAAA;EAAA,OACvB,GAAA,CAAI,KAAA,EAAO,SAAA,EAAW,GAAA;EAAA,OAUtB,EAAA,CAAG,KAAA,EAAO,SAAA;AAAA;;;cCdN,IAAA;AAAA,cACA,KAAA;AAAA,cACA,IAAA;AAAA,cACA,SAAA;AAAA,cACA,QAAA;;;;ADJb;;;UEIiB,aAAA;EACf,KAAA,GAAQ,IAAA;EACR,SAAA,GAAY,IAAA;IAAoB,OAAA;IAAe,IAAA;EAAA;IAAoB,OAAA;IAAgB,KAAA;EAAA;AAAA;;;UCHpE,WAAA,SAAoB,MAAA;EACnC,IAAA;EACA,OAAA;AAAA;AAAA,UAGe,YAAA;EAAA,CACd,GAAA,WAAc,WAAA;AAAA;AAAA,UAGA,yBAAA;EACf,EAAA;EACA,IAAA;AAAA;AAAA,UAGe,mBAAA;EACf,IAAA,GAAO,yBAAA,GAA4B,yBAAA;EACnC,KAAA,GAAQ,WAAA;EACR,IAAA,GAAO,MAAA;AAAA;AAAA,UAGQ,oBAAA;EAAA,CACd,GAAA,WAAc,mBAAA;AAAA;AAAA,UAGA,eAAA;EACf,EAAA;EACA,IAAA;EACA,UAAA,GAAa,MAA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A,CACxD,IAAA,IAAQ,MAAA;AAAA;AAAA,UAGM,YAAA,WAAuB,cAAA,GAAiB,cAAA;EACvD,OAAA,GAAU,CAAA;EACV,MAAA;AAAA;AAAA,UAGe,eAAA;EACf,IAAA;EACA,EAAA;EACA,KAAA;AAAA;AAAA,UAGe,kBAAA,WAA6B,cAAA,GAAiB,cAAA;EAC7D,KAAA,GAAQ,MAAA;EACR,OAAA,GAAU,CAAA;EACV,MAAA;AAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"types-NiKV-lj-.d.cts","names":[],"sources":["../../../../../../yayson/adapter.ts","../../../../../../yayson/symbols.ts","../../../../../../yayson/schema.ts","../../../../../../yayson/types.ts"],"mappings":";KAAY,SAAA;AAAA,cAEN,OAAA;EAAA,OACG,GAAA,CAAI,KAAA,EAAO,SAAA,GAAY,MAAA;EAAA,OACvB,GAAA,CAAI,KAAA,EAAO,SAAA,EAAW,GAAA;EAAA,OAUtB,EAAA,CAAG,KAAA,EAAO,SAAA;AAAA;;;cCdN,IAAA;AAAA,cACA,KAAA;AAAA,cACA,IAAA;AAAA,cACA,SAAA;AAAA,cACA,QAAA;;;;ADJb;;;UEIiB,aAAA;EACf,KAAA,GAAQ,IAAA;EACR,SAAA,GAAY,IAAA;IAAoB,OAAA;IAAe,IAAA;EAAA;IAAoB,OAAA;IAAgB,KAAA;EAAA;AAAA;;;UCHpE,WAAA,SAAoB,MAAA;EACnC,IAAA;EACA,OAAA;AAAA;AAAA,UAGe,YAAA;EAAA,CACd,GAAA,WAAc,WAAA;AAAA;AAAA,UAGA,yBAAA;EACf,EAAA;EACA,IAAA;AAAA;AAAA,UAGe,mBAAA;EACf,IAAA,GAAO,yBAAA,GAA4B,yBAAA;EACnC,KAAA,GAAQ,WAAA;EACR,IAAA,GAAO,MAAA;AAAA;AAAA,UAGQ,oBAAA;EAAA,CACd,GAAA,WAAc,mBAAA;AAAA;AAAA,UAGA,eAAA;EACf,EAAA;EACA,IAAA;EACA,UAAA,GAAa,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EAAA,CACxD,IAAA,IAAQ,MAAA;AAAA;AAAA,UAGM,YAAA,WAAuB,cAAA,GAAiB,cAAA;EACvD,OAAA,GAAU,CAAA;EACV,MAAA;AAAA;AAAA,UAGe,eAAA;EACf,IAAA;EACA,EAAA;EACA,KAAA;AAAA;AAAA,UAGe,kBAAA,WAA6B,cAAA,GAAiB,cAAA;EAC7D,KAAA,GAAQ,MAAA;EACR,OAAA,GAAU,CAAA;EACV,MAAA;AAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"yayson-3UYKq2H5.d.cts","names":[],"sources":["../../../../../../yayson/presenter.ts","../../../../../../yayson/store.ts","../../../../../../yayson.ts"],"mappings":";;;iBAyBwB,eAAA,CAAgB,OAAA,SAAgB,OAAA;EAAA,aAUhC,eAAA;iBAVe;WAQ5B,eAAA;iBAMM,SAAA;yBAIQ,SAAA,GAAY,WAAA;sBAIf,SAAA,GAAY,YAAA;qBAIb,MAAA,SAZJ;yBAgBQ,SAAA,UAAmB,MAAA;gCAiBZ,eAAA,EAAe,QAAA,EAAY,SAAA;iCAoB1B,SAAA,UAAmB,oBAAA;4BA0DxB,SAAA,GAAY,WAAA;iCAKZ,SAAA,GAAY,SAAA,uBAA8B,OAAA,GACtD,gBAAA,GACT,eAAA;sBA8Ee,SAAA,EAAS,OAAA,GAAY,gBAAA,GAAmB,eAAA;iCA0B7B,SAAA,GAAY,SAAA,WAAkB,OAAA,GAAY,gBAAA,GAAmB,eAAA;EAAA;;;;+BAItD,SAAA,GAAY,SAAA,WAAkB,OAAA,GAAY,gBAAA,GAAmB,eAAA;+BAI7D,SAAA,GAAY,SAAA,WAAkB,OAAA,GAAY,gBAAA,GAAmB,eAAA;oBAIxE,SAAA,EAAS,OAAA,GAAY,gBAAA,GAAmB,eAAA;AAAA;AAAA,KAQzD,SAAA,GAAY,UAAA,QAAkB,eAAA;;;cCnQpC,WAAA,YAAuB,aAAA;EAC3B,EAAA;EACA,IAAA;EACA,UAAA,GAAa,MAAA;EACb,a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c,KAAA;EACd,SAAA,EAAW,SAAA;EACX,OAAA,SAAgB,OAAA;AAAA;AAAA,iBAgBT,MAAA,CAAO,OAAA,GAAU,aAAA,GAAgB,YAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"yayson-CwZg5FNt.mjs","names":["Adapter","#findModel","#createStub","#resolveRelationships","#createModel","Adapter","adapters.sequelize"],"sources":["../src/yayson/adapters/sequelize.ts","../src/yayson/utils.ts","../src/yayson/presenter.ts","../src/yayson/schema.ts","../src/yayson/store.ts","../src/yayson.ts"],"sourcesContent":["import Adapter, { type ModelLike } from '../adapter.js'\n\ninterface SequelizeModel {\n get(key?: string): unknown\n constructor?: {\n primaryKeys?: Record<string, unknown>\n }\n}\nfunction isSequelizeModel(model: unknown): model is SequelizeModel {\n return model != null && typeof model === 'object' && 'get' in model && typeof model.get === 'function'\n}\n\nclass SequelizeAdapter extends Adapter {\n static override get(model: ModelLike): Record<string, unknown>\n static override get(model: ModelLike, key: string): unknown\n static override get(model: ModelLike, key?: string): unknown {\n if (isSequelizeModel(model)) {\n return model.get(key)\n }\n return undefined\n }\n\n static override id(model: ModelLike): string | undefined {\n // Retain backwards compatibility with older sequelize versions\n const hasPrimaryKeys = model.constructor && 'primaryKeys' in model.constructor\n const pkFields = hasPrimaryKeys\n ? // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Access primaryKeys for Sequelize v3/v4 compatibility\n Object.keys((model.constructor as unknown as { primaryKeys: Record<string, unknown> }).primaryKeys)\n : ['id']\n\n if (pkFields.length > 1) {\n throw new Error(\n 'YAYSON does not support Sequelize models with composite primary keys. You can only use one column for your primary key. Currently using: ' +\n pkFields.join(','),\n )\n } else if (pkFields.length < 1) {\n throw new Error(\n 'YAYSON can only serialize Sequelize models which have a primary key. This is used for the JSON:API model id.',\n )\n }\n\n const id = this.get(model, pkFields[0])\n if (id == null) {\n return undefined\n }\n return `${id}`\n }\n}\n\nexport default SequelizeAdapter\n","/**\n * Filters an attributes object to only include keys in the fields array.\n * Returns the original attributes if fields is undefined.\n */\nexport function filterByFields(\n attributes: Record<string, unknown>,\n fields: string[] | undefined,\n): Record<string, unknown> {\n if (!fields) {\n return attributes\n }\n const filtered: Record<string, unknown> = {}\n for (const key of fields) {\n if (key in attributes) {\n filtered[key] = attributes[key]\n }\n }\n return filtered\n}\n","import Adapter, { ModelLike } from './adapter.js'\nimport type {\n JsonApiDocument,\n JsonApiLink,\n JsonApiLinks,\n JsonApiRelationship,\n JsonApiRelationships,\n JsonApiResource,\n JsonApiResourceIdentifier,\n PresenterOptions,\n} from './types.js'\nimport { filterByFields } from './utils.js'\n\nfunction buildLinks(link: JsonApiLink | string | null | undefined): JsonApiLink | undefined {\n if (link == null) {\n return\n }\n if (typeof link === 'object' && (link.self != null || link.related != null)) {\n return link\n } else {\n return { self: String(link) }\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type -- Return type is inferred from class\nexport default function createPresenter(adapter: typeof Adapter) {\n class Presenter {\n declare ['constructor']: typeof Presenter\n\n static adapter = adapter\n static type = 'objects'\n static fields?: string[]\n\n scope: JsonApiDocument\n\n constructor(scope?: JsonApiDocument) {\n this.scope = scope ?? { data: null }\n }\n\n id(instance: ModelLike): string | undefined {\n return this.constructor.adapter.id(instance)\n }\n\n selfLinks(_instance: ModelLike): JsonApiLink | string | undefined {\n return undefined\n }\n\n links(_instance?: ModelLike): JsonApiLinks | undefined {\n return undefined\n }\n\n relationships(): Record<string, typeof Presenter> {\n return {}\n }\n\n attributes(instance: ModelLike | null): Record<string, unknown> {\n if (instance == null) {\n return {}\n }\n const attributes = { ...this.constructor.adapter.get(instance) }\n delete attributes['id']\n\n const relationships = this.relationships()\n if (relationships) {\n for (const key in relationships) {\n delete attributes[key]\n }\n }\n\n return filterByFields(attributes, this.constructor.fields)\n }\n\n includeRelationships(scope: JsonApiDocument, instance: ModelLike): unknown[] {\n const relationships = this.relationships()\n const result: unknown[] = []\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 const presenter = new factory(scope)\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 result.push(presenter.toJSON(data, { include: true }))\n }\n return result\n }\n\n buildRelationships(instance: ModelLike | null): JsonApiRelationships | null {\n if (instance == null) {\n return null\n }\n const rels = this.relationships()\n const links = this.links(instance) || {}\n let relationships: JsonApiRelationships | null = null\n\n if (!rels) {\n return null\n }\n\n for (const key in rels) {\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 | undefined\n const presenter = rels[key]\n const buildData = (d: ModelLike): JsonApiResourceIdentifier => {\n const id = this.constructor.adapter.id(d)\n if (!id) {\n throw new Error(\n `Model of type ${presenter.type} is missing an id (relationship '${key}' of ${this.constructor.type})`,\n )\n }\n return {\n id,\n type: presenter.type,\n }\n }\n const build = (d: ModelLike | null | undefined): JsonApiRelationship => {\n const rel: JsonApiRelationship = {}\n if (d != null) {\n rel.data = buildData(d)\n }\n if (links[key] != null) {\n rel.links = buildLinks(links[key])\n } else if (d == null) {\n rel.data = null\n }\n return rel\n }\n if (!relationships) {\n relationships = {}\n }\n if (!relationships[key]) {\n relationships[key] = {}\n }\n if (Array.isArray(data)) {\n relationships[key].data = data.map(buildData)\n if (links[key] != null) {\n relationships[key].links = buildLinks(links[key])\n }\n } else {\n relationships[key] = build(data)\n }\n }\n return relationships\n }\n\n buildSelfLink(instance: ModelLike): JsonApiLink | undefined {\n return buildLinks(this.selfLinks(instance))\n }\n\n toJSON(\n instanceOrCollection: ModelLike | ModelLike[] | null | undefined,\n options?: PresenterOptions,\n ): JsonApiDocument {\n const opts = options ?? {}\n if (opts.meta != null) {\n this.scope.meta = opts.meta\n }\n if (opts.links != null) {\n this.scope.links = opts.links\n }\n if (!this.scope.data) {\n this.scope.data = null\n }\n\n if (instanceOrCollection == null) {\n return this.scope\n }\n\n if (Array.isArray(instanceOrCollection)) {\n const collection = instanceOrCollection\n if (!this.scope.data) {\n this.scope.data = []\n }\n collection.forEach((instance: ModelLike) => {\n return this.toJSON(instance, options)\n })\n } else {\n const instance = instanceOrCollection\n let added = true\n const model: JsonApiResource = {\n id: this.id(instance),\n type: this.constructor.type,\n attributes: this.attributes(instance),\n }\n if (model.id === undefined) {\n delete model.id\n }\n const relationships = this.buildRelationships(instance)\n if (relationships != null) {\n model.relationships = relationships\n }\n const links = this.buildSelfLink(instance)\n if (links != null) {\n model.links = links\n }\n\n if (opts.include) {\n if (!this.scope.included) {\n this.scope.included = []\n }\n const allResources = (this.scope.included || []).concat(\n Array.isArray(this.scope.data) ? this.scope.data : this.scope.data ? [this.scope.data] : [],\n )\n if (!allResources.some((i) => i.id === model.id && i.type === model.type)) {\n this.scope.included.push(model)\n } else {\n added = false\n }\n } else if (this.scope.data != null) {\n if (Array.isArray(this.scope.data)) {\n if (!this.scope.data.some((i) => i.id === model.id && i.type === model.type)) {\n this.scope.data.push(model)\n } else {\n added = false\n }\n } else {\n // data is a single object, convert to array if needed\n this.scope.data = model\n }\n } else {\n this.scope.data = model\n }\n\n if (added) {\n this.includeRelationships(this.scope, instance)\n }\n }\n return this.scope\n }\n\n payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument {\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 const model: JsonApiResource = {\n type: this.constructor.type,\n attributes: this.attributes(instance),\n }\n const id = this.id(instance)\n if (id != null) {\n model.id = id\n }\n const relationships = this.buildRelationships(instance)\n if (relationships != null) {\n model.relationships = relationships\n }\n const result: JsonApiDocument = { data: model }\n const opts = options ?? {}\n if (opts.meta != null) result.meta = opts.meta\n if (opts.links != null) result.links = opts.links\n return result\n }\n\n render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument {\n return this.toJSON(instanceOrCollection, options)\n }\n\n static toJSON(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument {\n return new this().toJSON(instanceOrCollection, options)\n }\n\n static render(instanceOrCollection: ModelLike | ModelLike[] | null, options?: PresenterOptions): JsonApiDocument {\n return new this().render(instanceOrCollection, options)\n }\n\n static payload(instance: ModelLike, options?: PresenterOptions): JsonApiDocument {\n return new this().payload(instance, options)\n }\n }\n\n return Presenter\n}\n\nexport type Presenter = ReturnType<typeof createPresenter>\n","/**\n * Interface for Zod-like schema objects.\n * Any schema library that implements `parse` and `safeParse` methods will work.\n */\nexport interface ZodLikeSchema {\n parse: (data: unknown) => unknown\n safeParse: (data: unknown) => { success: true; data: unknown } | { success: false; error: unknown }\n}\n\nexport function isZodLikeSchema(schema: unknown): schema is ZodLikeSchema {\n return (\n schema != null &&\n typeof schema === 'object' &&\n 'parse' in schema &&\n typeof schema.parse === 'function' &&\n 'safeParse' in schema &&\n typeof schema.safeParse === 'function'\n )\n}\n\nexport interface ValidationResult {\n valid: boolean\n data: unknown\n error?: unknown\n}\n\nexport function validate(schema: unknown, data: unknown, strict: boolean): ValidationResult {\n if (!isZodLikeSchema(schema)) {\n throw new Error('Invalid schema: must have parse and safeParse methods')\n }\n\n if (strict) {\n const validData = schema.parse(data)\n return { valid: true, data: validData }\n } else {\n const result = schema.safeParse(data)\n if (result.success) {\n return { valid: true, data: result.data }\n } else {\n return { valid: false, data, error: result.error }\n }\n }\n}\n","import type {\n InferModelType,\n JsonApiDocument,\n JsonApiLink,\n JsonApiRelationships,\n JsonApiResourceIdentifier,\n SchemaRegistry,\n StoreModel,\n StoreModelWithOptionalId,\n StoreModels,\n StoreOptions,\n StoreRecord as StoreRecordType,\n StoreResult,\n ValidationError,\n} from './types.js'\nimport { TYPE, LINKS, META, REL_LINKS, REL_META } from './symbols.js'\nimport { validate } from './schema.js'\n\nfunction hasId<T extends StoreModelWithOptionalId>(model: T): model is T & { id: string | number } {\n return model.id != null\n}\n\nclass StoreRecord implements StoreRecordType {\n id: string | number\n type: string\n attributes?: Record<string, unknown>\n relationships?: JsonApiRelationships\n links?: JsonApiLink\n meta?: Record<string, unknown>\n\n constructor(options: StoreRecordType) {\n this.id = options.id\n this.type = options.type\n this.attributes = options.attributes\n this.relationships = options.relationships\n this.links = options.links\n this.meta = options.meta\n }\n}\n\nexport default class Store<S extends SchemaRegistry = SchemaRegistry> {\n records: StoreRecord[] = []\n schemas?: S\n strict: boolean\n validationErrors: ValidationError[] = []\n\n constructor(options?: StoreOptions<S>) {\n this.schemas = options?.schemas\n this.strict = options?.strict ?? false\n this.reset()\n }\n\n reset(): void {\n this.records = []\n this.validationErrors = []\n }\n\n #createStub(type: string, id: string | number): StoreModel {\n const stub: StoreModel = { id }\n stub[TYPE] = type\n return stub\n }\n\n #createModel(\n resource: {\n type: string\n id?: string | number | null\n attributes?: Record<string, unknown> | null\n relationships?: JsonApiRelationships | null\n meta?: Record<string, unknown> | null\n links?: JsonApiLink | null\n },\n options?: {\n models?: StoreModels\n includeRelMeta?: boolean\n },\n ): StoreModelWithOptionalId {\n const models = options?.models ?? {}\n\n const model: StoreModelWithOptionalId = { ...(resource.attributes || {}) }\n if (resource.id != null) {\n model.id = resource.id\n }\n const type = resource.type\n model[TYPE] = type\n if (resource.meta != null) {\n model[META] = resource.meta\n }\n if (resource.links != null) {\n model[LINKS] = resource.links\n }\n\n // Cache before resolving relationships (for circular refs)\n if (hasId(model)) {\n const idStr = String(model.id)\n if (!models[type]) {\n models[type] = {}\n }\n if (!models[type][idStr]) {\n models[type][idStr] = model\n }\n }\n\n if (resource.relationships != null) {\n const resolver = (ref: JsonApiResourceIdentifier): StoreModel => {\n return this.#findModel(ref.type, ref.id, models) ?? this.#createStub(ref.type, ref.id)\n }\n this.#resolveRelationships(model, resource.relationships, resolver, {\n includeRelMeta: options?.includeRelMeta,\n })\n }\n return model\n }\n\n #resolveRelationships(\n model: StoreModel | StoreModelWithOptionalId,\n relationships: JsonApiRelationships,\n resolver: (ref: JsonApiResourceIdentifier) => StoreModel | StoreModelWithOptionalId,\n options?: { includeRelMeta?: boolean },\n ): void {\n const includeRelMeta = options?.includeRelMeta ?? true\n\n for (const key in relationships) {\n const rel = relationships[key]\n const { data, links, meta } = rel\n\n model[key] = null\n if (data == null && links == null) {\n continue\n }\n\n if (Array.isArray(data)) {\n model[key] = data.filter((item) => item.id != null).map(resolver)\n } else if (data != null && data.id != null) {\n const relModel = resolver(data)\n if (includeRelMeta) {\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- REL_LINKS/REL_META only used when resolver returns StoreModel\n const modelWithMeta = relModel as StoreModel\n modelWithMeta[REL_LINKS] = links || {}\n modelWithMeta[REL_META] = meta || {}\n }\n model[key] = relModel\n } else if (data == null && (links != null || meta != null) && includeRelMeta) {\n const relModel: StoreModel = { id: '' }\n relModel[REL_LINKS] = links || {}\n relModel[REL_META] = meta || {}\n model[key] = relModel\n }\n }\n }\n\n toModel(rec: StoreRecord, type: string, models: StoreModels): StoreModel {\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- StoreRecord always has id\n const model = this.#createModel(rec, { models }) as StoreModel\n\n // Validate with schema if provided\n if (this.schemas && this.schemas[rec.type]) {\n const schema = this.schemas[rec.type]\n const result = validate(schema, model, this.strict)\n\n if (!result.valid) {\n this.validationErrors.push({\n type: rec.type,\n id: rec.id,\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 return validatedModel\n }\n\n return model\n }\n\n findRecord(type: string, id: string | number): StoreRecord | undefined {\n const idStr = String(id)\n return this.records.find((r) => r.type === type && String(r.id) === idStr)\n }\n\n findRecords(type: string): StoreRecord[] {\n return this.records.filter((r) => r.type === type)\n }\n\n #findModel(type: string, id: string | number, models: StoreModels): StoreModel | null {\n const idStr = String(id)\n const cached = models[type]?.[idStr]\n if (cached) {\n return cached\n }\n const rec = this.findRecord(type, id)\n if (rec == null) {\n return null\n }\n return this.toModel(rec, type, models)\n }\n\n find<T extends string>(type: T, id: string | number, models?: StoreModels): InferModelType<S, T> | null {\n const result = this.#findModel(type, id, models ?? {})\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Enable type inference from schema registry\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 ?? {}\n const recs = this.findRecords(type)\n if (recs == null) {\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 -- Enable type inference from schema registry\n return Object.values(modelsObj[type] || {}) as InferModelType<S, T>[]\n }\n\n remove(type: string, id?: string | number): void {\n const removeOne = (record: StoreRecord): 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 record = this.findRecord(type, String(id))\n if (record) {\n removeOne(record)\n }\n } else {\n this.findRecords(type).forEach(removeOne)\n }\n }\n\n syncAll(body: JsonApiDocument): StoreResult {\n // Clear previous validation errors\n this.validationErrors = []\n\n // Snapshot for rollback if strict validation throws\n const previousRecords = [...this.records]\n\n const syncData = (data: JsonApiDocument['data'] | JsonApiDocument['included']): StoreRecord[] => {\n if (data == null) {\n return []\n }\n const add = (obj: StoreRecordType): StoreRecord => {\n const { type, id } = obj\n this.remove(type, id)\n const rec = new StoreRecord(obj)\n this.records.push(rec)\n return rec\n }\n\n if (Array.isArray(data)) {\n return data.map((item) => {\n if (!item.id) {\n throw new Error(`Resource of type ${item.type} is missing an id`)\n }\n return add({\n ...item,\n attributes: item.attributes ?? undefined,\n relationships: item.relationships ?? undefined,\n id: item.id,\n })\n })\n } else {\n if (!data.id) {\n throw new Error(`Resource of type ${data.type} is missing an id`)\n }\n return [\n add({\n ...data,\n attributes: data.attributes ?? undefined,\n relationships: data.relationships ?? undefined,\n id: data.id,\n }),\n ]\n }\n }\n\n try {\n syncData(body.included)\n const recs = syncData(body.data)\n\n const models: StoreModels = {}\n const result: StoreResult = recs.map((rec) => this.toModel(rec, rec.type, models))\n if (body.meta != null) {\n result[META] = body.meta\n }\n return result\n } catch (e) {\n this.records = previousRecords\n throw e\n }\n }\n\n sync(body: JsonApiDocument): StoreModel | StoreResult {\n const result = this.syncAll(body)\n if (!Array.isArray(body.data) && body.data != null) {\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 /**\n * Build a model from a JSON:API document without storing it.\n * Useful for create payloads where id may be absent.\n *\n * Per JSON:API spec: \"The id member is not required when the resource object\n * originates at the client and represents a new resource to be created on the server.\"\n */\n static build(body: JsonApiDocument): StoreModelWithOptionalId {\n return new Store().build(body)\n }\n\n build(body: JsonApiDocument): StoreModelWithOptionalId {\n const { data } = body\n if (data == null || Array.isArray(data)) {\n throw new Error('build() expects a single resource in data, not null or an array')\n }\n return this.#createModel(data)\n }\n\n retrieve<T extends string>(type: T, body: JsonApiDocument): InferModelType<S, T> | null {\n const synced = this.syncAll(body)\n const model = synced.find((m) => m[TYPE] === type)\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 retrieveAll<T extends string>(type: T, body: JsonApiDocument): StoreResult<InferModelType<S, T>> {\n const synced = this.syncAll(body)\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((model) => model[TYPE] === type) as StoreResult<\n InferModelType<S, T>\n >\n result[META] = synced[META]\n return result\n }\n}\n","import Adapter from './yayson/adapter.js'\nimport * as adapters from './yayson/adapters/index.js'\nimport createPresenter, { Presenter } from './yayson/presenter.js'\nimport Store from './yayson/store.js'\nimport type {\n JsonApiDocument,\n JsonApiLink,\n JsonApiLinks,\n JsonApiRelationship,\n JsonApiRelationships,\n JsonApiResource,\n LegacyStoreOptions,\n PresenterOptions,\n SchemaRegistry,\n StoreOptions,\n ValidationError,\n} from './yayson/types.js'\nimport type { ZodLikeSchema } from './yayson/schema.js'\n\ntype AdapterOption = string | typeof Adapter\n\ninterface YaysonOptions {\n adapter?: AdapterOption\n}\n\ninterface YaysonResult {\n Store: typeof Store\n Presenter: Presenter\n Adapter: typeof Adapter\n}\n\nfunction lookupAdapter(nameOrAdapter?: AdapterOption): typeof Adapter {\n if (nameOrAdapter === 'default' || !nameOrAdapter) {\n return Adapter\n } else if (typeof nameOrAdapter === 'string') {\n if (nameOrAdapter === 'sequelize') {\n return adapters.sequelize\n } else {\n throw new Error('Adapter not found: ' + nameOrAdapter)\n }\n }\n return nameOrAdapter\n}\n\nfunction yayson(options?: YaysonOptions): YaysonResult {\n const adapter = lookupAdapter(options?.adapter)\n const Presenter = createPresenter(adapter)\n\n return {\n Store,\n Presenter,\n Adapter,\n }\n}\n\nexport default yayson\nexport type {\n Adapter,\n JsonApiDocument,\n JsonApiLink,\n JsonApiLinks,\n JsonApiRelationship,\n JsonApiRelationships,\n JsonApiResource,\n LegacyStoreOptions,\n PresenterOptions,\n SchemaRegistry,\n StoreOptions,\n ValidationError,\n YaysonOptions,\n YaysonResult,\n ZodLikeSchema,\n}\n"],"mappings":";;;AAQA,SAAS,iBAAiB,OAAyC;AACjE,QAAO,SAAS,QAAQ,OAAO,UAAU,YAAY,SAAS,SAAS,OAAO,MAAM,QAAQ;;AAG9F,IAAM,mBAAN,cAA+BA,gBAAQ;CAGrC,OAAgB,IAAI,OAAkB,KAAuB;AAC3D,MAAI,iBAAiB,MAAM,CACzB,QAAO,MAAM,IAAI,IAAI;;CAK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