Skip to main content

JSON:API

Introduction

JSON:API implementation provides multiple dependencies implementations to support read/write interactions with JSON:API based data source.

Implementations

makeJsonApiAdapter

This implementation of the adapter will execute context through HTTP requests using the fetch API.

makeJsonApiAdapter uses makeRestAdapterWith.

Usage

import { deepParamsSerializer } from '@foscia/http';
import { makeJsonApiAdapter } from '@foscia/jsonapi';

// Using blueprint (preconfigured with sensible defaults).
const { adapter } = makeJsonApiAdapter({
/* ...configuration */
});

const response = await adapter.execute({
/* ...context */
});

Configuration

JsonApiAdapter extends its configuration object from:

Defined in

makeJsonApiDeserializer

This implementation of the deserializer extract model instances from JSON:API documents.

makeJsonApiDeserializer extends the makeDeserializerWith.

Deserialized JSON:API document example

Here is an example of a JSON:API document which makeJsonApiDeserializer can deserialize to model instances.

{
"data": [
{
"type": "posts",
"id": "1",
"attributes": {
"title": "Foo",
"body": "Foo Body",
"publishedAt": "2023-10-24T10:00:00.000Z"
},
"relationships": {
"comments": {
"data": [
{
"type": "comments",
"id": "1"
},
{
"type": "comments",
"id": "2"
}
]
}
}
},
{
"type": "posts",
"id": "2",
"attributes": {
"title": "Bar",
"body": "Bar Body",
"publishedAt": null
},
"relationships": {
"comments": {
"data": []
}
}
}
],
"included": [
{
"type": "comments",
"id": "1",
"attributes": {
"body": "Foo Comment"
}
},
{
"type": "comments",
"id": "2",
"attributes": {
"body": "Bar Comment"
}
}
]
}

Usage

import { makeJsonApiDeserializer } from '@foscia/jsonapi';

// Using blueprint (preconfigured with sensible defaults).
const { deserializer } = makeJsonApiDeserializer({
/* ...configuration */
});

const { instances } = await deserializer.deserialize(data, {
/* ...context */
});

Configuration

makeJsonApiDeserializer extends its configuration object from:

NameTypeDescription
pullIdentifier(record: Record, context: {}) => Awaitable<DeserializerRecordIdentifier>Extract identifier (type and ID) from record.
pullAttribute(record: Record, deserializerContext: DeserializerContext, extract: Extract) => Awaitable<unknown>Extract raw attribute value from record.
pullAttribute(record: Record, deserializerContext: DeserializerContext, extract: Extract) => Awaitable<Arrayable<Record> | null | undefined>Extract raw relation value from record.

Defined in

makeJsonApiSerializer

This implementation of the serializer creates a JSON:API documents from model instance and relations.

makeJsonApiSerializer extends the makeSerializerWith.

Serialized JSON:API document example

Here is an example of a JSON:API document which makeJsonApiSerializer can create from a model instance.

{
"data": {
"type": "posts",
"id": "1",
"attributes": {
"title": "Foo",
"body": "Foo Body",
"publishedAt": "2023-10-24T10:00:00.000Z"
},
"relationships": {
"comments": {
"data": [
{
"type": "comments",
"id": "1"
},
{
"type": "comments",
"id": "2"
}
]
}
}
}
}

Usage

import { makeJsonApiSerializer } from '@foscia/jsonapi';

// Using blueprint (preconfigured with sensible defaults).
const { serializer } = makeJsonApiSerializer({
/* ...configuration */
});

const data = await serializer.serializeInstance(instance, {
/* ...context */
});

Configuration

makeJsonApiSerializer extends its configuration object from:

Defined in