### Quick Start Guide for Kitsu API Methods
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Provides examples for initializing Kitsu with different base URLs and demonstrates common API operations including fetching resources (get/fetch), creating (post/create), updating (patch/update), deleting (delete/remove), and using various JSON:API parameters like include, fields, filter, sort, and pagination.
```javascript
// 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
}
}
})
```
--------------------------------
### New Kitsu Output Example
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Presents the output format from the Kitsu library after refactoring, using the same JSON:API response data as the legacy example. It shows that link objects are now preserved at the root and within relationships, and the relationship data structure is maintained.
```json
data: {
id: '1',
type: 'libraryEntries',
links: {
self: 'https://kitsu.app/api/edge/library-entries/1'
},
ratingTwenty: 10,
user: {
links: {
self: 'https://kitsu.app/api/edge/library-entries/1/relationships/user',
related: 'https://kitsu.app/api/edge/library-entries/1/user'
}
data: {
id: '2',
type: 'users',
links: {
self: 'https://kitsu.app/api/edge/users/2'
},
name: 'Example'
}
}
}
```
--------------------------------
### Legacy Kitsu Output Example
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Shows the output format produced by the Kitsu library when processing the legacy JSON:API response, demonstrating the omission of link objects and flattening of relationship data.
```json
data: {
id: '1',
type: 'libraryEntries',
ratingTwenty: 10,
user: {
id: '2',
type: 'users',
name: 'Example'
}
}
```
--------------------------------
### Legacy JSON:API Response Example
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Illustrates the structure of a JSON:API response before Kitsu's internal refactoring, showing how links are present at the root and within relationships.
```json
data: {
id: '1',
type: 'libraryEntries'
links: {
self: 'https://kitsu.app/api/edge/library-entries/1'
},
attributes: {
ratingTwenty: 10
},
relationships: {
user: {
links: {
self: 'https://kitsu.app/api/edge/library-entries/1/relationships/user',
related: 'https://kitsu.app/api/edge/library-entries/1/user'
},
data: {
id: '2',
type: 'users'
}
}
}
},
included: [
{
id: '2',
type: 'users',
links: {
self: 'https://kitsu.app/api/edge/users/2'
},
attributes: {
name: 'Example'
}
}
```
--------------------------------
### Example Usage of query Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `query` function from Kitsu Core.
```js
query(...)
```
--------------------------------
### Example JSON:API Server GET Response
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Illustrates the structure of a typical response object returned by a JSON:API compliant server for a GET request, including primary data and included related resources.
```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'
}
}
]
}
```
--------------------------------
### Legacy Serialise Function Syntax (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Provides an example of the old syntax for calling the `serialise` function, which used `Function.prototype.apply` to pass options like camel casing and pluralisation.
```JavaScript
import { serialise, camel, kebab } from 'kitsu-core'
import plural from 'pluralize'
serialise.apply({ camel, resCase: kebab, plural }, [ model, obj, 'PATCH' ])
```
--------------------------------
### Install Project Dependencies
Source: https://github.com/wopian/kitsu/blob/master/CONTRIBUTING.md
Commands to install project dependencies using either Yarn or npm.
```bash
yarn
# or
npm install
```
--------------------------------
### New Kitsu-core Serialisation Input Example - JSON/JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Provides an example of the data structure expected by the refactored kitsu-core `serialise` function in version 10.0.0. Relationships now require a nested `data` property and can include `links` and `meta` at the relationship level, aligning with JSON:API.
```json
{
id: '1',
type: 'libraryEntries',
links: { self: 'library-entries/1' }
user: { // one-to-one relationship
links: {
self: 'library-entries/1/relationships/user'
related: 'libary-entries/1/user'
},
data: {
id: '2',
type: 'users',
links: { self: 'users/2' }
name: 'Example'
}
},
unit: { // one-to-many relationship
links: {
self: 'library-entries/1/relationships/unit',
related: 'library-entries/1/unit'
},
data: [
{
id: '3',
type: 'episodes',
links: { self: 'episodes/3' },
number: 12
}
]
}
}
```
--------------------------------
### Accessing Relationship Resources with Kitsu
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Demonstrates how to perform standard CRUD operations (GET, PATCH, POST, DELETE) directly on relationship endpoints using the Kitsu library instance.
```javascript
import Kitsu from 'kitsu'
const api = new Kitsu()
api.get('posts/1/comments')
api.patch('post/1/comments', { id: '1', body: 'An updated comment'})
api.post('post/1/comments', { body: 'A new comment'})
api.delete('post/1/comments', 1)
```
--------------------------------
### Legacy Kitsu-core Serialisation Input Example - JSON/JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Provides an example of the data structure expected by the kitsu-core `serialise` function prior to version 10.0.0. Relationships were represented directly as nested objects or arrays within the main resource object.
```json
{
id: '1',
type: 'libraryEntries',
links: { self: 'library-entries/1' }
user: { // one-to-one relationship
id: '2',
type: 'users',
links: { self: 'users/2' }
name: 'Example'
},
unit: [ // one-to-many relationship
{
id: '3',
type: 'episodes',
links: { self: 'episodes/3' }
number: 12
}
]
}
```
--------------------------------
### Legacy Kitsu-core Serialisation Output Example - JSON/JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Shows the JSON:API output structure generated by the kitsu-core `serialise` function prior to version 10.0.0, based on the legacy input format. Note how relationship links were incorrectly placed within the attributes object.
```json
data: {
id: '1',
type: 'libraryEntries',
attributes: {
links: { self: 'library-entries/1' }
},
relationships: {
user: {
data: {
id: '2',
type: 'users',
attributes: {
links: { self: 'users/2' }
name: 'Example'
}
}
},
unit: {
data: [
{
id: '3',
type: 'episodes',
attributes: {
links: { self: 'episodes/3' }
number: 12
}
}
]
}
}
}
```
--------------------------------
### Example Usage of serialise Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `serialise` function from Kitsu Core.
```js
serialise(...)
```
--------------------------------
### Example Usage of splitModel Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `splitModel` function from Kitsu Core.
```js
splitModel(...)
```
--------------------------------
### Install Kitsu Core with Yarn or NPM - Bash
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Commands to install the `kitsu-core` package using either Yarn or NPM package managers.
```bash
yarn add kitsu-core
npm install kitsu-core
```
--------------------------------
### Installing Kitsu via Yarn or NPM
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Commands to add the Kitsu library to your project using either the Yarn or NPM package managers.
```bash
yarn add kitsu
npm install kitsu
```
--------------------------------
### New Kitsu-core Serialisation Output Example - JSON/JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Shows the JSON:API output structure generated by the refactored kitsu-core `serialise` function in version 10.0.0, based on the new input format. Relationship links and meta are now correctly placed within the relationship object, adhering to JSON:API.
```json
data: {
id: '1',
type: 'libraryEntries',
links: { self: 'library-entries/1' },
relationships: {
user: {
links: {
self: 'library-entries/1/relationships/user'
related: 'libary-entries/1/user'
},
data: {
id: '2',
type: 'users',
links: { self: 'users/2' },
attributes: {
name: 'Example'
}
}
},
unit: {
links: {
self: 'library-entries/1/relationships/unit',
related: 'library-entries/1/unit'
},
data: [
{
id: '3',
type: 'episodes',
links: { self: 'episodes/3' },
attributes: {
number: 12
}
}
]
}
}
}
```
--------------------------------
### Use Kitsu Core from CDN - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Example showing how to access and use components from the Kitsu Core library when it's included via a CDN, typically available under the global `kitsuCore` object.
```js
kitsuCore.camel(...)
```
--------------------------------
### Importing Kitsu - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Shows the legacy and new ways to import the Kitsu library after the compiled output directory changed from 'lib' to 'dist' in version 10.0.0.
```javascript
// Legacy behaviour, any of:
import Kitsu from 'kitsu'
import Kitsu from 'kitsu/lib/index'
// New behaviour, any of:
import Kitsu from 'kitsu'
import Kitsu from 'kitsu/index' // Node 12+
import Kitsu from 'kitsu/dist/index'
```
--------------------------------
### Example Usage of deserialise Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `deserialise` function from Kitsu Core.
```js
deserialise(...)
```
--------------------------------
### Example Usage of kebab Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `kebab` function from Kitsu Core, which converts strings to kebab-case.
```js
kebab(...)
```
--------------------------------
### Import/Require Kitsu Core Module - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Demonstrates how to import `kitsu-core` components using ES Modules (`import`) for environments like Babel or CommonJS (`require`) for environments like Browserify, followed by a basic usage example.
```js
import { camel } from 'kitsu-core' // ES Modules and Babel
const { camel } = require('kitsu-core') // CommonJS and Browserify
camel(...)
```
--------------------------------
### Legacy Data Output Structure (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Demonstrates the simplified output structure produced by legacy Kitsu Core versions, where related data is flattened into the main `data` object.
```JavaScript
data: {
id: '1',
type: 'libraryEntries',
ratingTwenty: 10,
user: {
id: '2',
type: 'users',
name: 'Example'
}
}
```
--------------------------------
### Example Usage of isDeepEqual Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `isDeepEqual` function from Kitsu Core.
```js
isDeepEqual(...)
```
--------------------------------
### Example Kitsu Processed GET Response Object
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Shows how Kitsu automatically processes a JSON:API response, linking included relationships directly into the primary data object for easier access.
```json5
{
data: {
id: '1',
type: 'articles',
title: 'JSON API paints my bikeshed',
author: {
data: {
id: '42',
type: 'people',
name: 'John'
}
}
}
}
```
--------------------------------
### New Data Output Structure (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Illustrates the output structure produced by newer Kitsu Core versions, showing how the original JSON:API structure with `links` and nested `data` is largely preserved.
```JavaScript
data: {
id: '1',
type: 'libraryEntries',
links: {
self: 'library-entries/1'
},
ratingTwenty: 10,
user: {
links: {
self: 'library-entries/1/relationships/user',
related: 'library-entries/1/user'
},
data: {
id: '2',
type: 'users',
links: {
self: 'users/2'
},
name: 'Example'
}
}
}
```
--------------------------------
### Example Usage of error Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `error` function from Kitsu Core.
```js
error(...)
```
--------------------------------
### Get Resource with Nested Filters (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Provides an example of attempting to use nested JSON:API filters, noting that this specific structure is not supported by the kitsu.app API.
```javascript
// resource?filter[x][y]=value
api.get('resource', {
params: {
filter: {
x: {
y: 'value'
}
}
}
})
```
--------------------------------
### Example Usage of camel Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `camel` function from Kitsu Core, which converts strings to camelCase.
```js
camel(...)
```
--------------------------------
### Getting Authenticated User Data with Params (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Example showing how to use the `api.self` method with JSON:API parameters, specifically requesting only the 'name' and 'birthday' fields for the user resource using the `fields` parameter.
```javascript
api.self({
params: {
fields: {
users: 'name,birthday'
}
}
})
```
--------------------------------
### Example Usage of deattribute Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `deattribute` function from Kitsu Core.
```js
deattribute(...)
```
--------------------------------
### Example Usage of linkRelationships Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `linkRelationships` function from Kitsu Core.
```js
linkRelationships(...)
```
--------------------------------
### Example Usage of snake Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `snake` function from Kitsu Core, which converts strings to snake_case.
```js
snake(...)
```
--------------------------------
### Getting Authenticated User Data with Kitsu API (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Example demonstrating the basic usage of the `api.self` method to retrieve the authenticated user's data. This method is a shortcut for fetching the current user and requires the JSON:API server to support `filter[self]=true`.
```javascript
api.self()
```
--------------------------------
### Converting to Snake Case (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Provides a simple example of how to use the `snake` function to convert a string from camelCase to snake_case.
```javascript
snake('helloWorld') // 'hello_world'
```
--------------------------------
### Using Nested Query Parameters with Kitsu
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Shows how to pass a nested JavaScript object as a query parameter to the Kitsu `get` method. The library automatically serializes this object into a nested query string format (e.g., `filter[myFilter][content][name]=filter_name`).
```javascript
// Requests /resources?filter[myFilter][content][name]=filter_name
api.get('resource', {
filter: {
myFilter: {
content: {
name: 'filter_name'
}
}
})
```
--------------------------------
### Example Usage of filterIncludes Function - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
A basic example demonstrating a call to the `filterIncludes` function from Kitsu Core.
```js
filterIncludes(...)
```
--------------------------------
### Get Resource with JSON:API Params (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Demonstrates how to fetch a resource using the `api.get` method with JSON:API parameters for filtering fields and applying filters.
```javascript
api.get('users', {
params: {
fields: {
users: 'name,birthday'
},
filter: {
name: 'wopian'
}
}
})
```
--------------------------------
### Generating Nested Query Parameters (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Shows how to use the `kitsuCore.query` function to generate URL query strings that include nested parameters, demonstrating the structure of the input object and the resulting encoded string.
```JavaScript
// Returns %2Fresources%3Ffilter%5BmyFilter%5D%5Bcontent%5D%5Bname%5D%3Dfilter_name
kitsuCore.query({
filter: {
myFilter: {
content: {
name: 'filter_name'
}
}
})
```
--------------------------------
### Perform Raw GET Request (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Demonstrates making a raw GET request using the `api.request` method to fetch mappings for a specific anime, filtered by external site.
```javascript
api.request({
url: 'anime/1/mappings',
type: 'mappings',
params: { filter: { externalSite: 'aozora' } }
})
```
--------------------------------
### Migrating Kitsu Legacy/Node Import Path
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Illustrates the required change in import paths for the legacy and node versions of the Kitsu library, removing the `/lib/` segment from the path.
```javascript
- import Kitsu from 'kitsu/lib/legacy'
+ import Kitsu from 'kitsu/legacy'
```
--------------------------------
### Initializing Kitsu Client with Custom Base URL in JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
This example demonstrates how to initialize a Kitsu client instance with a custom base URL, allowing it to connect to a different JSON:API server instead of the default kitsu.app endpoint.
```javascript
const api = new Kitsu({
baseURL: 'https://api.example.org/2'
})
```
--------------------------------
### Get Collection with Relationships (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Shows how to retrieve a collection of resources and include their related resources using the `include` JSON:API parameter.
```javascript
api.get('anime', {
params: {
include: 'categories'
}
})
```
--------------------------------
### Legacy PATCH/POST Input Body - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Demonstrates the structure of the 'body' parameter for PATCH and POST requests in Kitsu versions prior to 10.0.0, illustrating how relationships were included directly within the main object.
```javascript
{
id: '1',
type: 'libraryEntries',
links: { self: 'library-entries/1' }
user: { // one-to-one relationship
id: '2',
type: 'users',
links: { self: 'users/2' }
name: 'Example'
},
unit: [ // one-to-many relationship
{
id: '3',
type: 'episodes',
links: { self: 'episodes/3' }
number: 12
}
]
}
```
--------------------------------
### Semantic Commit Message Format
Source: https://github.com/wopian/kitsu/blob/master/CONTRIBUTING.md
Example of the required semantic commit message format used in the project, including the type and category.
```text
feat(category): added feature x
```
--------------------------------
### Legacy Data Input Structure (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Shows the structure of the input data object used in legacy Kitsu Core versions, including `data`, `links`, `attributes`, `relationships`, and `included` fields.
```JavaScript
data: {
id: '1',
type: 'libraryEntries',
links: {
self: 'library-entries/1'
},
attributes: {
ratingTwenty: 10
},
relationships: {
user: {
links: {
self: 'library-entries/1/relationships/user',
related: 'library-entries/1/user'
},
data: {
id: '2',
type: 'users'
}
}
}
},
included: [
{
id: '2',
type: 'users',
links: {
self: 'users/2'
},
attributes: {
name: 'Example'
}
}
]
```
--------------------------------
### Legacy PATCH/POST Output JSON - JSON:API
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Shows the JSON:API format generated by Kitsu versions prior to 10.0.0 for PATCH and POST requests, detailing how relationships were structured within the 'relationships' object.
```json
data: {
id: '1',
type: 'libraryEntries',
attributes: {
links: { self: 'library-entries/1' }
},
relationships: {
user: {
data: {
id: '2',
type: 'users',
attributes: {
links: { self: 'users/2' }
name: 'Example'
}
}
},
unit: {
data: [
{
id: '3',
type: 'episodes',
attributes: {
links: { self: 'episodes/3' }
number: 12
}
}
]
}
}
}
```
--------------------------------
### New PATCH/POST Input Body - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Demonstrates the updated structure for the 'body' parameter in Kitsu 10.0.0+ PATCH and POST requests, where relationships now require a nested object containing 'links' and 'data'.
```javascript
{
id: '1',
type: 'libraryEntries',
links: { self: 'library-entries/1' }
user: { // one-to-one relationship
links: {
self: 'library-entries/1/relationships/user'
related: 'libary-entries/1/user'
},
data: {
id: '2',
type: 'users',
links: { self: 'users/2' }
name: 'Example'
}
},
unit: { // one-to-many relationship
links: {
self: 'library-entries/1/relationships/unit',
related: 'library-entries/1/unit'
},
data: [
{
id: '3',
type: 'episodes',
links: { self: 'episodes/3' },
number: 12
}
]
}
}
```
--------------------------------
### New Serialise Function Syntax (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Presents the updated syntax for the `serialise` function, where camel case and pluralisation options are passed directly as properties within a dedicated options object argument.
```JavaScript
import { serialise, camel } from 'kitsu-core'
import plural from 'pluralize'
serialise(model, obj, 'PATCH', {
camelCaseTypes: camel,
pluralTypes: plural
})
```
--------------------------------
### Migrating Kitsu-core Utility Exports - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Shows how utility functions like camel, kebab, and snake are now consistently exported as named exports in kitsu-core version 10.0.0, changing from previous versions where they might have been default exports from specific paths.
```javascript
// Legacy behaviour, any of:
import { camel } from 'kitsu-core'
import camel from 'kitsu-core/lib/camel'
// New behaviour, any of:
import { camel } from 'kitsu-core'
import { camel } from 'kitsu-core/camel'
import { camel } from 'kitsu-core/dist/camel'
```
--------------------------------
### Get Single Resource by ID (Method 1) (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Illustrates fetching a single resource by appending its ID directly to the resource path, including related resources.
```javascript
api.get('anime/2', {
params: {
include: 'categories'
}
})
```
--------------------------------
### New PATCH/POST Output JSON - JSON:API
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Shows the JSON:API format generated by Kitsu 10.0.0+ for PATCH and POST requests, reflecting the new input structure where relationship objects can include top-level 'links'.
```json
data: {
id: '1',
type: 'libraryEntries',
links: { self: 'library-entries/1' },
relationships: {
user: {
links: {
self: 'library-entries/1/relationships/user'
related: 'libary-entries/1/user'
},
data: {
id: '2',
type: 'users',
links: { self: 'users/2' },
attributes: {
name: 'Example'
}
}
},
unit: {
links: {
self: 'library-entries/1/relationships/unit',
related: 'library-entries/1/unit'
},
data: [
{
id: '3',
type: 'episodes',
links: { self: 'episodes/3' },
attributes: {
number: 12
}
}
]
}
}
}
```
--------------------------------
### Get Resource Relationship Data Only (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Demonstrates how to fetch only the relationship data for a specific resource by appending the relationship name to the resource path.
```javascript
api.get('anime/2/categories')
```
--------------------------------
### Migrating Kitsu-core Exports - JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/MIGRATING.md
Illustrates the change in import paths for the main kitsu-core library and its named exports when migrating to version 10.0.0. The compiled output path changed from 'lib' to 'dist', affecting direct imports.
```javascript
// Legacy behaviour, any of:
import kitsu from 'kitsu-core'
import { serialise } from 'kitsu-core'
import { serialise } from 'kitsu-core/lib/serialise'
// New behaviour, any of:
import kitsu from 'kitsu-core'
import { serialise } from 'kitsu-core'
import { serialise } from 'kitsu-core/serialise' // Node 12+
import { serialise } from 'kitsu-core/dist/serialise'
```
--------------------------------
### Handling Network Errors with Axios in Kitsu JS (v4.5+)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Demonstrates how to access Axios-specific network error details (config, response) from the caught error object when using Kitsu version 4.5 or later.
```js
catch (err) {
err.name // 'Error'
err.message // 'Request failed with status code 404'
err.config // Object containing everything used to send the request
err.response // Same as config, but for response
}
```
--------------------------------
### Get Single Resource by ID (Method 2) (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Shows an alternative method to fetch a single resource by using the `filter` JSON:API parameter with the resource ID.
```javascript
api.get('anime', {
params: {
include: 'categories',
filter: { id: '2' }
}
})
```
--------------------------------
### Update Single Resource (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Example of updating a single resource using the `api.update` (or `api.patch`) method by providing the resource type and an object containing the ID and updated attributes.
```javascript
api.update('posts', {
id: '1',
content: 'Goodbye World'
})
```
--------------------------------
### Update Multiple Resources (Bulk) (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Example of updating multiple resources in a single request by providing an array of resource objects to `api.update`, assuming the API supports the JSON:API Bulk Extension.
```javascript
api.update('posts', [
{ id: '1', content: 'Hello World' },
{ id: '2', content: 'Another post' }
])
```
--------------------------------
### Handling JSON:API Errors in Kitsu JS (v4.5+)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Shows how JSON:API errors are exposed directly on the caught error object (`err.errors`) in Kitsu version 4.5 and above, allowing for easier destructuring. Axios details are also available.
```js
catch (err) {
err.name // 'Error'
err.message // 'Request failed with status code 404'
err.config // Object containing everything used to send the request
err.response // Same as config, but for response
err.errors // Array of JSON:API errors: { title, detail, code, status }
}
```
--------------------------------
### Adding Pluralization Rules in Kitsu Client (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
This example demonstrates how to interact with the `plural` property of the Kitsu instance to add custom uncountable rules, modifying how the library handles pluralization for specific words when pluralization is enabled.
```javascript
api.plural.plural('paper') //=> 'papers'
api.plural.addUncountableRule('paper')
api.plural.plural('paper') //=> 'paper'
```
--------------------------------
### Deserialising Basic JSON:API Response with Kitsu Core
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Provides an example of using the `deserialise` function to process a simple JSON:API response object containing only `data` and `meta` fields. The attributes within the data object are hoisted.
```javascript
deserialise({
data: {
id: '1',
attributes: { liked: true }
},
meta: { hello: 'world' }
}) // { data: { id: '1', liked: true }, meta: { hello: 'world' } }
```
--------------------------------
### Removing Multiple Resources with Kitsu API (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Example showing how to remove multiple resources of the same type using the `api.delete` method with an array of IDs. This functionality requires the JSON:API server to support the Bulk Extension.
```javascript
api.delete('posts', [ 1, 2 ])
```
--------------------------------
### Setting Request Headers in Kitsu JS (v2.x+)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Explains the method for setting custom request headers in Kitsu version 2.0.0 and later by directly modifying the `kitsu.headers` object, replacing the removed `kitsu.header()` method.
```js
kitsu.headers['key'] = 'value'
```
--------------------------------
### Removing Single Resource with Kitsu API (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Example demonstrating how to remove a single resource by its type and ID using the `api.delete` method. This sends a DELETE request to the specified resource endpoint.
```javascript
api.delete('posts', 123)
```
--------------------------------
### Handling Kitsu Internal Errors in JS
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Describes the structure of errors thrown directly by the Kitsu library itself, which contain a name and message but no additional details like config or response. This behavior is consistent across versions from 3.x onwards.
```js
catch (err) {
err.name // 'Error'
err.message // 'POST requires an object or array body'
}
```
--------------------------------
### Deleting Resource by ID in Kitsu JS (v2.x+)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Shows the updated syntax for deleting a resource in Kitsu version 2.0.0 and later, where the resource ID is passed directly as an integer argument instead of within an object.
```js
kitsu.remove('model', 1)
```
--------------------------------
### Handling JSON:API Errors in Kitsu JS (v3.x - v4.4)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/MIGRATING.md
Illustrates the previous method of handling JSON:API errors in Kitsu versions before 4.5, where errors were returned as part of the response object alongside the data, rather than being thrown as an error.
```js
const { data, errors } = await api.get(...)
errors // Undefined or an array of JSON:API errors
```
--------------------------------
### Removing Relationships with Kitsu API (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Example showing how to remove one or more relationships from a specific resource using the `api.delete` method. It targets the relationships endpoint of a post resource by its ID and provides an array of relationship IDs to remove.
```javascript
api.delete('posts/1/relationships/uploads', [456, 789])
```
--------------------------------
### Using Axios Interceptors (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Shows how to add request and response interceptors to the API client instance, leveraging Axios's interceptor functionality. Interceptors can modify requests before they are sent or responses before they are processed, and can also handle errors. Includes an example of removing an interceptor.
```javascript
// 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)
})
```
```javascript
// 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)
})
```
```javascript
const myInterceptor = api.interceptors.request.use(function () {...})
api.interceptors.request.eject(myInterceptor)
```
--------------------------------
### Initializing Kitsu Client in JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
This snippet shows the basic initialization of a Kitsu client instance using the default options, connecting to the default API endpoint (kitsu.app).
```javascript
const api = new Kitsu()
```
--------------------------------
### Perform Raw PATCH Request (No ID in Body) (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Illustrates a raw PATCH request using `api.request` with a body containing only the subtype. This example may be intended for creation or a specific update scenario without an explicit ID in the body object.
```javascript
api.request({
method: 'PATCH',
url: 'anime',
type: 'anime',
body: { subtype: 'tv' }
})
```
--------------------------------
### Run Project Tests
Source: https://github.com/wopian/kitsu/blob/master/CONTRIBUTING.md
Commands to execute the project's test suite using either Yarn or npm.
```bash
yarn test
# or
npm test
```
--------------------------------
### Clone Kitsu Repository
Source: https://github.com/wopian/kitsu/blob/master/CONTRIBUTING.md
Commands to clone the forked Kitsu repository from GitHub and navigate into the project directory.
```bash
git clone https://github.com/your-username/kitsu.git
cd kitsu
```
--------------------------------
### Creating Resources with Kitsu (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Demonstrates how to use the `api.create` method (an alias for `post`) to add new resources to the API. This method supports creating single resources or multiple resources simultaneously if the API supports the JSON:API Bulk Extension. Parameters include the resource model, the data body, and optional configuration.
```javascript
api.create('posts', {
content: 'Hello World',
targetUser: {
data: {
id: '42603',
type: 'users'
}
},
user: {
data: {
id: '42603',
type: 'users'
}
}
})
```
```javascript
api.create('posts', [
{ content: 'Hello World' },
{ content: 'Another post' }
])
```
--------------------------------
### Include Kitsu Core via CDN - HTML
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
HTML script tags to include the Kitsu Core library directly from jsDelivr or unpkg CDNs in a browser environment.
```html
```
--------------------------------
### Create Feature Branch
Source: https://github.com/wopian/kitsu/blob/master/CONTRIBUTING.md
Command to create a new local branch for your feature and switch to it.
```bash
git checkout -b your-feature-name
```
--------------------------------
### Using splitModel with resourceCase Option in JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Illustrates the use of the `splitModel` function with the `resourceCase` option, demonstrating how to apply different casing transformations (kebab-case and snake_case) to the resource URL using functions from 'kitsu-core'.
```javascript
import { kebab, snake } from 'kitsu-core'
splitModel('libraryEntries', { resourceCase: kebab })
// [ 'libraryEntries', 'library-entries' ]
splitModel('libraryEntries', { resourceCase: snake })
// [ 'libraryEntries', 'library_entries' ]
```
--------------------------------
### Initializing Kitsu Client with Custom Headers in JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
This snippet illustrates how to initialize a Kitsu client instance by providing custom headers, such as a User-Agent or Authorization token, which will be included in all subsequent requests made by this instance.
```javascript
const api = new Kitsu({
headers: {
'User-Agent': 'MyApp/1.0.0 (github.com/username/repo)',
Authorization: 'Bearer 1234567890'
}
})
```
--------------------------------
### Basic Usage of splitModel in JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Demonstrates the default behavior of the `splitModel` function when given a path string. It splits the path and returns an array containing the model name and the full resource URL.
```javascript
splitModel('posts/1/comments')
// [ 'comments', 'posts/1/comments' ]
```
--------------------------------
### Push Changes to Remote
Source: https://github.com/wopian/kitsu/blob/master/CONTRIBUTING.md
Command to push the local feature branch to your remote fork on GitHub.
```bash
git push origin your-feature-name
```
--------------------------------
### Importing Kitsu in JavaScript/TypeScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Demonstrates how to import the Kitsu library using both ES Modules syntax (for modern JavaScript/TypeScript with bundlers like Webpack/Rollup or Babel) and CommonJS syntax (for Node.js or older environments).
```js
import Kitsu from "kitsu"; // ES Modules & Babel
const Kitsu = require("kitsu"); // CommonJS & Browserify
```
--------------------------------
### Handle Errors (Promises) (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Demonstrates how to catch and handle errors when using the `kitsu` library with Promise syntax, accessing JSON:API errors and Axios response details.
```javascript
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
})
```
--------------------------------
### Serialising Data with Options (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Demonstrates using the `serialise` function to convert a JavaScript object into JSON-API format, including options for custom camelCase and pluralisation handling using imported helper functions.
```javascript
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 })
```
--------------------------------
### Handle Errors (Async/Await) (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Shows how to catch and handle errors when using the `kitsu` library with async/await syntax, accessing JSON:API errors and Axios response details.
```javascript
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
}
```
--------------------------------
### Converting Kebab-Case to Camel Case with Kitsu Core
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Demonstrates using the `camel` function to convert a string from kebab-case format to camelCase format.
```javascript
camel('hello-world') // 'helloWorld'
```
--------------------------------
### Handling Simple String Error with Kitsu Core
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Demonstrates calling the `error` function with a simple string message. This function is used for uniform error handling within the Kitsu library.
```javascript
error('Hello')
```
--------------------------------
### Using splitModel with pluralModel Option in JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Shows how to use the `splitModel` function with the `pluralModel` option, providing a custom pluralization function (from the 'pluralize' library) to handle singular model names correctly.
```javascript
import plural from 'pluralize'
splitModel('posts/1/comment', { pluralModel: plural })
// [ 'comment', 'posts/1/comments' ]
```
--------------------------------
### Converting Snake-Case to Camel Case with Kitsu Core
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Demonstrates using the `camel` function to convert a string from snake_case format to camelCase format.
```javascript
camel('hello_world') // 'helloWorld'
```
--------------------------------
### Constructing JSON:API Query String in JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Constructs a URL query string from a JavaScript object containing JSON:API parameters. Supports nested parameters and offers traditional or modern serialization. Takes an optional parameters object, an internal prefix, and a boolean for serialization style. Returns the formatted query string.
```javascript
query({
filter: {
slug: 'cowboy-bebop',
title: {
value: 'foo'
}
},
sort: '-id'
})
// filter%5Bslug%5D=cowboy-bebop&filter%5Btitle%5D%5Bvalue%5D=foo&sort=-id
```
--------------------------------
### Update Resource with Relationships (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Shows how to update a resource and include relationship data in the update payload using the `api.update` method.
```javascript
api.update('posts', {
content: 'Hello World',
uploads: {
id: '167585',
type: 'uploads'
}
})
```
--------------------------------
### Basic Data Serialisation (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Shows the basic usage of the `serialise` function to convert a JavaScript object into JSON-API format without specifying custom case conversion or pluralisation options.
```javascript
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')
```
--------------------------------
### Deserialising JSON:API Response with Relationships and Included Data
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Shows how the `deserialise` function handles a JSON:API response that includes relationships and an `included` section. It demonstrates how related resources from `included` are linked and merged into the main data object.
```javascript
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' } } } }
```
--------------------------------
### Linking JSON:API Relationships in JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Links related resources from the `included` array to the main `data` object based on defined relationships. Takes the primary data object, an optional array of included resources, and internal cache objects. Returns the data object with relationships resolved.
```javascript
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' }
// }
// }
```
--------------------------------
### Perform Bulk PATCH Request (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Demonstrates performing a bulk PATCH operation using `api.request`. The body is an array allowing multiple anime entries to be updated in a single request.
```javascript
api.request({
method: 'PATCH',
url: 'anime',
type: 'anime',
body: [
{ id: '1', subtype: 'tv' }
{ id: '2', subtype: 'ona' }
]
})
```
--------------------------------
### Accessing and Modifying Headers (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Demonstrates how to retrieve, access, and update the headers associated with the Kitsu API client instance. This allows inspection of current headers or adding/changing values like authorization tokens.
```javascript
api.headers
```
```javascript
api.headers['User-Agent']
```
```javascript
api.headers['Authorization'] = 'Bearer 1234567890'
```
--------------------------------
### Commit Changes
Source: https://github.com/wopian/kitsu/blob/master/CONTRIBUTING.md
Command to commit staged changes with a semantic commit message. The -am flag stages all modified and deleted files.
```bash
git commit -am 'feat: add feature name'
```
--------------------------------
### Perform Raw PATCH Request (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Shows how to make a raw PATCH request using `api.request` to update the subtype of an anime identified by its ID.
```javascript
api.request({
method: 'PATCH',
url: 'anime',
type: 'anime',
body: { id: '1', subtype: 'tv' }
})
```
--------------------------------
### Clear To-One Relationship (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Demonstrates how to clear a to-one relationship for a resource by sending `null` to the relationship endpoint using `api.update`.
```javascript
api.update('posts/1/relationships/uploads', null)
```
--------------------------------
### Perform Raw DELETE Request (JavaScript)
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu/README.md
Shows how to make a raw DELETE request using `api.request` to remove an anime identified by its ID.
```javascript
api.request({
method: 'DELETE',
url: 'anime/1',
type: 'anime',
body: { id: '1' }
})
```
--------------------------------
### Converting Camel Case to Kebab Case in JavaScript
Source: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/README.md
Converts a camelCase string into its kebab-case equivalent. Takes a single string input and returns the transformed string.
```javascript
kebab('helloWorld') // 'hello-world'
```