Directly construct a Document. Does not fetch any data from a server.
The raw JSON:API data describing the document
The context to use to fetch related documents (not used during the initial construction)
Optional url: URLAn optional URL can be provided to indicate where the raw data came from
Optional sparseFields: SparseFieldsAn object listing sparse fieldsets for subsequent fetch operations
SchemaError when the given rawData does not look like a JSON:API document
Private Readonly contextThe context to use to fetch related documents (not used during the initial construction)
Readonly rawThe raw JSON:API data describing the document
Optional Readonly sparseAn object listing sparse fieldsets for subsequent fetch operations
Optional Readonly urlAn optional URL can be provided to indicate where the raw data came from
true if this document's primary data is an array of resources and not just a single resource
Map from type and id to RelatedResource for all resources under the top level included member.
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(...)
}
}
The primary Resource in this document.
Null if the primary data consists of the value null
The JSON:API document
{
"data": {
"type": "articles",
"id": "1"
}
}
would have a primary resource with type "articles" and id "1".
CardinalityError if the document instead contains an array of resources.
Array of the primary Resources in this document.
Empty array when no primary resources are contained in the document
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".
CardinalityError if the document instead only contains a singular resource.
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.
The id of the resource objects to look for
The type of the resource objects to look for
Static fromURLFetch JSON:API data from the given URL and construct a Document from it.
Context that will provide the JSON:API data, normally by fetching it from a server
Optional sparseFields: SparseFieldsOnly these fields (per type) are requested from the server
Fetching a document from a server:
const articleDoc = await JsonApi.Document.fromURL(new URL('http://example.com/article/1'), context);
const article = articleDoc.resource;
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
Holds an
application/vnd.api+jsondocument 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
memoizedare 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