Kitsu Core
Core JSON:API serialisation and deserialisation components
Check out the Migration Guide for breaking changes in 6.x
Features
- JSON-API 1.0 compliant
- Automatically links relationships to data
- Works in Node and on the web
- Uses the Promise API
Node / Browser Support
Package | Package Size* | Node | Chrome | Firefox | Safari | Edge | IE |
---|
kitsu-core | ≤ 4.3 kb | 6+ | 54+ | 51+ | 10+ | 15+ | |
kitsu-core/node | ≤ 1.5 kb | 6+ | | | | | |
* Including all dependencies, minified & gzipped
Install
Yarn / NPM
yarn add kitsu-core
npm install kitsu-core
import { camel } from 'kitsu-core'
import { camel } from 'kitsu-core/node'
const { camel } = require('kitsu-core')
camel(...)
Packd CDN
<script src='https://bundle.run/kitsu-core@6?name=kitsuCore'></script>
kitsuCore.camel(...)
API
Table of Contents
deattribute
packages/kitsu-core/src/deattribute/index.js:29-38
Hoists attributes to be top-level
Parameters
Examples
Deattribute an array of resources
const data = [
{
id: '1',
type: 'users',
attributes: { slug: 'wopian' }
}
]
const output = await deattribute(data)
Deattribute a resource
const data = {
id: '1',
type: 'users',
attributes: { slug: 'wopian' }
}
const output = await deattribute(data)
Returns (Object | Array) Deattributed resource data
deserialise
packages/kitsu-core/src/deserialise/index.js:56-70
Deserialises a JSON-API response
Parameters
Examples
Deserialise with a basic data object
await deserialise({
data: {
id: '1',
attributes: { liked: true }
},
meta: { hello: 'world' }
})
Deserialise with relationships
await 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:7-13
Mutates an error and rethrows it
Parameters
filterIncludes
packages/kitsu-core/src/filterIncludes/index.js:12-21
Filters includes for the specific relationship
Parameters
included
Object The response included objectopts
Object
opts.id
string The relationship IDopts.type
string The relationship type
Returns Array The matched includes
linkRelationships
packages/kitsu-core/src/linkRelationships/index.js:55-74
Links relationships to included data
Parameters
data
Object The response data objectincluded
Object The response included object
queryFormat
packages/kitsu-core/src/query/index.js:8-11
Formats a single URL query
Parameters
value
string Right-hand side of the querykey
string Left-hand side of the query
Returns string URL query string
query
packages/kitsu-core/src/query/index.js:20-31
Constructs a URL query string for JSON:API parameters
Parameters
params
Object Parameters to parseprefix
string Prefix for nested parameters - used internally (default null
) (optional, default null
)
Returns string URL query string
isValid
packages/kitsu-core/src/serialise/index.js:11-20
Checks if data is valid for serialisation
Parameters
serialise
packages/kitsu-core/src/serialise/index.js:67-92
Serialises an object into a JSON-API structure
Parameters
model
string Request modelobj
Object The data (optional, default {}
)method
string Request type (optional, default 'POST'
)
Examples
Due to its usage in kitsu, it MUST be called with this set in 6.0.x
import { serialise, camel, kebab } from 'kitsu-core'
import plural from 'pluralize'
const output = await serialise.apply({ camel, resCase: kebab, plural }, [ model, obj, 'PATCH' ])
Returns Object The serialised data
camel
packages/kitsu-core/src/camel/index.js:14-14
Converts kebab-case and snake_case into camelCase
Parameters
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 kekab-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