Détail du package

kitsu

wopian14.4kMIT11.0.1

A simple, lightweight & framework agnostic JSON:API client using Axios

kitsu, kitsu.app, anime, manga

readme

Kitsu

npm npm bundlephobia types

checks coverage maintainability repoDependants

contributors sponsor

A simple, lightweight & framework agnostic JSON:API client using Axios

Migration guide for v10 & previous major releases

#

Features

  • JSON-API 1.0 compliant
  • Automatically links relationships to data
  • Works in Node & browsers
  • Uses the Promise API

Node / Browser Support

Package Package
Size*
ESM Size† Node Chrome Firefox Safari Edge
kitsu ≤ 8.7 kb ≤ 8.6 KB 14+ 83+ 78+ 13.1+ 95+

* Including all dependencies & minified with brotli † EcmaScript Modules package size*

Response Comparison

<summary>Object from a GET Response by a JSON:API Server</summary> json5 { 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' } } ] }
<summary>Object from a GET Response with kitsu:</summary> json5 { data: { id: '1' type: 'articles' title: 'JSON API paints my bikeshed' author: { data: { id: '42' type: 'people' name: 'John' } } } }

Install

Yarn / NPM

yarn add kitsu
npm install kitsu
import Kitsu from "kitsu"; // ES Modules & Babel
const Kitsu = require("kitsu"); // CommonJS & Browserify

Quick Start

// kitsu.app's API
const api = new Kitsu()

// Other JSON:API servers
const api = new Kitsu({
  baseURL: 'https://api.example/2'
})

// Using with async/await
const res = await api.get('anime')

// Using with Promises
api.get('anime')
  .then(res => { ... })
  .catch(err => { ... })

// Fetching resources (get/fetch)
api.fetch('anime')
api.fetch('anime', { params: { filter: { id: 1 } } })
api.fetch('anime/1/episodes')
api.fetch('anime/1/relationships/episodes')

// Creating resources (post/create)
api.create('post', {
  content: 'some content'
})

// Updating resources (patch/update)
api.update('post', {
  id: '1',
  content: 'new content'
})

// Deleting resources (delete/remove)
api.remove('post', 1)

// JSON:API parameters
api.get('users', {
  params: {
    include: 'followers,waifu.character',
    fields: {
      users: 'slug,followers,waifu'
    },
    filter: {
      slug: 'wopian'
    },
    sort: '-id',
    page: {
      limit: 5,
      offset: 0
    }
  }
})

More Examples

If you're working with kitsu.app's API, their API docs lists all available resources with their attributes & relationships

Contributing

See CONTRIBUTING

Releases

See CHANGELOG

License

All code released under MIT

API

Table of Contents

Kitsu

packages/kitsu/src/index.js:31-567

Creates a new kitsu instance

Parameters

  • options Object? Options (optional, default {})

    • options.baseURL string Set the API endpoint (optional, default 'https://kitsu.app/api/edge')
    • options.headers Object? Additional headers to send with the requests
    • options.query ("traditional" | "modern" | Function) Query serializer function to use. This will impact the way keys are serialized when passing arrays as query parameters. 'modern' is recommended for new projects. (optional, default traditional)
    • options.camelCaseTypes boolean If enabled, type will be converted to camelCase from kebab-casae or snake_case (optional, default true)
    • options.resourceCase ("kebab" | "snake" | "none") Case to convert camelCase to. kebab - /library-entries; snake - /library_entries;none-/libraryEntries` (optional, default kebab)
    • options.pluralize boolean If enabled, /user will become /users in the URL request and type will be pluralized in POST, PATCH and DELETE requests (optional, default true)
    • options.timeout number Set the request timeout in milliseconds (optional, default 30000)
    • options.axiosOptions Object? Additional options for the axios instance (see axios/axios#request-config for details)

Examples

Using with kitsu.app's API

const api = new Kitsu()

Using another API server

const api = new Kitsu({
  baseURL: 'https://api.example.org/2'
})

Setting headers

const api = new Kitsu({
  headers: {
    'User-Agent': 'MyApp/1.0.0 (github.com/username/repo)',
    Authorization: 'Bearer 1234567890'
  }
})

plural

packages/kitsu/src/index.js:58-59

If pluralization is enabled (default, see Kitsu constructor docs) then pluralization rules can be added

Examples

Adding an uncountable pluralization rule

api.plural.plural('paper') //=> 'papers'
api.plural.addUncountableRule('paper')
api.plural.plural('paper') //=> 'paper'

headers

packages/kitsu/src/index.js:73-73

Get the current headers or add additional headers

Examples

Get all headers

api.headers

Get a single header's value

api.headers['User-Agent']

Add or update a header's value

api.headers['Authorization'] = 'Bearer 1234567890'

Returns Object All the current headers

interceptors

packages/kitsu/src/index.js:120-120

Axios Interceptors (alias of axios.interceptors)

You can intercept responses before they are handled by get, post, patch and delete and before requests are sent to the API server.

Examples

Request Interceptor

// Add a request interceptor
api.interceptors.request.use(config => {
   // Do something before request is sent
   return config
}, error => {
   // Do something with the request error
   return Promise.reject(error)
})

Response Interceptor

// Add a response interceptor
api.interceptors.response.use(response => {
   // Any status code that lie within the range of 2xx cause this function to trigger
   // Do something with response data
   return response
}, error => {
   // Any status codes that falls outside the range of 2xx cause this function to trigger
   // Do something with response error
   return Promise.reject(error)
})

Removing Interceptors

const myInterceptor = api.interceptors.request.use(function () {...})
api.interceptors.request.eject(myInterceptor)

get

packages/kitsu/src/index.js:218-253

Fetch resources (alias fetch)

Parameters
  • model string Resource to fetch data from. Expected formats are :resource, :resource/:id, :resource/:id/:relationship or :resource/:id/relationships/:relationship
  • config Object? Additional configuration (optional, default {})

    • config.headers Object? Additional headers to send with the request
    • config.params Object? JSON:API request queries. JSON:API query parameters not listed are supported

      • config.params.fields Object? Return a sparse fieldset with only the included attributes/relationships - JSON:API Sparse Fieldsets
      • config.params.filter Object? Filter dataset by attribute values - JSON:API Filtering
      • config.params.include string? Include relationship data - JSON:API Includes
      • config.params.sort string? Sort dataset by one or more comma separated attributes (prepend - for descending order) - JSON:API Sorting
      • config.params.page Object? JSON:API Pagination. All pagination strategies are supported, even if they are not listed below.

        • config.params.page.limit number? Number of resources to return in request (Offset-based) - Note: For kitsu.app, max is 20 except on libraryEntries which has a max of 500
        • config.params.page.offset number? Number of resources to offset the dataset by (Offset-based)
        • config.params.page.number number? Page of resources to return in request (Page-based) - Note: Not supported on kitsu.app
        • config.params.page.size number? Number of resources to return in request (Page-based and cursor-based) - Note: Not supported on kitsu.app
        • config.params.page.before string? Get the previous page of resources (Cursor-based) - Note: Not Supported on kitsu.app
        • config.params.page.after string? Get the next page of resources (Cursor-based) - Note: Not Supported on kitsu.app
    • config.axiosOptions Object? Additional options for the axios instance (see axios/axios#request-config for details)
Examples

Getting a resource with JSON:API parameters

api.get('users', {
  params: {
    fields: {
      users: 'name,birthday'
    },
    filter: {
      name: 'wopian'
    }
  }
})

Getting a collection of resources with their relationships

api.get('anime', {
  params: {
    include: 'categories'
  }
})

Getting a single resource by ID (method one)

api.get('anime/2', {
  params: {
    include: 'categories'
  }
})

Getting a single resource by ID (method two)

api.get('anime', {
  params: {
    include: 'categories',
    filter: { id: '2' }
 }
})

Getting a resource's relationship data only

api.get('anime/2/categories')

Getting a resource with nested JSON:API filters (not supported by kitsu.app's API)

// resource?filter[x][y]=value
api.get('resource', {
  params: {
    filter: {
      x: {
        y: 'value'
      }
    }
  }
})

Handling errors (async/await)

try {
  const { data } = await api.get('anime')
} catch (err) {
  // Array of JSON:API errors: http://jsonapi.org/format/#error-objects
  if (err.errors) err.errors.forEach(error => { ... })
  // Error type (Error, TypeError etc.)
  err.name
  // Error message
  err.message
  // Axios request parameters
  err.config
  // Axios response parameters
  err.response
}

Handling errors (Promises)

api.get('anime')
  .then(({ data }) => { ... })
  .catch(err => {
    // Array of JSON:API errors: http://jsonapi.org/format/#error-objects
    if (err.errors) err.errors.forEach(error => { ... })
    // Error type (Error, TypeError etc.)
    err.name
    // Error message
    err.message
    // Axios request parameters
    err.config
    // Axios response parameters
    err.response
  })

Returns Promise[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) JSON-parsed response

patch

packages/kitsu/src/index.js:289-323

Update a resource (alias update)

Parameters
Examples

Update a resource

api.update('posts', {
  id: '1',
  content: 'Goodbye World'
})

Update a resource with relationships

api.update('posts', {
  content: 'Hello World',
  uploads: {
    id: '167585',
    type: 'uploads'
  }
})

Clear to-one relationships from a resource

api.update('posts/1/relationships/uploads', null)

Clear to-many relationships from a resource

api.update('posts/1/relationships/uploads', [])

Update multiple resources (API must support the Bulk Extension)

api.update('posts', [
  { id: '1', content: 'Hello World' },
  { id: '2', content: 'Another post' }
])

Returns Promise<(Object | Array[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))> JSON-parsed response

post

packages/kitsu/src/index.js:358-390

Create a new resource (alias create)

Parameters
Examples

Create a post on a user's profile feed

api.create('posts', {
  content: 'Hello World',
  targetUser: {
    data: {
      id: '42603',
      type: 'users'
    }
  },
  user: {
    data: {
      id: '42603',
      type: 'users'
    }
  }
})

Create multiple resources (API must support the Bulk Extension)

api.create('posts', [
  { content: 'Hello World' },
  { content: 'Another post' }
])

Returns Promise<(Object | Array[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))> JSON-parsed response

delete

packages/kitsu/src/index.js:410-450

Remove a resource (alias remove)

Parameters
Examples

Remove one or more relationships from a resource

api.delete('posts/1/relationships/uploads', [456, 789])

Remove a single resource

api.delete('posts', 123)

Remove multiple resources (API must support the Bulk Extension)

api.delete('posts', [ 1, 2 ])

Returns Promise<(Object | Array[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))> JSON-parsed response

self

packages/kitsu/src/index.js:474-483

Get the authenticated user's data

Note Requires the JSON:API server to support filter[self]=true

Parameters
  • config Object? Additional configuration (optional, default {})

    • config.params Object? JSON:API request queries. See #get for documentation
    • config.headers Object? Additional headers to send with the request
    • config.axiosOptions Object? Additional options for the axios instance (see axios/axios#request-config for details)
Examples

Get the authenticated user's resource

api.self()

Using JSON:API parameters

api.self({
  params: {
    fields: {
      users: 'name,birthday'
    }
  }
})

Returns Promise[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) JSON-parsed response

request

packages/kitsu/src/index.js:538-566

Send arbitrary requests

Note Planned changes to the get, patch, post and delete methods in a future major release may make this method redundent. See https://github.com/wopian/kitsu/issues/415 for details.

Parameters
Examples

Raw GET request

api.request({
  url: 'anime/1/mappings',
  type: 'mappings',
  params: { filter: { externalSite: 'aozora' } }
})

Raw PATCH request

api.request({
  method: 'PATCH',
  url: 'anime',
  type: 'anime',
  body: { id: '1', subtype: 'tv' }
})

Raw POST request

api.request({
  method: 'PATCH',
  url: 'anime',
  type: 'anime',
  body: { subtype: 'tv' }
})

Raw DELETE request

api.request({
  method: 'DELETE',
  url: 'anime/1',
  type: 'anime',
  body: { id: '1' }
})

Bulk Extension support (PATCH, POST & DELETE)

api.request({
  method: 'PATCH',
  url: 'anime',
  type: 'anime',
  body: [
    { id: '1', subtype: 'tv' }
    { id: '2', subtype: 'ona' }
  ]
})

Returns Promise[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) JSON-parsed response

changelog

Change Log

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

11.0.1 (2025-04-09)

Note: Version bump only for package kitsu

11.0.0 (2025-04-09)

Bug Fixes

  • kitsu: correct jsdoc type definitions of return types (33f1bc2)

BREAKING CHANGES

  • kitsu: drop support for node 14 and 16. minimum supported node is now 18 (f193e1e)
  • kitsu: upgrade axios to v1 (#1075) (a973b00)

10.2.1 (2025-04-05)

Note: Version bump only for package kitsu

10.2.0 (2025-01-03)

Features

10.1.5 (2024-03-30)

Bug Fixes

  • kitsu: update dependency axios to v0.28.1 to address CVE-2023-45857 (#964) (ac23118)

10.1.4 (2024-03-14)

Bug Fixes

  • kitsu: export types in fine-grained exports (#968) (54b3657)

10.1.3 (2024-03-14)

Bug Fixes

  • kitsu: export types in fine-grained exports (#968) (54b3657)

10.1.2 (2023-06-20)

Bug Fixes

  • correct the typing for config on post (#873) (c741e0a)

10.1.1 (2023-04-26)

Note: Version bump only for package kitsu

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

10.0.3 (2022-10-30)

Note: Version bump only for package kitsu

10.0.2 (2022-08-17)

Note: Version bump only for package kitsu

10.0.1 (2022-08-12)

Note: Version bump only for package kitsu

10.0.0 (2022-08-11)

Note: Version bump only for package kitsu

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

Note: Version bump only for package kitsu

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

Note: Version bump only for package kitsu

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

Note: Version bump only for package kitsu

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

Note: Version bump only for package kitsu

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

Note: Version bump only for package kitsu

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

Note: Version bump only for package kitsu

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

Note: Version bump only for package kitsu

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

Features

  • kitsu: add axios options for all operations (#623) (2e4dc9e)

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

Note: Version bump only for package kitsu

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

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

Note: Version bump only for package kitsu

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

Features

  • return header object when present (b3e2624)

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

Bug Fixes

  • kitsu: apply resCase setting to ID-less resource relationships (a165983)

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

Note: Version bump only for package kitsu

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

Bug Fixes

  • re-add support for resource object-linkage for relationships (#552) (f66d077)

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

Chores

  • release: update documentation (92d4246)

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

Build System / Dependencies

  • update dependency axios to ^0.21.1 (b34d854)

Chores

  • release: update documentation (840d383)

Documentation Changes

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

Refactors

  • kitsu: change output directory to dist (ac249b7)

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

Build System / Dependencies

Chores

  • release: update documentation (fe39500)

Documentation Changes

  • kitsu: add missing commas to quick start (32ab682)
  • kitsu: update examples to 10.x (0154200)
  • kitsu: update quick start to 10.x (e1b988c)
  • fix markdown links again (44689d1)

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

Build System / Dependencies

Chores

  • release: update documentation (6d025ca)

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

Bug Fixes

  • external: pin axios to 0.19 to avoid breaking bug in 0.20 (aa56def)

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

  • kitsu: document the expected structure of the model parameters (173277e)

Tests

  • add test case for camelCase relationships (818f033)

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

Chores

  • release: update documentation (f89a7cf)

Documentation Changes

  • add typescript types badges (3a09066)

Tests

  • run tests in both node and jsdom environments (a383794)

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

Chores

  • release: update documentation (699bb64)

Documentation Changes

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

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

Chores

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

Documentation Changes

  • kitsu: fix typo in migration guide (af5ac6b)

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

Chores

  • release: update documentation (da50d92)

Documentation Changes

New Features

  • kitsu: delete - add param parameter (85a192b)
  • kitsu: patch - add params parameter (8d8eda3)
  • kitsu: post - add params parameter (13686be)

Refactors

  • kitsu: merge params and headers parameters into a single config object (f98eef9)
  • kitsu: request - move headers into config object (85937c4)
  • kitsu: self - move params and headers into a config object (c55949a)
  • kitsu: use merge-options instead of Object.assign (8b49cc1)
  • kitsu: use merge-options instead of Object.assign for class constructor options (9a162a0)

9.1.11 (2020-06-14)

Bug Fixes

  • kitsu: use ID-less URL for bulk extension in PATCH requests (2c31d42)

Chores

  • release: update documentation (ba00f68)

Other Changes

Tests

  • kitsu: re-add bulk test (3fcd884)

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)
  • remove replace-in-file rewriting module import (cbdc45e)

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)

Chores

  • release: update documentation (912f59d)

Other Changes

  • update sizelimit warning (11959bf)

9.1.4 (2020-05-21)

Chores

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

Documentation Changes

  • kitsu: document 1.0/1.1/Extensions pagination strategies (b54ede6)
  • kitsu: enumerate resourceCase string values (52c1c82)

9.1.3 (2020-05-21)

Chores

  • release: update documentation (de730f2)

Documentation Changes

  • kitsu: add clearing of to-one/to-many relationships to api.patch examples (113e60f)

9.1.2 (2020-05-21)

Chores

  • release: update documentation (20e7cc6)

Documentation Changes

  • kitsu: declare optional parameters in JSDoc syntax (95eb9c1)
  • 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)

Chores

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

Documentation Changes

  • correct errors in types (9ad8fc0)
  • kitsu: document usage of the bulk extension (e37b7ad)

New Features

  • kitsu: support arbitrary requests (e8aacc5)
  • kitsu: support the bulk extension specification (f793988), closes #336

9.0.7 (2020-05-19)

Chores

  • release: update documentation (523553e)

Documentation Changes

  • kitsu: link to axios docs for axiosOptions and interceptors (b678290)

Other Changes

9.0.6 (2020-05-07)

Chores

  • release: update documentation (03fc40e)

Documentation Changes

  • kitsu: add alias for delete to Quick Start in README (c9ba1b6)
  • kitsu: fix URL to more examples (d3d46ce)
  • kitsu: remove node 12 notice (6540e39)
  • kitsu: remove unused URL from README (bafbeeb)
  • kitsu: update Quick Start in README (f49064b)

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)

9.0.3 (2020-05-07)

Chores

  • release: update documentation (95c3fbb)

Documentation Changes

  • kitsu: add JSON:API query parameters to Quick Start (a5c501a)

9.0.2 (2020-05-07)

Chores

  • release: update documentation (84d00c5)

9.0.1 (2020-05-07)

Chores

  • release: update documentation (03ec026)

Documentation Changes

  • kitsu: add migration guide link to README (e434df2)

9.0.0 (2020-05-07)

Build System / Dependencies

Chores

  • release: update documentation (08362a8)

Documentation Changes

New Features

  • kitsu: handle nested (relationship) routes using kitsu-core's splitModel (4f641b9)

Refactors

  • kitsu: (internal) pass camel and plural options as arguments to serialise (046b51a)