Skip to content

Commit 4f641b9

Browse files
committed
feat(kitsu): handle nested (relationship) routes using kitsu-core's splitModel
1 parent 782d1b6 commit 4f641b9

4 files changed

Lines changed: 64 additions & 9 deletions

File tree

packages/kitsu/src/delete.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ describe('kitsu', () => {
4646
done()
4747
})
4848

49+
it('handles nested routes', async done => {
50+
expect.assertions(1)
51+
const api = new Kitsu({ headers: { Authorization: true } })
52+
mock.onDelete('/posts/1/comments/1').reply(config => {
53+
expect(JSON.parse(config.data)).toEqual({
54+
data: {
55+
id: '1',
56+
type: 'comments'
57+
}
58+
})
59+
return [ 200 ]
60+
})
61+
api.delete('posts/1/comments', 1).catch(err => {
62+
done.fail(err)
63+
})
64+
done()
65+
})
66+
4967
it('throws an error if ID is missing', async () => {
5068
expect.assertions(1)
5169
const api = new Kitsu()

packages/kitsu/src/index.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios'
22
import pluralise from 'pluralize'
3-
import { camel, deserialise, error, kebab, query, serialise, snake } from 'kitsu-core'
3+
import { camel, deserialise, error, kebab, query, serialise, snake, splitModel } from 'kitsu-core'
44

55
/**
66
* Creates a new `kitsu` instance
@@ -218,13 +218,16 @@ export default class Kitsu {
218218
*/
219219
async patch (model, body, headers = {}) {
220220
try {
221-
const serialData = serialise(model, body, 'PATCH', {
221+
const [ resourceModel, url ] = splitModel(model, {
222+
resourceCase: this.resCase,
223+
pluralModel: this.plural
224+
})
225+
const serialData = serialise(resourceModel, body, 'PATCH', {
222226
camelCaseTypes: this.camel,
223227
pluralTypes: this.plural
224228
})
225-
const url = this.plural(this.resCase(model)) + '/' + body.id
226229
const { data } = await this.axios.patch(
227-
url,
230+
`${url}/${body.id}`,
228231
serialData,
229232
{ headers: Object.assign(this.headers, headers) }
230233
)
@@ -258,10 +261,13 @@ export default class Kitsu {
258261
*/
259262
async post (model, body, headers = {}) {
260263
try {
261-
const url = this.plural(this.resCase(model))
264+
const [ resourceModel, url ] = splitModel(model, {
265+
resourceCase: this.resCase,
266+
pluralModel: this.plural
267+
})
262268
const { data } = await this.axios.post(
263269
url,
264-
serialise(model, body, 'POST', {
270+
serialise(resourceModel, body, 'POST', {
265271
camelCaseTypes: this.camel,
266272
pluralTypes: this.plural
267273
}),
@@ -287,9 +293,12 @@ export default class Kitsu {
287293
*/
288294
async delete (model, id, headers = {}) {
289295
try {
290-
const url = this.plural(this.resCase(model)) + '/' + id
291-
const { data } = await this.axios.delete(url, {
292-
data: serialise(model, { id }, 'DELETE', {
296+
const [ resourceModel, url ] = splitModel(model, {
297+
resourceCase: this.resCase,
298+
pluralModel: this.plural
299+
})
300+
const { data } = await this.axios.delete(`${url}/${id}`, {
301+
data: serialise(resourceModel, { id }, 'DELETE', {
293302
camelCaseTypes: this.camel,
294303
pluralTypes: this.plural
295304
}),

packages/kitsu/src/patch.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,13 @@ describe('kitsu', () => {
8080
const request = await api.patch('posts', patchSingle.kitsu)
8181
expect(request).toEqual({ data: patchSingle.kitsu })
8282
})
83+
84+
it('handes nested routes', async () => {
85+
expect.assertions(1)
86+
const api = new Kitsu({ headers: { Authorization: true } })
87+
mock.onPatch(`something/1/relationships/posts/${patchSingle.jsonapi.data.id}`).reply(200, patchSingle.jsonapi)
88+
const request = await api.patch('something/1/relationships/posts', patchSingle.kitsu)
89+
expect(request).toEqual({ data: patchSingle.kitsu })
90+
})
8391
})
8492
})

packages/kitsu/src/post.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ describe('kitsu', () => {
4848
done()
4949
})
5050

51+
it('handles nested routes', async done => {
52+
expect.assertions(1)
53+
const api = new Kitsu({ headers: { Authorization: true } })
54+
mock.onPost('/something/1/relationships/anime').reply(config => {
55+
expect(JSON.parse(config.data)).toEqual({
56+
data: {
57+
type: 'anime',
58+
attributes: {
59+
name: 'Name'
60+
}
61+
}
62+
})
63+
return [ 200 ]
64+
})
65+
api.post('something/1/relationships/anime', { type: 'anime', name: 'Name' }).catch(err => {
66+
done.fail(err)
67+
})
68+
done()
69+
})
70+
5171
it('sends data in request with client-generated ID', async done => {
5272
expect.assertions(1)
5373
const api = new Kitsu()

0 commit comments

Comments
 (0)