Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
fix: Handle 204 empty API body on DELETE (#107)
Browse files Browse the repository at this point in the history
* fix: Handle empty 204 response from API

response.json() throws an exception if there's no JSON in API response to parse. Check the status of
the response, and if a 204 return an empty object in body

Fixes #105

* test: Update DELETE tests to expect 204 response

* style: Fixing typo
  • Loading branch information
ynnoj authored Sep 5, 2017
1 parent 9df506a commit 6ebcb99
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/factories/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RequestFactory {
}

if (!config.host) {
throw new Error('You have not specificed an API host');
throw new Error('You have not specified an API host');
}

const body = {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export function cartIdentifier() {
}

export function parseJSON(response) {
if (response.status === 204) {
return new Promise((resolve) => {
resolve({
status: response.status,
ok: response.ok,
json: '{}',
});
});
}

return new Promise(resolve => response.json()
.then(json => resolve({
status: response.status,
Expand Down
12 changes: 6 additions & 6 deletions test/unit/brands.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ describe('Moltin brands', () => {
},
})
.delete('/brands/brand-1')
.reply(200, brands[0]);
.reply(204);

return Moltin.Brands.Delete(brands[0].id)
.then((response) => {
assert.propertyVal(response, 'id', 'brand-1');
assert.equal(response, '{}');
});
});

Expand Down Expand Up @@ -180,11 +180,11 @@ describe('Moltin brands', () => {
id: 'brand-1',
}],
})
.reply(200, brands[0]);
.reply(204);

return Moltin.Products.DeleteRelationships(products[0].id, 'brand', brands[0].id)
.then((response) => {
assert.propertyVal(response, 'id', 'brand-1');
assert.equal(response, '{}');
});
});

Expand All @@ -205,11 +205,11 @@ describe('Moltin brands', () => {
id: 'brand-2',
}],
})
.reply(200, brands[0]);
.reply(204);

return Moltin.Products.DeleteRelationships(products[0].id, 'brand', [brands[0].id, brands[1].id])
.then((response) => {
assert.propertyVal(response, 'id', 'brand-1');
assert.equal(response, '{}');
});
});

Expand Down
19 changes: 8 additions & 11 deletions test/unit/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,11 @@ describe('Moltin categories', () => {
},
})
.delete('/categories/1')
.reply(200, {
type: 'category',
id: '1',
});
.reply(204);

return Moltin.Categories.Delete('1')
.then((response) => {
assert.propertyVal(response, 'id', '1');
assert.equal(response, '{}');
});
});

Expand Down Expand Up @@ -183,11 +180,11 @@ describe('Moltin categories', () => {
id: 'category-1',
}],
})
.reply(200, categories[0]);
.reply(204);

return Moltin.Products.DeleteRelationships(products[0].id, 'category', categories[0].id)
.then((response) => {
assert.propertyVal(response, 'id', 'category-1');
assert.equal(response, '{}');
});
});

Expand All @@ -208,11 +205,11 @@ describe('Moltin categories', () => {
id: 'category-2',
}],
})
.reply(200, categories[0]);
.reply(204);

return Moltin.Products.DeleteRelationships(products[0].id, 'category', [categories[0].id, categories[1].id])
.then((response) => {
assert.propertyVal(response, 'id', 'category-1');
assert.equal(response, '{}');
});
});

Expand All @@ -230,11 +227,11 @@ describe('Moltin categories', () => {
id: 'category-1',
}],
})
.reply(200, categories[0]);
.reply(204);

return Moltin.Products.UpdateRelationships(products[0].id, 'category', categories[0].id)
.then((response) => {
assert.propertyVal(response, 'id', 'category-1');
assert.equal(response, '{}');
});
});

Expand Down
15 changes: 6 additions & 9 deletions test/unit/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,11 @@ describe('Moltin collections', () => {
},
})
.delete('/collections/1')
.reply(200, {
type: 'collection',
id: '1',
});
.reply(204);

return Moltin.Collections.Delete('1')
.then((response) => {
assert.propertyVal(response, 'id', '1');
assert.equal(response, '{}');
});
});

Expand Down Expand Up @@ -179,11 +176,11 @@ describe('Moltin collections', () => {
id: 'collection-1',
}],
})
.reply(200, collections[0]);
.reply(204);

return Moltin.Products.DeleteRelationships(products[0].id, 'collection', collections[0].id)
.then((response) => {
assert.propertyVal(response, 'id', 'collection-1');
assert.equal(response, '{}');
});
});

Expand All @@ -204,11 +201,11 @@ describe('Moltin collections', () => {
id: 'collection-2',
}],
})
.reply(200, collections[0]);
.reply(204);

return Moltin.Products.DeleteRelationships(products[0].id, 'collection', [collections[0].id, collections[1].id])
.then((response) => {
assert.propertyVal(response, 'id', 'collection-1');
assert.equal(response, '{}');
});
});

Expand Down
7 changes: 2 additions & 5 deletions test/unit/currencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,11 @@ describe('Moltin currencies', () => {
},
})
.delete('/currencies/1')
.reply(200, {
type: 'currency',
id: '1',
});
.reply(204);

return Moltin.Currencies.Delete('1')
.then((response) => {
assert.propertyVal(response, 'id', '1');
assert.equal(response, '{}');
});
});

Expand Down
7 changes: 2 additions & 5 deletions test/unit/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,11 @@ describe('Moltin products', () => {
},
})
.delete('/products/1')
.reply(200, {
type: 'product',
id: '1',
});
.reply(204);

return Moltin.Products.Delete(1)
.then((response) => {
assert.propertyVal(response, 'id', '1');
assert.equal(response, '{}');
});
});
});

0 comments on commit 6ebcb99

Please sign in to comment.