Class ResourceAbstract

This class represents a JSON:API resource object and is used as base class for PrimaryResources and RelatedResources.

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

Hierarchy

Constructors

  • Parameters

    • document: Document

      The parent document that contains this resource

    • id: string

      The id of this resource

    • type: string

      The resource type

    • context: Context

      The context to use to fetch related documents

    Returns Resource

Properties

context: Context

The context to use to fetch related documents

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