File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ( )
Original file line number Diff line number Diff 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 } ) ,
You can’t perform that action at this time.
0 commit comments