Skip to content

TypeScript API / laikacms/json-api / toJsonApi

Function: toJsonApi() ​

ts
function toJsonApi<T, O, I>(
   data, 
   type, 
   idField
): object;

Transforms a domain object to JSON:API resource format.

Per JSON:API §7.2.2, a resource object MUST NOT have an attribute named type or id. This function strips both type and id (in addition to the caller-supplied idField) from the attributes bag.

Type Parameters ​

Type Parameter
T extends string
O extends Record<string, unknown>
I extends string | number | symbol

Parameters ​

ParameterTypeDescription
dataOThe domain object
typeTJSON:API resource type
idFieldIName of the ID field in the domain object

Returns ​

object

attributes ​

ts
attributes: Omit<O, I | "type" | "id">;

id ​

ts
id: string;

type ​

ts
type: T;

Released under the MIT License.