Directly construct a Document. Does not fetch any data from a server.
SchemaError when the given rawData does not look like a JSON:API document
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
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.
The JSON:API document
{
"data": {
"type": "articles",
"id": "1"
}
}
would have a primary resource with type "articles" and id "1".
Null if the primary data consists of the value null
CardinalityError if the document instead contains an array of resources.
Array of the primary Resources in this 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".
Empty array when no primary resources are contained in the document
CardinalityError if the document instead only contains a singular resource.
Static
fromURLFetch JSON:API data from the given URL and construct a Document from it.
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).
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
Generated using TypeDoc
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