-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
947 lines (897 loc) · 35 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
import { Spec } from './jsonapiSpec';
import { SchemaError } from './schemaChecker';
import { memoized } from './memoized.decorator';
export { Spec };
/**
* This is the main module of Grivet.
*
* It contains the [[Document]], [[Resource]] and [[Relationship]] classes that
* perform most of the work interpreting a JSON:API structure.
*
* Normally you would start by creating a [[Document]] from existing JSON:API data or using the [[Document.fromURL]]
* method to fetch data from a server. Then you can examine the resources in that document ([[Document.resource]]
* or [[Document.resources]]) and traverse to other related resources ([[Resource.relatedResource]] or
* [[Resource.relatedResources]]).
*
* You have to provide an implementation of the [[Context]] interface to specify how documents should be fetched from
* URLs. The documentation of the [[Context]] interface shows several possible implementations (e.g. using the Angular http client
* or Axios).
*
* Based on version 1.0 of JSON:API.
*/
export namespace JsonApi {
/** Thrown when there is mismatch between the expected resource count (one or many) and the actual resource count */
export class CardinalityError extends Error {
constructor(message?: string) {
super(message);
//tslint:disable:no-unsafe-any
Object.setPrototypeOf(this, new.target.prototype);
}
}
/** Thrown when an explicitly provided id does not match the id received from the server */
export class IdMismatchError extends Error {
constructor(message?: string) {
super(message);
//tslint:disable:no-unsafe-any
Object.setPrototypeOf(this, new.target.prototype);
}
}
/**
* Implement this interface to define how a [[JsonApiDocument]] (the JSON:API raw data) is fetched from a given URL.
*
* [[include:guides/context.md]]
*/
export interface Context {
/**
* Should return a `Promise` resolving to the JSON:API document for the given `url`
*/
getDocument(url: URL): Promise<Spec.JsonApiDocument>;
}
/** List of fields that should be fetched from the server */
export type FieldNames = string[];
/** Mapping from resource type to sparse fields */
export type SparseFields = { [resourceType: string]: FieldNames };
/**
* Holds an `application/vnd.api+json` [document](https://jsonapi.org/format/1.0/#document-top-level) and
* provides methods to access the resources in that document.
*
* This is the main class that acts as an entry point to traverse to other resources.
* Use the static [[fromURL]] method to fetch and construct a [[Document]] from a given URL.
*
* Methods and accessors marked as `memoized` are only executed once per instance (the first time they are called)
* and return a cached result on subsequent calls.
*
* @see https://jsonapi.org/format/1.0/#document-top-level
*/
export class Document {
/**
* Fetch JSON:API data from the given URL and construct a [[Document]] from it.
*
* ### Simplest example
*
* Fetching a document from a server:
* ```typescript
* const articleDoc = await JsonApi.Document.fromURL(new URL('http://example.com/article/1'), context);
* const article = articleDoc.resource;
* ```
* [[include:guides/sparseFieldsets.md]]
*
* @param context Context that will provide the JSON:API data, normally by fetching it from a server
* @param sparseFields Only these fields (per type) are requested from the server
*/
static async fromURL(url: URL, context: Context, sparseFields?: SparseFields): Promise<Document> {
for (const resourceType in sparseFields) {
url.searchParams.append(`fields[${resourceType}]`, sparseFields[resourceType].join(','));
}
const rawData = await context.getDocument(url);
return new Document(rawData, context, url, sparseFields);
}
/**
* Directly construct a [[Document]]. Does not fetch any data from a server.
* @param rawData The raw JSON:API data describing the document
* @param context The context to use to fetch related documents (not used during the initial construction)
* @param url An optional URL can be provided to indicate where the raw data came from
* @param sparseFields An object listing [sparse fieldsets](https://jsonapi.org/format/1.0/#fetching-sparse-fieldsets) for subsequent fetch operations
* @throws [[SchemaError]] when the given rawData does not look like a JSON:API document
*/
constructor(
readonly rawData: Spec.JsonApiDocument,
private readonly context: Context,
public readonly url?: URL,
public readonly sparseFields?: SparseFields
) {
Spec.checkDocumentSchema(rawData);
}
/**
* `true` if this document's primary data is an array of resources and not just a single resource
* @memoized
*/
@memoized()
get hasManyResources(): boolean {
return Array.isArray(this.rawData.data);
}
/**
* Array of the primary [[Resource]]s in this document. For example the JSON:API document
*
* ```json
* {
* "data": [{
* "type": "articles",
* "id": "1"
* }]
* }
* ```
*
* would have a primary resource array of length 1 with one element with type "articles" and id "1".
*
* @returns Empty array when no primary resources are contained in the document
* @throws [[CardinalityError]] if the document instead only contains a singular resource.
* @memoized
*/
@memoized()
get resources(): PrimaryResource[] {
if (!this.hasManyResources) {
throw new CardinalityError(
'Document does not contain an array of resources. Use the `resource` property instead'
);
}
if (!('data' in this.rawData)) {
return [];
}
return (<Spec.ResourceObject[]>this.rawData.data).map(
primaryData => new PrimaryResource(primaryData, this, primaryData.type, this.context)
);
}
/**
* The primary [[Resource]] in this document. For example the JSON:API document
*
* ```json
* {
* "data": {
* "type": "articles",
* "id": "1"
* }
* }
* ```
*
* would have a primary resource with type "articles" and id "1".
*
* @returns Null if the primary data consists of the value `null`
* @throws [[CardinalityError]] if the document instead contains an array of resources.
* @memoized
*/
@memoized()
get resource(): PrimaryResource | null | undefined {
if (this.hasManyResources) {
throw new CardinalityError('Document contains an array of resources. Use the `resources` property instead');
}
if (this.rawData.data === null) {
return null;
}
if (!('data' in this.rawData)) {
return undefined;
}
const primaryData = <Spec.ResourceObject>this.rawData.data;
return new PrimaryResource(primaryData, this, primaryData.type, this.context);
}
/**
* Map from type and id to [[RelatedResource]] for all resources under the top level `included` member.
* For example for the JSON:API document
*
* ```json
* {
* "data": null,
* "included": [
* {"type": "articles", "id": "1"},
* {"type": "articles", "id": "2"},
* {"type": "people", "id": "5"}
* ]
* }
* ```
* calling `includedResources` would produce
* ```typescript
* {
* articles: {
* '1': RelatedResource(...),
* '2': RelatedResource(...),
* },
* people: {
* '5': RelatedResource(...)
* }
* }
* ```
* @memoized
*/
@memoized()
get includedResources(): IncludedResourcesMap {
const res: IncludedResourcesMap = {};
for (const includedResource of this.rawData.included || []) {
if (!(includedResource.type in res)) {
res[includedResource.type] = {};
}
res[includedResource.type][includedResource.id] = new RelatedResource(
this,
includedResource.id,
includedResource.type,
this.context
);
}
return res;
}
}
/** Collection of [[RelatedResource]]s included in a compound document, organized by type and id */
export type IncludedResourcesMap = { [type: string]: { [id: string]: RelatedResource } };
/** Mapping from relationship name to [[Relationship]] */
export type Relationships = { [relationshipName: string]: Relationship };
/** Mapping from link name to [[Link]] */
export type Links = { [linkName: string]: Link };
/** @hidden */
type RelationshipToResource = { [relationshipName: string]: Promise<Resource> };
/** @hidden */
type RelationshipToResources = { [relationshipName: string]: Promise<Resource[]> };
/** @hidden */
type RelationshipToDocument = { [relationshipName: string]: Promise<Document> };
/** @hidden */
class RelatedResourceAccessor<T extends RelationshipToResource> implements ProxyHandler<T> {
constructor(private readonly parent: Resource) {}
/**
* Provide access to parent related resource
* @hidden
*/
async get(target: T, relationshipName: string, receiver: any): Promise<Resource | null | undefined> {
if (relationshipName in this.parent.relationships) {
return this.parent.relationships[relationshipName].resource();
}
}
}
/** @hidden */
class RelatedResourcesAccessor<T extends RelationshipToResources> implements ProxyHandler<T> {
constructor(private readonly parent: Resource) {}
/**
* Provide access to parent related resources
* @hidden
*/
async get(target: T, relationshipName: string, receiver: any): Promise<Resource[]> {
if (relationshipName in this.parent.relationships) {
return this.parent.relationships[relationshipName].resources();
}
return [];
}
}
/** @hidden */
class RelatedDocumentAccessor<T extends RelationshipToDocument> implements ProxyHandler<T> {
constructor(private readonly parent: Resource) {}
/**
* Provide access to parent related document
* @hidden
*/
async get(target: T, relationshipName: string, receiver: any): Promise<Document | undefined> {
if (relationshipName in this.parent.relationships) {
return this.parent.relationships[relationshipName].relatedDocument();
}
}
}
/**
* This class represents a [JSON:API resource object](https://jsonapi.org/format/1.0/#document-resource-objects)
* and is used as base class for [[PrimaryResource]]s and [[RelatedResource]]s.
*
* The abstract [[getData]] method is implemented in the derived classes to specify how
* the raw JSON:API data is obtained.
*
* Methods and accessors marked as `memoized` are only executed once per instance (the first time they are called)
* and return a cached result on subsequent calls.
*
* @see https://jsonapi.org/format/1.0/#document-resource-objects
*/
export abstract class Resource {
/**
* @param document The parent document that contains this resource
* @param id The id of this resource
* @param type The resource type
* @param context The context to use to fetch related documents
*/
constructor(
protected readonly document: Document,
public readonly id: string,
public readonly type: string,
private readonly context: Context
) {}
protected abstract getData(): Spec.ResourceObject;
/**
* The raw JSON:API data of this resource
* @memoized
*/
@memoized()
get rawData(): Spec.ResourceObject {
return this.getData();
}
/**
* Object containing all [attributes](https://jsonapi.org/format/1.0/#document-resource-object-attributes) of this resource
*/
get attributes(): Spec.AttributesObject | undefined {
return this.rawData.attributes;
}
/**
* Object containing all [[Relationship]]s defined by this resource
* @see https://jsonapi.org/format/1.0/#document-resource-object-relationships
* @memoized
*/
@memoized()
get relationships(): Relationships {
if (!this.rawData.relationships) {
return {};
}
const result: Relationships = {};
for (const relationshipName in this.rawData.relationships) {
result[relationshipName] = new Relationship(
this.document,
this.rawData.relationships[relationshipName],
this.context
);
}
return result;
}
/**
* Object containing all [[Link]]s defined by this resource
* @see https://jsonapi.org/format/1.0/#document-links
* @memoized
*/
@memoized()
get links(): Links {
if (!this.rawData.links) {
return {};
}
const result: Links = {};
for (const linkName in this.rawData.links) {
result[linkName] = new Link(this.rawData.links[linkName], this.document.url);
}
return result;
}
/**
* Object containing all entries inside `links` of `meta` interpreted as
* JSON:API Link (either string or link object)
* @memoized
*/
@memoized()
get metaLinks(): Links {
if (!this.rawData.meta) {
return {};
}
if (!this.rawData.meta.links) {
return {};
}
const result: Links = {};
for (const linkName in this.rawData.meta.links) {
result[linkName] = new Link(this.rawData.meta.links[linkName], this.document.url);
}
return result;
}
/**
* Object containing all [meta data](https://jsonapi.org/format/1.0/#document-meta) of this resource
* @memoized
*/
@memoized()
get meta(): Spec.MetaObject | undefined {
return this.rawData.meta;
}
/**
* Self-link defined in the `links` member
*/
get selfLink(): Link | undefined {
return this.links['self'];
}
/**
* Proxy providing all multiple [[Resource]]s reachable via relationships from this resource.
* For example, for this JSON:API document
*
* ```json
* {
* "data": {
* "type": "articles",
* "id": "1",
* "relationships": {
* "comments": {
* "data": [{"type":"comments", "id":"1"}, {"type":"comments", "id":"2"}]
* },
* "ratings": {
* "data": [{"type":"ratings", "id":"1"}, {"type":"ratings", "id":"2"}]
* }
* }
* },
* "included": [...]
* }
* ```
*
* calling `relatedResources` on the primary _articles_ resource will give you access to the following data:
*
* ```typescript
* {
* comments: [
* RelatedResource(...),
* RelatedResource(...)
* ],
* ratings: [
* RelatedResource(...),
* RelatedResource(...)
* ]
* }
* ```
*
* This accessor is a shortcut so that you do not have to go through `relationships` manually every time.
* Writing
*
* ```typescript
* const comments = await article.relatedResources['comments'];
* ```
*
* is equivalent to
*
* ```typescript
* const comments = await article.relationships['comments'].resources();
* ```
*
* As this shortcut uses the [[Relationship.resources]] method, it prefers resource linkage (i.e. id/type pairs in `data`)
* to `related` links should both be present in the relationship. You can use the [[Relationship.resourcesFromRelatedLink]]
* method if you need to fetch related resources via link instead of resource linkage.
*
* As this is only a proxy object, calling `relatedResources` will not yet construct any [[RelatedResource]]
* instances. Only when accessing a specific related resource (e.g. calling `article.relatedResources['comments']`)
* are the new resources actually constructed.
*/
get relatedResources() {
return new Proxy(<RelationshipToResources>{}, new RelatedResourcesAccessor(this));
}
/**
* Proxy providing all singular [[Resource]]s reachable via relationships from this resource.
* For example, for this JSON:API document
*
* ```json
* {
* "data": {
* "type": "articles",
* "id": "1",
* "relationships": {
* "author": {
* "data": {"type":"people", "id":"1"}
* },
* "reviewer": {
* "data": {"type":"people", "id":"12"}
* }
* }
* },
* "included": [...]
* }
* ```
*
* calling `relatedResource` on the primary _articles_ resource will give you access to the following data:
*
* ```typescript
* {
* author: RelatedResource(...),
* reviewer: RelatedResource(...)
* }
* ```
*
* This accessor is a shortcut so that you do not have to go through `relationships` manually every time.
* Writing
*
* ```typescript
* const author = await article.relatedResource['author'];
* ```
*
* is equivalent to
*
* ```typescript
* const author = await article.relationships['author'].resource();
* ```
*
* As this shortcut uses the [[Relationship.resource]] method, it prefers resource linkage (i.e. id/type pairs in `data`)
* to `related` links should both be present in the relationship. You can use the [[Relationship.resourceFromRelatedLink]]
* method if you need to fetch a related resource via link instead of resource linkage.
*
* As this is only a proxy object, calling `relatedResource` will not yet construct any [[RelatedResource]]
* instances. Only when accessing a specific related resource (e.g. calling `article.relatedResource['author']`)
* is the new resource actually constructed.
*/
get relatedResource() {
return new Proxy(<RelationshipToResource>{}, new RelatedResourceAccessor(this));
}
/**
* Proxy providing all [[Document]]s reachable via `related` links in relationships.
* For example, for this JSON:API document
*
* ```json
* {
* "data": {
* "type": "articles",
* "id": "1",
* "relationships": {
* "author": {
* "links": {"related": "http://example.com/people/1"}
* },
* "reviewer": {
* "links": {"related": "http://example.com/people/12"}
* }
* }
* }
* }
* ```
*
* calling `relatedDocuments` on the primary _articles_ resource will give you access to the following data:
*
* ```typescript
* {
* author: Document(...),
* reviewer: Document(...)
* }
* ```
*
* As this is only a proxy object, calling `relatedDocuments` does not yet fetch any related documents.
* Only when accessing a specific related document (e.g. calling `article.relatedDocuments['author']`) is the
* new document actually requested from the server.
*/
get relatedDocuments() {
return new Proxy(<RelationshipToDocument>{}, new RelatedDocumentAccessor(this));
}
}
/**
* A resource contained in the top level `data` member of a [[Document]].
*
* Always constructed non-lazily from raw JSON:API data. Normally this is
* done automatically when accessing the [[Document.resource]] or [[Document.resources]]
* accessors, for example:
*
* ```typescript
* // a document containing one _article_ resource
* const doc = await JsonApi.Document.fromURL(new URL('http://example.com/article'), context);
* const article = doc.resource; // the primary resource
* ```
*
* Methods and accessors marked as `memoized` are only executed once per instance (the first time they are called)
* and return a cached result on subsequent calls.
*/
export class PrimaryResource extends Resource {
private readonly pRawData: Spec.ResourceObject;
/**
* Directly construct a primary resource. Normally a primary resource is obtained via the [[Document.resource]]
* or [[Document.resources]] accessors while traversing a document structure.
* @param rawData The JSON:API resource object from which to construct this primary resource
* @param document The parent document that contains this primary resource
* @param resourceType The type of this resource
* @param context The context to use to fetch related documents
* @param id The id of this resource
* @throws [[IdMismatchError]] when the optional `id` argument does not match the id present in `rawData`
* @throws [[SchemaError]] when `rawData` does not look like a [JSON:API resource object](https://jsonapi.org/format/1.0/#document-resource-objects)
*/
constructor(rawData: Spec.ResourceObject, document: Document, resourceType: string, context: Context, id?: string) {
Spec.checkResourceObjectSchema(rawData);
const passedId = id;
id = rawData.id;
if (passedId !== undefined && id !== passedId) {
throw new IdMismatchError(`ID in rawData does not match given ID: ${id} != ${passedId}`);
}
super(document, id, resourceType, context);
this.pRawData = rawData;
}
protected getData(): Spec.ResourceObject {
return this.pRawData;
}
}
/**
* A resource contained in the top level `included` member of a [[Document]] or linked via href.
*
* This resource is initialized lazily on demand when its [[getData]] method is first called.
* The resource then looks for its id and type (given at construction) in the parent document
* `data` and `included` members to find its raw data.
*
* Normally a related resource is obtained via the [[Resource.relatedResource]] and [[Resource.relatedResources]]
* accessors or the [[Relationship.resource]] and [[Relationship.resources]] methods, for example:
*
* ```typescript
* // a document containing one _article_ resource with a related _author_ resource
* const doc = await JsonApi.Document.fromURL(new URL('http://example.com/article'), context);
* const article = doc.resource; // the primary resource
* const author = await article.relatedResource['author']; // the related resource
* ```
*
* Methods and accessors marked as `memoized` are only executed once per instance (the first time they are called)
* and return a cached result on subsequent calls.
*/
export class RelatedResource extends Resource {
/**
* Directly construct a related document. It will be lazily initialized from `document` on first use,
* so we need to provide an id and a type here so that the resource can find itself in `document`.
* @param document The parent document that contains this related resource
* @param id The id of this resource
* @param resourceType The type of this resource
* @param context The context to use to fetch related documents
* @throws [[IdMismatchError]] when `id` was not found in the given `document`
*/
constructor(document: Document, id: string, resourceType: string, context: Context) {
super(document, id, resourceType, context);
}
/**
* Looks up the matching resource in the parent [[Document]]. All primary resources (those in the `data` member)
* and all included resources (those in the `included` member) are searched.
* @throws [[IdMismatchError]] if the resource is not found or found multiple times
* @throws [[SchemaError]] when `rawData` does not look like a [JSON:API resource object](https://jsonapi.org/format/1.0/#document-resource-objects)
*/
protected getData(): Spec.ResourceObject {
const primaryDataArray = this.document.hasManyResources
? <Spec.ResourceObject[]>this.document.rawData.data
: [<Spec.ResourceObject>this.document.rawData.data];
const candidates = primaryDataArray.concat(this.document.rawData.included || []);
const filtered = candidates.filter(
resourceObject => resourceObject.type === this.type && resourceObject.id === this.id
);
if (filtered.length === 0) {
throw new IdMismatchError(`Resource with id "${this.id}" and type "${this.type}" not found in document`);
}
if (filtered.length > 1) {
throw new IdMismatchError(
`Resource with id "${this.id}" and type "${this.type}" found more than once in document`
);
}
Spec.checkResourceObjectSchema(filtered[0]);
return filtered[0];
}
}
/**
* Represents a link with URL and optional meta data
* @see https://jsonapi.org/format/1.0/#document-links
*/
export class Link {
/** The complete url for this link */
readonly url: URL;
/** Any additional meta data */
readonly meta?: object;
/**
* @param rawData Can be a string containing an href or a [[LinkObject]]
* @param referringDocumentURL The _origin_ part of this URL will be used as prefix if `rawData` only refers to a
* pathname and not a full URL
*/
constructor(rawData: Spec.Link, referringDocumentURL?: URL) {
const origin = referringDocumentURL ? referringDocumentURL.origin : '';
if (typeof rawData === 'string') {
try {
this.url = new URL(rawData);
} catch (e) {
this.url = new URL(origin + rawData);
}
} else {
try {
this.url = new URL(rawData.href);
} catch (e) {
this.url = new URL(origin + rawData.href);
}
this.meta = rawData.meta;
}
}
}
/**
* Defines relations from one [[Resource]] to another (included in the document or external) and can resolve them.
*
* This class is normally not used directly. It is used internally in the [[relatedResource]], [[relatedResources]]
* and [[relatedDocuments]] accessors and you should use those if you just want to gain access to other resources.
*
* If you need to work with relationships directly (e.g. to obtain meta data about the relationship itself), you can
* use the [[Resource.relationships]] accessor.
*
* Methods and accessors marked as `memoized` are only executed once per instance (the first time they are called)
* and return a cached result on subsequent calls.
*
* @see https://jsonapi.org/format/1.0/#document-resource-object-relationships
*/
export class Relationship {
/**
* Directly construct a relationship
* @param referringDocument The document that contains the relationship
* @param rawData The JSON:API relationship object from which to construct the relationship
* @param context The context to use to fetch related documents
* @throws [[SchemaError]] when `rawData` does not look like a [JSON:API relationship object](https://jsonapi.org/format/1.0/#document-resource-object-relationships)
*/
constructor(
private readonly referringDocument: Document,
private readonly rawData: Spec.RelationshipObject,
private readonly context: Context
) {
Spec.checkRelationshipObjectSchema(rawData);
}
/**
* `true` if the relationship only contains a `meta` member and no `data` or `links`
* @memoized
*/
@memoized()
get empty(): boolean {
return this.links === undefined && this.data === undefined;
}
/**
* Map of link names to [[Link]]s defined under the `links` member of this relationship
* @memoized
*/
@memoized()
get links(): Links | undefined {
if (!this.rawData.links) {
return undefined;
}
const result: Links = {};
for (const linkName in this.rawData.links) {
result[linkName] = new Link(this.rawData.links[linkName], this.referringDocument.url);
}
return result;
}
/**
* One or many [[ResourceIdentifierObject]]s defined in the `data` member of this relationship
* @memoized
*/
@memoized()
get data(): Spec.ResourceIdentifierObject | Spec.ResourceIdentifierObject[] | undefined | null {
return this.rawData.data;
}
/**
* The [[Document]] referred to by the `related` link in the `links` member of the relationship.
* Fetches the related document from the server using the context given at construction and respecting any
* sparseFields of the referring document.
* @memoized
*/
@memoized()
async relatedDocument(): Promise<Document | undefined> {
if (this.links && 'related' in this.links) {
return Document.fromURL(this.links['related'].url, this.context, this.referringDocument.sparseFields);
}
}
/**
* List of [[Resource]]s referenced by this relationship (if there are many resources).
* Resource linkage (resource identifiers found in the `data` member of this relationship) has priority.
* If there is no resource linkage, the primary resources found in [[relatedDocument]] are used.
* @throws [[CardinalityError]] if there is only a singular resource.
* @throws [[SchemaError]] if neither a `links` nor a `data` member is present
* @memoized
*/
@memoized()
async resources(): Promise<Resource[]> {
if ('data' in this.rawData) {
const resourceIdentifiers = this.data;
if (resourceIdentifiers !== null && resourceIdentifiers !== undefined) {
if (!Array.isArray(resourceIdentifiers)) {
throw new CardinalityError(
'Relationship does not contain an array of resources. Use the `resource` method instead.'
);
}
return Promise.resolve(
resourceIdentifiers.map(rid => new RelatedResource(this.referringDocument, rid.id, rid.type, this.context))
);
}
}
const relatedDoc = await this.relatedDocument();
if (relatedDoc) {
return relatedDoc.resources;
}
throw new SchemaError(
'A relationship object relating to a resource must contain a `links.related` or `data` member'
);
}
/**
* The one [[Resource]] referenced by this relationship.
* Resource linkage (the resource identifier found in the `data` member of this relationship) has priority.
* If there is no resource linkage, the primary resource found in [[relatedDocument]] is used.
* @throws [[CardinalityError]] if there are many resources.
* @throws [[SchemaError]] if neither a `links` nor a `data` member is present
* @memoized
*/
@memoized()
async resource(): Promise<Resource | null | undefined> {
if ('data' in this.rawData) {
const resourceIdentifier = this.data;
if (resourceIdentifier !== null && resourceIdentifier !== undefined) {
if (Array.isArray(resourceIdentifier)) {
throw new CardinalityError(
'Relationship contains more than one resource. Use the `resources` method instead.'
);
}
return Promise.resolve(
new RelatedResource(this.referringDocument, resourceIdentifier.id, resourceIdentifier.type, this.context)
);
}
}
const relatedDoc = await this.relatedDocument();
if (relatedDoc) {
return relatedDoc.resource;
}
if ('data' in this.rawData) {
const resourceIdentifier = this.data;
if (resourceIdentifier === null) {
return null;
}
if (resourceIdentifier === undefined) {
return undefined;
}
}
throw new SchemaError(
'A relationship object relating to a resource must contain a `links.related` or `data` member'
);
}
/**
* Works like the [[resources]] method, but prefers the `related` link instead of resource linkage.
* If no `related` link is present or if the link does not work, it falls back to the [[resources]] method.
* @throws [[CardinalityError]] if there is only a singular resource.
* @throws [[SchemaError]] if neither a `links` nor a `data` member is present
* @memoized
*/
@memoized()
async resourcesFromRelatedLink(): Promise<Resource[]> {
try {
const relatedDoc = await this.relatedDocument();
if (relatedDoc) {
return relatedDoc.resources;
}
} catch (err) {}
return this.resources();
}
/**
* Works like the [[resource]] method, but prefers the `related` link instead of resource linkage.
* If no `related` link is present or if the link does not work, it falls back to the [[resource]] method.
* @throws [[CardinalityError]] if there are many resources.
* @throws [[SchemaError]] if neither a `links` nor a `data` member is present
* @memoized
*/
@memoized()
async resourceFromRelatedLink(): Promise<Resource | null | undefined> {
try {
const relatedDoc = await this.relatedDocument();
if (relatedDoc) {
return relatedDoc.resource;
}
} catch (err) {}
return this.resource();
}
}
/**
* Some helpers for constructing a document to POST to a server
* @hidden
*/
export class ClientDocument {
private readonly rawData: Spec.ClientJsonApiDocument;
constructor(resourceType: string, id?: string) {
this.rawData = { data: { type: resourceType } };
if (id !== undefined) {
this.rawData.data.id = id;
}
}
/** Sets a primary resource attribute @hidden */
setAttribute(name: string, value: string) {
if (!this.rawData.data.attributes) {
this.rawData.data.attributes = {};
}
this.rawData.data.attributes[name] = value;
}
/** Adds a named relationship to a resource @hidden */
setRelationship(
name: string,
ressourceIdentifier: Spec.ResourceIdentifierObject | Spec.ResourceIdentifierObject[]
) {
if (!this.rawData.data.relationships) {
this.rawData.data.relationships = {};
}
this.rawData.data.relationships[name] = { data: ressourceIdentifier };
}
/** Adds the resource to `included` @hidden */
includeResource(resource: Spec.ResourceObject) {
if (!this.rawData.included) {
this.rawData.included = [];
}
this.rawData.included.push(resource);
}
/** Adds the resources to `included` @hidden */
includeResources(resources: Spec.ResourceObject[]) {
if (!this.rawData.included) {
this.rawData.included = [];
}
this.rawData.included.push(...resources);
}
/** The raw JSON:API data @hidden */
get data(): Spec.ClientJsonApiDocument {
return this.rawData;
}
}
}