Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

kitsu-core

wopian46.8kMIT11.1.0TypeScript support: included

Simple, lightweight & framework agnostic JSON:API (de)serialsation components

kitsu, jsonapi, json-api, serialize, deserialize

readme

Kitsu Core

npm npm bundlephobia types

checks repoDependants contributors sponsor

Simple, lightweight & framework agnostic JSON:API (de)serialisation components

Migration guide for v11 & previous major releases

#

Features

  • JSON-API 1.0 compliant
  • Automatically links relationships to data
  • Works in Node & browsers
  • Tree shakeable components
  • Zero dependencies

Node / Browser Support

Package Package
Size*
ESM Size Node† Chrome† Firefox† Safari† Edge†
kitsu-core ≤ 2.16 kb ≤ 1.75 KB 18+ 116+ 118+ 17.1+ 134+

* Minified with brotli † Guaranteed supported versions. Older versions may still work or might require polyfills

Install

Yarn / NPM

yarn add kitsu-core
npm install kitsu-core
import { camel } from 'kitsu-core'      // ES Modules and Babel
const { camel } = require('kitsu-core') // CommonJS and Browserify

camel(...)

CDNs

<!-- jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/kitsu-core"></script>

<!-- unpkg -->
<script src="https://unpkg.com/kitsu-core"></script>
kitsuCore.camel(...)

Contributing

See CONTRIBUTING

Releases

See CHANGELOG

License

All code released under MIT

API

Table of Contents

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') // 'helloWorld'

Convert snake_case

camel('hello_world') // 'helloWorld'

Returns string camelCase formatted string

deattribute

packages/kitsu-core/src/deattribute/index.js:29-51

Hoists attributes to be top-level

Parameters

Examples

Deattribute an array of resources

// JSON:API 'data' field
const data = [
  {
    id: '1',
    type: 'users',
    attributes: { slug: 'wopian' }
  }
]

const output = deattribute(data) // [ { id: '1', type: 'users', slug: 'wopian' } ]

Deattribute a resource

// JSON:API 'data' field
const data = {
  id: '1',
  type: 'users',
  attributes: { slug: 'wopian' }
}

const output = deattribute(data) // { id: '1', type: 'users', slug: 'wopian' }

Returns (Object | Array<Object>) Deattributed resource data

hoist

packages/kitsu-core/src/deserialise/index.js:24-77

Recursively traverses and clones an object or array, handling cyclic references. If the object is a wrapper of the form { data: ... }, it unwraps and processes the data property.

Parameters

  • object any The input to hoist (object or array)

Returns any The hoisted object or array

deserialise

packages/kitsu-core/src/deserialise/index.js:150-170

Deserialises a JSON-API response

Parameters

  • response Object The raw JSON:API response object
  • options Object Deserialisation options (optional, default {})

    • options.hoistData boolean If enabled, the contents of the data property will be hoisted to the parent. This provides a flatter response object, but removes access to links and meta properties. It will transform:js { data: { id: '1', type: 'people', coworkers: data: [ { id: '2', type: 'people' } ] } }into the following:js { id: '1', type: 'people', coworkers: [ { id: '2', type: 'people' } ] } (optional, default false)

Examples

Deserialise with a basic data object

deserialise({
  data: {
    id: '1',
    attributes: { liked: true }
  },
  meta: { hello: 'world' }
}) // { data: { id: '1', 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' }
    }
  ]
}) // { data: { id: '1', user: { data: { type: 'users', id: '2', 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 object
  • relationship Object

    • relationship.id string The relationship ID
    • relationship.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)
// {
//   id: '1',
//   type: 'users',
//   attributes: { name: 'Emma' }
// }

Returns Object The matched includes

kebab

packages/kitsu-core/src/kebab/index.js:11-11

Converts camelCase into kebab-case

Parameters

  • input string camelCase string

Examples

kebab('helloWorld') // 'hello-world'

Returns string kebab-case formatted string

linkRelationships

packages/kitsu-core/src/linkRelationships/index.js:144-164

Links relationships to included data

Parameters

  • data Object The response data object
  • included Array<Object>? The response included object (optional, default [])
  • previouslyLinked Object? A mapping of already visited resources (internal use only) (optional, default {})
  • relationshipCache Object? A cache object for relationship meta and links (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)
// {
//   attributes: { author: 'Joe' },
//   author: {
//     data: { id: '1', name: 'Joe', type: 'people' }
//   }
// }

Returns any Parsed data

isDeepEqual

packages/kitsu-core/src/deepEqual/index.js:18-42

Compare two objects equality

Parameters

  • left Object Object to compare against the right object
  • right Object Object to compare against the left object

Examples

Deep equality check

isDeepEqual({
  firstName: 'John',
  lastName: 'Doe',
  age: 35
},{
  firstName: 'John',
  lastName: 'Doe',
  age: 35
}) // true

