Kitsu
A simple, lightweight & framework agnostic JSON:API client for Kitsu.io and other APIs
Check out the Migration Guide for breaking changes and new features in 4.x
Features
- JSON-API 1.0 compliant
- Automatically links relationships to data
- Works in Node and on the web
- Uses the Promise API
- Configurable timeout handling
Install
yarn add kitsu
npm install kitsu
Node / Browser Support
Package | Package Size | Node | Chrome | Firefox | Safari | Edge | IE |
---|
Default | 17.9 kb | 6+ | 49+ | 57+ | 10.1+ | 15+ | |
Legacy | 19.9 kb | 6+ | 4+ | 3+ | 3.1+ | 12+ | 8+ |
Node | 14.5 kb | 6+ | | | | | |
Response Comparison
A GET response by a JSON:API server returns:
{
data: {
id: '1'
type: 'articles'
attributes: {
title: 'JSON API paints my bikeshed'
}
relationships: {
author: {
data: {
id: '42'
type: 'people'
}
}
}
}
included: [
{
id: '42'
type: 'people'
attributes: {
name: 'John'
}
}
]
}
A GET request with kitsu
returns:
{
data: {
id: '1'
type: 'articles'
title: 'JSON API paints my bikeshed'
author: {
id: '42'
type: 'people'
name: 'John'
}
}
}
Quick Start
import Kitsu from 'kitsu'
const Kitsu = require('kitsu')
const Kitsu = require('kitsu/lib/legacy')
const Kitsu = require('kitsu/lib/node')
const api = new Kitsu()
const api = new Kitsu({
baseURL: 'https://api.example/2'
})
const { data } = await api.get('anime')
api.get('anime')
.then(({ data }) => { ... })
.catch(err => throw err)
api.get('anime', { params }, { headers })
api.get('anime/1')
api.get('anime/1/episodes')
api.create('post', {
content: 'some content'
})
api.update('post', {
id: '1',
content: 'new content'
})
api.remove('post', 1)
More Examples
If you're working with Kitsu.io's API, their API docs lists all available resources with their attributes and relationships
API
Table of Contents
Kitsu
Parameters
options
Object Options
options.baseURL
string Set the API endpoint (default https://kitsu.io/api/edge
)options.headers
Object Additional headers to send with requestsoptions.camelCaseTypes
boolean If true, the type
value will be camelCased, e.g library-entries
and library_entries
become libraryEntries
(default true
)options.resourceCase
string kebab
, snake
or none
. If kebab
, /libraryEntries
will become /library-entries
. If snake
, /libraryEntries
will become /library_entries
, If none
, /libraryEntries
will be unchanged (default kebab
)options.pluralize
boolean If true
, /user
will become /users
in the URL request and type
will be pluralized in post, patch and delete requests - user
-> users
(default true
)options.timeout
number Set the request timeout in milliseconds (default 30000
)
Examples
const api = new Kitsu()
const api = new Kitsu({
baseURL: 'https://api.example.org/2'
})
const api = new Kitsu({
headers: {
'User-Agent': 'MyApp/1.0.0 (github.com/username/repo)',
Authorization: 'Bearer 1234567890'
}
})
get
Fetch resources
Aliases: fetch
Parameters
model
string Model to fetch data fromparams
Object JSON-API request queries (optional, default {}
)
headers
Object Additional headers to send with request (optional, default {}
)
Examples
api.get('users', {
fields: {
users: 'name,birthday'
},
filter: {
name: 'wopian'
}
})
api.get('anime', {
include: 'categories'
})
api.get('anime', {
include: 'categories',
filter: { id: '2' }
})
api.get('anime/2', {
include: 'categories'
})
api.get('anime/2/categories')
try {
const { data } = api.get('anime')
} catch (err) {
if (err.errors) err.errors.forEach(error => {
console.log(error)
})
}
api.get('anime')
.then(res => res.data)
.catch(err => {
if (err.errors) err.errors.forEach(error => {
console.log(error)
})
})
Returns Object JSON-parsed response
Get the current headers or add additional headers
Examples
api.headers
api.headers['User-Agent']
api.headers['Authorization'] = 'Bearer 1234567890'
Returns Object All the current headers
isAuth
Check if the client is authenticated (oAuth2/Authorization header)
Examples
if (api.isAuth) console.log('Authenticated')
else console.log('Not authenticated')
Returns boolean
patch
Update a resource
Aliases: update
Parameters
model
string Model to update data inbody
Object Data to send in the requestheaders
Object Additional headers to send with request (optional, default {}
)
Examples
api.update('posts', {
id: '12345678',
content: 'Goodbye World'
})
Returns Object JSON-parsed response
post
Create a new resource
Aliases: create
Parameters
model
string Model to create a resource underbody
Object Data to send in the requestheaders
Object Additional headers to send with request (optional, default {}
)
Examples
api.create('posts', {
content: 'Hello World',
targetUser: {
id: '42603',
type: 'users'
},
user: {
id: '42603',
type: 'users'
}
})
Returns Object JSON-parsed response
remove
Remove a resource
Parameters
model
string Model to remove data fromid
(string | number) Resource ID to removeheaders
Object Additional headers to send with request (optional, default {}
)
Examples
api.remove('posts', 123)
Returns Object JSON-parsed response
self
Get the authenticated user's data
Note: Requires the JSON:API server to support filter[self]=true
Parameters
params
Object JSON-API request queries (optional, default {}
)
headers
Object Additional headers to send with request (optional, default {}
)
Examples
api.self()
api.self({
fields: 'name,birthday'
})
Returns Object JSON-parsed response
Contributing
See CONTRIBUTING
Releases
See CHANGELOG
License
All code released under MIT