Holds an application/vnd.api+json document 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.

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.

See

https://jsonapi.org/format/1.0/#document-top-level

Hierarchy

  • Document

Constructors

  • Directly construct a Document. Does not fetch any data from a server.

    Parameters

    • rawData: JsonApiDocument

      The raw JSON:API data describing the document

    • context: Context

      The context to use to fetch related documents (not used during the initial construction)

    • Optional url: URL

      An optional URL can be provided to indicate where the raw data came from

    • Optional sparseFields: SparseFields

      An object listing sparse fieldsets for subsequent fetch operations

    Returns Document

    Throws

    SchemaError when the given rawData does not look like a JSON:API document

Properties

context: Context

The context to use to fetch related documents (not used during the initial construction)

The raw JSON:API data describing the document

sparseFields?: SparseFields

An object listing sparse fieldsets for subsequent fetch operations

url?: URL

An optional URL can be provided to indicate where the raw data came from

Accessors

  • get hasManyResources(): boolean
  • true if this document's primary data is an array of resources and not just a single resource

    Returns boolean

    Memoized

  • get includedResources(): IncludedResourcesMap
  • Map from type and id to RelatedResource for all resources under the top level included member.

    Returns IncludedResourcesMap

    Example

    For the JSON:API document

    {
    "data": null,
    "included": [
    {"type": "articles", "id": "1"},
    {"type": "articles", "id": "2"},
    {"type": "people", "id": "5"}
    ]
    }

    calling includedResources would produce

    {
    articles: {
    '1': RelatedResource(...),
    '2': RelatedResource(...),
    },
    people: {
    '5': RelatedResource(...)
    }
    }

    Memoized

  • get resource(): undefined | null | PrimaryResource
  • The primary Resource in this document.

    Returns undefined | null | PrimaryResource

    Null if the primary data consists of the value null

    Example

    The JSON:API document

    {
    "data": {
    "type": "articles",
    "id": "1"
    }
    }

    would have a primary resource with type "articles" and id "1".

    Throws

    CardinalityError if the document instead contains an array of resources.

    Memoized

  • get resources(): PrimaryResource[]
  • Array of the primary Resources in this document.

    Returns PrimaryResource[]

    Empty array when no primary resources are contained in the document

    Example

    The JSON:API document

    {
    "data": [{
    "type": "articles",
    "id": "1"
    }]
    }

    would have a primary resource array of length 1 with one element with type "articles" and id "1".

    Throws

    CardinalityError if the document instead only contains a singular resource.

    Memoized

Methods

  • All resource objects in this document with the given id and type, i.e. matching primary resources (those in the data member) and matching included resources (those in the included member). A compliant document contains at most one; an empty array is returned when there is none.

    The lookup is served from an index that is built on first use, so resolving many related resources of a large compound document does not scan the whole document for each of them. Like the memoized accessors, the index does not reflect changes made to rawData after it was built.

    Parameters

    • id: string

      The id of the resource objects to look for

    • resourceType: string

      The type of the resource objects to look for

    Returns readonly ResourceObject[]

  • Fetch JSON:API data from the given URL and construct a Document from it.

    Parameters

    • url: URL
    • context: Context

      Context that will provide the JSON:API data, normally by fetching it from a server

    • Optional sparseFields: SparseFields

      Only these fields (per type) are requested from the server

    Returns Promise<Document>

    Example

    Simplest example

    Fetching a document from a server:

    const articleDoc = await JsonApi.Document.fromURL(new URL('http://example.com/article/1'), context);
    const article = articleDoc.resource;

    Specifying sparse fieldsets

    Sparse fieldsets enable a client to request only certain fields (attributes or relationships) to reduce data transfer from server to client. When constructing a document, you can optionally provide a mapping from resource type to a list of fields that you are interested in for this type:

    const sparseFields = {
    articles: ['author'],
    people: ['first-name', 'last-name']
    };

    const articleDoc = await JsonApi.Document.fromURL(new URL('http://example.com/articles/1'), context, sparseFields);
    const article = articleDoc.resource;
    const author = await article.relatedResource['author'];

    If the server also implements sparse fieldsets, the article resource will contain only the author relationship (and no attributes) and the author resource will contain only the first-name and last-name attributes (and no relationships).

Generated using TypeDoc