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 relatedResource and relatedResources accessors or the resource and resources methods.

Example

// 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

Remarks

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.

Hierarchy

Constructors

  • 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.

    Throws

    IdMismatchError when id was not found in the given document

    Parameters

    • document: Document

      The parent document that contains this related resource

    • id: string

      The id of this resource

    • resourceType: string

      The type of this resource

    • context: Context

      The context to use to fetch related documents

    Returns RelatedResource

Properties

document: Document

The parent document that contains this resource

id: string

The id of this resource

type: string

The resource type

Accessors

  • Object containing all entries inside links of meta interpreted as JSON:API Link (either string or link object)

    Memoized

    Returns Links

  • get relatedDocuments(): RelationshipToDocument
  • Proxy providing all Documents reachable via related links in relationships.

    Example

    For this JSON:API document

    {
    "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:

    {
    author: Document(...),
    reviewer: Document(...)
    }

    Remarks

    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.

    Returns RelationshipToDocument

  • get relatedResource(): RelationshipToResource
  • Proxy providing all singular Resources reachable via relationships from this resource.

    Example

    For this JSON:API document

    {
    "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:

    {
    author: RelatedResource(...),
    reviewer: RelatedResource(...)
    }

    This accessor is a shortcut so that you do not have to go through relationships manually every time. Writing

    const author = await article.relatedResource['author'];
    

    is equivalent to

    const author = await article.relationships['author'].resource();
    

    Remarks

    As this shortcut uses the 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 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.

    Returns RelationshipToResource

  • get relatedResources(): RelationshipToResources
  • Proxy providing all multiple Resources reachable via relationships from this resource.

    Example

    For this JSON:API document

    {
    "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:

    {
    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

    const comments = await article.relatedResources['comments'];
    

    is equivalent to

    const comments = await article.relationships['comments'].resources();
    

    Remarks

    As this shortcut uses the 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 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.

    Returns RelationshipToResources

Methods

Generated using TypeDoc