Kitsu Core
Core JSON:API serialisation and deserialisation components
Migration guide for v9 and previous major releases
Features
- JSON-API 1.0 compliant
- Automatically links relationships to data
- Works in Node and on the web
Node / Browser Support
Package | Package Size* | Node | Chrome | Firefox | Safari | Edge |
---|
kitsu-core | ≤ 1.5 kb | 10+ | 67+ | 68+ | 12+ | 18+ |
* Including all dependencies, minified & gzipped
Install
Yarn / NPM
yarn add kitsu-core
npm install kitsu-core
import { camel } from 'kitsu-core'
const { camel } = require('kitsu-core')
camel(...)
CDNs
<script src='https://cdn.jsdelivr.net/npm/kitsu-core'></script>
<script src='https://unpkg.com/kitsu-core'></script>
kitsuCore.camel(...)
API
Table of Contents
deattribute
packages/kitsu-core/src/deattribute/index.js:29-51
Hoists attributes to be top-level
Parameters
Examples
Deattribute an array of resources
const data = [
{
id: '1',
type: 'users',
attributes: { slug: 'wopian' }
}
]
const output = deattribute(data)
Deattribute a resource
const data = {
id: '1',
type: 'users',
attributes: { slug: 'wopian' }
}
const output = deattribute(data)
Returns (Object | Array<Object>) Deattributed resource data
deserialise
packages/kitsu-core/src/deserialise/index.js:57-72
Deserialises a JSON-API response
Parameters
response
Object The raw JSON:API response object
Examples
Deserialise with a basic data object
deserialise({
data: {
id: '1',
attributes: { liked: true }
},
meta: { hello: 'world' }
})
Deserialise with relationships
deserialise({
data: {
id: '1',
relationships: {
user: {
data: {
type: 'users',
id: '2' }
}
}
},
included: [
{
type: 'users',
id: '2',
attributes: { slug: 'wopian' }
}
]
})
Returns Object The deserialised response
error
packages/kitsu-core/src/error/index.js:27-33
Uniform error handling for Axios, JSON:API and internal package errors. Mutated Error object is rethrown to the caller.
Parameters
Examples
error('Hello')
error({errors: [ { code: 400 } ]})
error({
response: {
data: {
errors: [ {
title: 'Filter is not allowed',
detail: 'x is not allowed',
code: '102',
status: '400'
} ]
}
}
})
- Throws Object The mutated Error
filterIncludes
packages/kitsu-core/src/filterIncludes/index.js:33-46
Filters includes for the specific relationship requested
Parameters
included
Array<Object> The response included objectrelationship
Object
relationship.id
string The relationship IDrelationship.type
string The relationship type
Examples
const includes = [
{
id: '1',
type: 'users',
attributes: { name: 'Emma' }
},
{
id: '2',
type: 'users',
attributes: { name: 'Josh' }
}
]
const relationship = { id: '1', type: 'users' }
const response = filterIncludes(includes, relationship)
Returns Array<Object> The matched includes
linkRelationships
packages/kitsu-core/src/linkRelationships/index.js:97-117
Links relationships to included data
Parameters
data
Object The response data objectincluded
Array<Object>? The response included object (optional, default []
)
Examples
const data = {
attributes: { author: 'Joe' },
relationships: {
author: {
data: { id: '1', type: 'people' }
}
}
}
const included = [ {
id: '1',
type: 'people',
attributes: { name: 'Joe' }
} ]
const output = linkRelationships(data, included)
Returns any Parsed data
query
packages/kitsu-core/src/query/index.js:33-44
Constructs a URL query string for JSON:API parameters
Parameters
params
Object? Parameters to parseprefix
string? Prefix for nested parameters - used internally (optional, default null
)
Examples
query({
filter: {
slug: 'cowboy-bebop',
title: {
value: 'foo'
}
}
sort: '-id'
})
Returns string URL query string
serialise
packages/kitsu-core/src/serialise/index.js:249-260
Serialises an object into a JSON-API structure
Parameters
type
string Resource typedata
(Object | Array<Object>)? The data (optional, default {}
)method
string? Request type (PATCH, POST, DELETE) (optional, default 'POST'
)options
Object? Optional configuration for camelCase and pluralisation handling (optional, default {}
)
options.camelCaseTypes
Function Convert library-entries and library_entries to libraryEntries (default no conversion). To use parameter, import camel from kitsu-core (optional, default s=>s
)options.pluralTypes
Function Pluralise types (default no pluralisation). To use parameter, import pluralize (or another pluralisation npm package) (optional, default s=>s
)
Examples
Setting camelCaseTypes and pluralTypes options (example shows options used by kitsu
by default)
import { serialise, camel } from 'kitsu-core'
import pluralize from 'pluralize'
const model = 'anime'
const obj = { id: '1', slug: 'shirobako' }
const output = serialise(model, obj, 'PATCH', { camelCaseTypes: camel, pluralTypes: pluralize })
Basic usage (no case conversion or pluralisation)
import { serialise } from 'kitsu-core'
const model = 'anime'
const obj = { id: '1', slug: 'shirobako' }
const output = serialise(model, obj, 'PATCH')
Returns Object The serialised data
splitModel
packages/kitsu-core/src/splitModel/index.js:29-39
Split model name from the model's resource URL
Parameters
url
string URL path for the modeloptions
Object? Optional configuration for camelCase and pluralisation handling
options.resourceCase
Function Convert libraryEntries to library-entries or library_entries (default no conversion). To use parameter, import kebab or snake from kitsu-core (optional, default s=>s
)options.pluralModel
Function Pluralise models (default no pluralisation). To use parameter, import pluralize (or another pluralisation npm package) (optional, default s=>s
)
Examples
splitModel('posts/1/comments')
With pluralModel option
import plural from 'pluralize'
splitModel('posts/1/comment', { pluralModel: plural })
With resourceCase option
import { kebab, snake } from 'kitsu-core'
splitModel('libraryEntries', { resourceCase: kebab })
splitModel('libraryEntries', { resourceCase: snake })
Returns [string, string] } Array containing the model name and the resource URL with pluralisation applied
camel
packages/kitsu-core/src/camel/index.js:14-14
Converts kebab-case and snake_case into camelCase
Parameters
input
string String to convert
Examples
Convert kebab-case
camel('hello-world')
Convert snake_case
camel('hello_world')
Returns string camelCase formatted string
kebab
packages/kitsu-core/src/kebab/index.js:11-11
Converts camelCase into kebab-case
Parameters
Examples
kebab('helloWorld')
Returns string kebab-case formatted string
snake
packages/kitsu-core/src/snake/index.js:11-11
Converts camelCase into snake_case
Parameters
Examples
snake('helloWorld')
Returns string snake_case formatted string
Contributing
See CONTRIBUTING
Releases
See CHANGELOG
License
All code released under MIT