
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
ts-json-api
Advanced tools
A collection of TypeScript interfaces and classes for working with [JSONAPI.org](http://jsonapi.org/)-standard requests/responses.
A collection of TypeScript interfaces and classes for working with JSONAPI.org-standard requests/responses.
There library supports JSONAPI standard in all its variations. Therefore, there are a number of interfaces you may find useful.
This is the main interface. It works with any acceptable combination of
ResponseWithData
, ResponseWithErrors
, & ResponseWithMeta
. If you need to
target specifc response types, it is recommend you use those more specific
interfaces (covered below).
See also: ResponseWithData
, ResponseWithErrors
, and ResponseWithMeta
.
This represents a single ResourceObject in a JSONAPI response. See also:
ResourceObjects
and ResourceObjectOrObjects
.
Due to JSONAPI's nested structure, it is constructed of a decent number of individual pieces. We recommend taking a look at src/types/index.ts for reference. Should you need to target more specific pieces of a response, it should be pretty self-explanitory.
import { Relationship, ResourceObject, Response } as JsonApi from 'ts-json-api';
interface Article extends ResourceObject {
type: 'articles';
attributes: {
title: string;
};
relationships: {
author: Relationship<Person>;
comments: Relationship<Comment[]>;
};
}
type ArticleItemResponse = Response<Article>;
type ArticleCollectionResponse = Response<Article[]>;
interface Person extends ResourceObject {
type: 'people';
attributes: {
'firstName': string;
'lastName': string;
twitter: string;
};
}
interface Comment extends ResourceObject {
type: 'comments';
attributes: {
body: string;
};
}
ApiResourceObject
classts-json-api
provides an ResourceObject class that is helpful for acessing Resource Object data and updating it in an immutable way. All functions on the ApiResourceObject
class will return a new ResourceObject
, unaffecting the original.
import { ApiResourceObject } from 'ts-json-api';
const exampleInput = {
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
},
"comments": {
"links": {
"self": "http://example.com/articles/1/relationships/comments",
"related": "http://example.com/articles/1/comments"
},
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
},
"links": {
"self": "http://example.com/articles/1"
}
}
};
const article = new ApiResourceObject(exampleInput);
console.log(article.type());
// "articles"
console.log(article.id());
// "1"
console.log(article.attributes());
/// {
/// "title": "JSON API paints my bikeshed!"
/// }
console.log(article.attribute('title'));
// "JSON API paints my bikeshed"
const updatedArticle = article.update({ title: "New Title" });
console.log(updatedArticle.attribute('title'));
// "New Title"
console.log(article.attribute('title'))
// "JSON API paints my bikeshed"
// > NOTE: The original `article` is not affected
/**
* Relationships
*/
const relationships = article.relationships();
console.log(relationships.author.id());
// "9"
// Note that all relationship(s)-fetching methods return ResourceObject/ResourceObjects representing those relationship objects
console.log(
relationships.comments[0].id(),
relationships.comments[1].id()
);
// "5", "12"
console.log(article.relationship('author').id());
// "9"
// You "add" a relationship when the relationship represents a collection (ie. comments).
const updatedArticle = article.addRelationship('comments', 'comments', '432');
const updatedArticle = article.addRelationship('comments', CommentResourceObject);
// You "set" a relationships when the relationship represents a single item (ie. author)
const updatedArticle = article.setRelationship('editor', 'people', '123');
const updatedArticle = article.setRelationship('editor', PeopleResourceObject);
// You can also remove a relationship
const updatedArticle = article.removeRelationship('comments', '9');
/**
* Other helpful methods
*/
// Helpful for posting to an API and/or working with other libraries
article.toJSON()
// Maybe your API call doesn't want to include relationship info
article.withoutRelationships().toJSON()
This package exposes all of its useful utility functions. Documentation is
coming, but feel free to browse around src/fp
. All functions are curried for
all you functional programming geeks.
FAQs
A collection of TypeScript interfaces and classes for working with [JSONAPI.org](http://jsonapi.org/)-standard requests/responses.
The npm package ts-json-api receives a total of 4,288 weekly downloads. As such, ts-json-api popularity was classified as popular.
We found that ts-json-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.