Returns boolean Whether the objects are equal

query

packages/kitsu-core/src/query/index.js:57-66

Constructs a URL query string for JSON:API parameters

Parameters

  • params Object? Parameters to parse
  • prefix string? Prefix for nested parameters - used internally (optional, default undefined)
  • traditional boolean Use the traditional (default) or modern param serializer. Set to false if your server is running Ruby on Rails or other modern web frameworks (optional, default true)

Examples

query({
  filter: {
    slug: 'cowboy-bebop',
    title: {
      value: 'foo'
    }
  }
 sort: '-id'
})
// filter%5Bslug%5D=cowboy-bebop&filter%5Btitle%5D%5Bvalue%5D=foo&sort=-id

Returns string URL query string

serialise

packages/kitsu-core/src/serialise/index.js:210-221

Serialises an object into a JSON-API structure

Parameters

  • type string Resource type
  • data (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 the kitsu package by default)

import { serialise, camel } from 'kitsu-core'
import pluralize from 'pluralize'

const model = 'anime'
const obj = { id: '1', slug: 'shirobako' }

// { data: { id: '1', type: 'anime', attributes: { 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' }

// { data: { id: '1', type: 'anime', attributes: { slug: 'shirobako' } } }
const output = serialise(model, obj, 'PATCH')

Returns Object The serialised data

snake

packages/kitsu-core/src/snake/index.js:11-11

Converts camelCase into snake_case

Parameters

  • input string camelCase string

Examples

snake('helloWorld') // 'hello_world'

Returns string snake_case formatted string

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 model
  • options 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')
// [ 'comments', 'posts/1/comments' ]

With pluralModel option

import plural from 'pluralize'
splitModel('posts/1/comment', { pluralModel: plural })
// [ 'comment', 'posts/1/comments' ]

With resourceCase option

import { kebab, snake } from 'kitsu-core'
splitModel('libraryEntries', { resourceCase: kebab })
// [ 'libraryEntries', 'library-entries' ]

splitModel('libraryEntries', { resourceCase: snake })
// [ 'libraryEntries', 'library_entries' ]

Returns [string, string] } Array containing the model name and the resource URL with pluralisation applied

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

11.1.0 (2025-08-25)

Features

  • kitsu-core: implement opt-in data hoisting when deserialising response (72dc2be)

11.0.3 (2025-08-07)

Bug Fixes

  • docs: replace dead badges with active badges (ecdaf75)

11.0.2 (2025-08-07)

Note: Version bump only for package kitsu-core

11.0.1 (2025-04-09)

Note: Version bump only for package kitsu-core

11.0.0 (2025-04-09)

Bug Fixes

  • kitsu-core: correct jsdoc syntax for deserialiseArray (9096868)
  • kitsu-core: correct jsdoc syntax for filterIncludes (bb74de9)
  • kitsu-core: remove unused meta jsdoc (ead2add)
  • kitsu-core: return an empty string if splitModel is passed an empty url (5ef65dd)

Features

  • kitsu-core: return an empty string if paramKeyName is passed an undefined value (6112780)

BREAKING CHANGES

  • kitsu-core: drop support for node 14 and 16. minimum supported node is now 18 (5d715c2)

10.2.1 (2025-04-05)

Note: Version bump only for package kitsu-core

10.2.0 (2025-01-03)

Note: Version bump only for package kitsu-core

10.1.5 (2024-03-30)

Note: Version bump only for package kitsu-core

10.1.4 (2024-03-14)

Bug Fixes

  • kitsu-core: export types in fine-grained exports (b1f15bf)

10.1.3 (2024-03-14)

Bug Fixes

  • kitsu-core: export types in fine-grained exports (b1f15bf)

10.1.2 (2023-06-20)

Note: Version bump only for package kitsu-core

10.1.1 (2023-04-26)

