Skip to content

Commit f793988

Browse files
committed
feat(kitsu): support the bulk extension specification
Maps array of IDs for DELETE. POST and PATCH work out of the box with the changes in kitsu-core. Closes #336
1 parent 0d10ba3 commit f793988

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

packages/kitsu/src/delete.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ describe('kitsu', () => {
6464
done()
6565
})
6666

67+
it('deletes multiple resources (bulk extension)', async done => {
68+
expect.assertions(1)
69+
const api = new Kitsu({ headers: { Authorization: true } })
70+
mock.onDelete('/posts').reply(config => {
71+
expect(JSON.parse(config.data)).toEqual({
72+
data: [
73+
{ id: '1', type: 'posts' },
74+
{ id: '2', type: 'posts' }
75+
]
76+
})
77+
return [ 200 ]
78+
})
79+
api.delete('post', [ 1, 2 ]).catch(err => {
80+
done.fail(err)
81+
})
82+
done()
83+
})
84+
6785
it('throws an error if ID is missing', async () => {
6886
expect.assertions(1)
6987
const api = new Kitsu()

packages/kitsu/src/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ export default class Kitsu {
285285
*
286286
* @memberof Kitsu
287287
* @param {string} model Model to remove data from
288-
* @param {string|number} id Resource ID to remove
288+
* @param {string|number|Array} id Resource ID to remove. Pass an array of IDs to delete multiple resources (Bulk Extension)
289289
* @param {Object} headers Additional headers to send with request
290290
* @returns {Object} JSON-parsed response
291291
* @example <caption>Remove a user's post</caption>
@@ -297,8 +297,19 @@ export default class Kitsu {
297297
resourceCase: this.resCase,
298298
pluralModel: this.plural
299299
})
300-
const { data } = await this.axios.delete(`${url}/${id}`, {
301-
data: serialise(resourceModel, { id }, 'DELETE', {
300+
const isBulk = Array.isArray(id)
301+
let path, payload
302+
303+
if (isBulk) {
304+
path = url
305+
payload = id.map(id => ({ id }))
306+
} else {
307+
path = `${url}/${id}`
308+
payload = { id }
309+
}
310+
311+
const { data } = await this.axios.delete(path, {
312+
data: serialise(resourceModel, payload, 'DELETE', {
302313
camelCaseTypes: this.camel,
303314
pluralTypes: this.plural
304315
}),

0 commit comments

Comments
 (0)