Skip to content

Commit

Permalink
Merge pull request #35 from LIN3S/fix/add-item-handle-error
Browse files Browse the repository at this point in the history
[FIX] Catch error exception in addItem method
  • Loading branch information
0sserc authored May 29, 2019
2 parents 73c0a3d + d6ef3a9 commit 88de241
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This changelog references the relevant changes done between versions.
To get the diff for a specific change, go to https://github.com/LIN3S/SyliusShopApiClient/commit/XXX where XXX is the change hash
To get the diff between two versions, go to https://github.com/LIN3S/SyliusShopApiClient/compare/v0.2.0...v0.3.0

* 0.9.1
* [FIX] Catch error exception in `addItem` method
* 0.9.0
* [FEATURE] Added user `requestPasswordReset` and `passwordReset` endpoints
* 0.8.0
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lin3s-sylius-shop-api-client",
"version": "0.9.0",
"version": "0.9.1",
"author": "LIN3S",
"license": "MIT",
"description": "JavaScript client on top of SyliusShopApiPlugin to build integrations with ease.",
Expand All @@ -13,6 +13,13 @@
"client"
],
"main": "dist/common/index.js",
"repository": {
"type": "git",
"url": "https://github.com/LIN3S/SyliusShopApiClient"
},
"bugs": {
"url": "https://github.com/LIN3S/SyliusShopApiClient/issues"
},
"files": [
"dist",
"LICENSE",
Expand Down
12 changes: 7 additions & 5 deletions src/Cart/AddItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import session from "../session";
import createCart from './Pickup';
import {contentTypeJson} from '../requestConfig';

const addItem = (cartId, data, config, resolve) => {
const addItem = (cartId, data, config, {resolve, reject}) => {
const headers = {
...contentTypeJson(config),
};
Expand All @@ -13,17 +13,19 @@ const addItem = (cartId, data, config, resolve) => {
`${config.baseUrl}/shop-api/carts/${cartId}/items`,
JSON.stringify(data),
headers
).then(response => resolve(response.data));
)
.then(response => resolve(response.data))
.catch(error => reject(error));
};

export default config => ({productCode, variantCode, quantity, ...rest}) => {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
try {
const cartId = session(config).Cart.id();
addItem(cartId, {productCode, variantCode, quantity, ...rest}, config, resolve);
addItem(cartId, {productCode, variantCode, quantity, ...rest}, config, {resolve, reject});
} catch (exception) {
createCart(config)().then(cart => {
addItem(cart.tokenValue, {productCode, variantCode, quantity, ...rest}, config, resolve);
addItem(cart.tokenValue, {productCode, variantCode, quantity, ...rest}, config, {resolve, reject});
});
}
});
Expand Down

0 comments on commit 88de241

Please sign in to comment.