Bug Fixes

  • kitsu-core: Merge meta keys to preserve data (#853) (474d91b)

10.1.0 (2023-02-28)

Features

  • kitsu: Configurable modern query serializer (ef94ae0)

10.0.5 (2023-02-28)

Bug Fixes

  • kitsu-core: Allow empty POST body (16cd20d)

10.0.4 (2022-10-30)

Note: Version bump only for package kitsu-core

10.0.3 (2022-10-30)

Bug Fixes

  • kitsu-core: build list queryparams (#781) (d34e871)
  • kitsu-core: ensure linkedRelationships does not overwrite meta object references (#783) (97ba151)

10.0.2 (2022-08-17)

Bug Fixes

  • kitsu-core: deserialize meta only relationships (#727) (5b2c64c)

10.0.1 (2022-08-12)

Bug Fixes

  • kitsu-core: prevent relationship mutation during multi-circular deserialisation (#722) (d261d4c)

10.0.0 (2022-08-11)

Note: Version bump only for package kitsu-core

10.0.0-alpha.26 (2022-08-11)

Bug Fixes

  • kitsu-core: fix inability to link relationship links on circular resources (#699) (95d3453)

10.0.0-alpha.25 (2022-07-13)

Note: Version bump only for package kitsu-core

10.0.0-alpha.24 (2022-06-23)

Bug Fixes

  • kitsu-core: fix deserialisation of relationships from primary data (#683) (42b871e)

10.0.0-alpha.23 (2022-05-03)

Note: Version bump only for package kitsu-core

10.0.0-alpha.22 (2022-03-04)

Bug Fixes

  • kitsu-core: use typeof instead of constructor comparison for checking if Object (#654) (af9d893)

10.0.0-alpha.21 (2022-02-17)

Note: Version bump only for package kitsu-core

10.0.0-alpha.20 (2022-01-23)

Note: Version bump only for package kitsu-core

10.0.0-alpha.19 (2021-12-09)

Note: Version bump only for package kitsu-core

10.0.0-alpha.18 (2021-11-02)

Note: Version bump only for package kitsu-core

10.0.0-alpha.17 (2021-10-19)

Bug Fixes

  • kitsu-core: Deserialize and link nested relations (#601) (5bb705a)

10.0.0-alpha.16 (2021-10-18)

Note: Version bump only for package kitsu-core

10.0.0-alpha.15 (2021-10-08)

Note: Version bump only for package kitsu-core

10.0.0-alpha.14 (2021-08-16)

Note: Version bump only for package kitsu-core

10.0.0-alpha.13 (2021-06-07)

Note: Version bump only for package kitsu-core

10.0.0-alpha.12 (2021-06-07)

Note: Version bump only for package kitsu-core

10.0.0-alpha.11 (2021-06-07)

Note: Version bump only for package kitsu-core

10.0.0-alpha.10 (2021-02-04)

Bug Fixes

  • kitsu-core: prevent empty relationships from being stripped during serialisation (8a7d453), closes #517

Chores

  • release: update documentation (92d4246)

Documentation Changes

  • kitsu-core: fix typo in MIGRATING.md (9a51336)

Tests

  • add test for empty to-one/to-many relationship serialisation (0dda0fe)

10.0.0-alpha.9 (2021-01-06)

Chores

  • release: update documentation (840d383)

Documentation Changes

  • update minimum browser support for compiled outputs (1bd4f77)

Refactors

  • kitsu-core: change camel, kebab and snake to named exports (1accdbb)
  • kitsu-core: change output directory to dist (ddcbe09), closes #510

10.0.0-alpha.8 (2020-12-06)

Build System / Dependencies

Chores

  • release: update documentation (fe39500)

10.0.0-alpha.7 (2020-10-25)

Build System / Dependencies

Chores

  • release: update documentation (6d025ca)

10.0.0-alpha.6 (2020-08-22)

Chores

  • release: update documentation (e71b1dc)

10.0.0-alpha.5 (2020-08-04)

Chores

  • release: update documentation (2f479c2)

Documentation Changes

10.0.0-alpha.4 (2020-08-04)

Chores

  • release: update documentation (56b33eb)
  • add funding to package.json (8b00d5a)

Documentation Changes

  • specify the Kitsu package default (239cd86)

10.0.0-alpha.3 (2020-07-26)

Chores

  • release: update documentation (f89a7cf)

Documentation Changes

  • add typescript types badges (3a09066)

10.0.0-alpha.2 (2020-07-16)

Bug Fixes

  • kitsu-core: allow longer prototype chain on serialise (#447) (7826683)

Chores

  • release: update documentation (699bb64)

Documentation Changes

  • move contributing, releases and license above api documentation (9c7d986)

10.0.0-alpha.1 (2020-06-15)

Chores

  • release: update documentation (1b1f6d6)
  • release: update documentation (883512b)

10.0.0-alpha.0 (2020-06-15)

Bug Fixes

  • kitsu-core: don't serialise meta object as an attribute (dbd625c)
  • kitsu-core: serialise v9 relationship structures (32c40bf)

Chores

  • release: update documentation (da50d92)

Documentation Changes

  • kitsu-core: update internal serialise JSDoc (fcd06d9)
  • update READMEs (469d23a)
  • kitsu: add missing comma to example output (31b21da)
  • kitsu-core: add v10 migration guide (b9b4f6e)

Refactors

  • kitsu-core: remove redundant internal function (22a7bc9)

9.1.11 (2020-06-14)

Chores

  • release: update documentation (ba00f68)

Other Changes

9.1.10 (2020-05-31)

Chores

  • release: update documentation (3817edc)

Documentation Changes

  • fix spacing in description (8811add)

9.1.9 (2020-05-31)

Chores

  • release: update documentation (220ad78)

Continuous Integration

Documentation Changes

  • update package descriptions (29b8693)

9.1.8 (2020-05-28)

Bug Fixes

  • add export paths with .js and .mjs for Node 13.1/14 exports field (a8a06dd)

Chores

  • release: update documentation (fa0cdd8)
  • release: update documentation (ce00974)

9.1.7 (2020-05-28)

Bug Fixes

  • use Node 13.1/14 exports field in package.json (0a4692a)

Chores

  • release: update documentation (8424d78)

9.1.6 (2020-05-21)

Chores

  • release: update documentation (dca0f14)

Continuous Integration

  • npm: ignore yarn log files (297d1ef)

9.1.5 (2020-05-21)

Bug Fixes

  • kitsu-core: preverve serialised relationship attributes (ddcc17a), closes #418

Chores

  • release: update documentation (912f59d)

9.1.4 (2020-05-21)

Chores

  • release: update documentation (c2b9e13)
  • release: update documentation (e8b37f1)

Documentation Changes

  • kitsu: enumerate resourceCase string values (52c1c82)

9.1.3 (2020-05-21)

Chores

  • release: update documentation (de730f2)

9.1.2 (2020-05-21)

Bug Fixes

  • kitsu-core: optional chain constructor calls to allow invalid JSON values (66d76ef), closes #416

Chores

  • release: update documentation (20e7cc6)

Documentation Changes

  • kitsu-core: declare optional parameters in JSDoc syntax (a78a075)
  • autogenerate typescript definitions (6e1879f)
  • update JSDoc Array syntax for better TypeScript usability (8f147ab)

9.1.1 (2020-05-21)

Bug Fixes

  • kitsu-core: throw error if type is missing during serialisation (570ef11)

Chores

  • release: update documentation (1c19a06)

9.1.0 (2020-05-21)

Bug Fixes

  • kitsu-core: resolve linkedRelationships regression introduced in 568eff5 (66095cc)

Chores

  • release: update documentation (d77384c)
  • increase package warning limit (c0136dc)

Documentation Changes

  • correct errors in types (9ad8fc0)
  • kitsu-core: add internal documentation for new private functions (0d10ba3)
  • kitsu-core: update description of deserialise parameter (94dc48a)

New Features

  • kitsu-core: support the bulk extension specification (serialise arrays) (920ece3), closes #336

Refactors

  • kitsu-core: cleanup linkRelationships + use optional chaining (568eff5)
  • kitsu-core: use optional chaining in deserialise (43d5d4b)

9.0.7 (2020-05-19)

Chores

  • release: update documentation (523553e)

Other Changes

9.0.6 (2020-05-07)

Chores

  • release: update documentation (03fc40e)
  • remove rogue console.log (29d3ae3)

Documentation Changes

  • kitsu-core: remove node 12 notice (45f20bb)

9.0.5 (2020-05-07)

Chores

  • release: update documentation (2df72e7)
  • trim CHANGELOG length (af7db19)

9.0.4 (2020-05-07)

Chores

  • release: update documentation (9561c63)

Documentation Changes

  • kitsu-core: add example for query (63c15f4)
  • kitsu-core: add examples for error (cb40de1)
  • kitsu-core: add examples for filterIncludes (66b5a6f)
  • kitsu-core: add examples for linkRelationships (ff05659)

9.0.3 (2020-05-07)

Chores

  • release: update documentation (95c3fbb)

9.0.2 (2020-05-07)

Bug Fixes

  • kitsu-core: correctly parse attributes.attributes (closes #137) (b058e42)

Chores

  • release: update documentation (84d00c5)

Tests

  • kitsu-core: add test for ensuring all relationships exist in output (1fc81d9)

9.0.1 (2020-05-07)

Chores

  • release: update documentation (03ec026)

Documentation Changes

  • kitsu-core: add migration guide link to README (bf282af)

9.0.0 (2020-05-07)

Build System / Dependencies

Chores

  • release: update documentation (08362a8)

Documentation Changes

  • kitsu-core: add 9.0.0 migration guide (8576749)
  • update README (6a73433)
  • kitsu-core: use lowercase string for param type (8254710)

New Features

  • kitsu-core: add splitModel (782d1b6)
  • kitsu-core: preserve links in relationships during deserialisation (cbf50df)

Refactors

  • kitsu-core: pass camelCase and pluralisation options as arguments to serialise (34b9cae)

BREAKING CHANGES

  • kitsu-core: for deserialise and linkRelationships
  • kitsu-core: serialise.apply[{ camel, resCase, plural}, [ model, data, method ]) is no longer neccessary. New syntax is serialise(model, data, method, { camelCaseTypes: camel, pluralTypes: plural}).