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.

    Throws

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

    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

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

    Memoized

    Returns boolean

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

    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

    Returns IncludedResourcesMap

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

    Example

    The JSON:API document

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

    Returns undefined | null | PrimaryResource

  • get resources(): PrimaryResource[]
  • Array of the primary Resources in this 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".

    Returns

    Empty array when no primary resources are contained in the document

    Throws

    CardinalityError if the document instead only contains a singular resource.

    Memoized

    Returns PrimaryResource[]

Methods

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

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

    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>

Generated using TypeDoc