Skip to content
This repository was archived by the owner on Sep 8, 2021. It is now read-only.

Commit 95e838d

Browse files
committed
Update micro-api-client and improve error handling
1 parent 6c41c8b commit 95e838d

File tree

3 files changed

+53
-47
lines changed

3 files changed

+53
-47
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
"uglify-js": "^2.6.2"
2929
},
3030
"dependencies": {
31-
"micro-api-client": "^2.0.0"
31+
"micro-api-client": "^3.0.2"
3232
}
3333
}

src/index.js

+49-43
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,35 @@ export default class GoCommerce {
7272
this.loadCart();
7373
}
7474

75+
_authOptionalRequest(path, options = {}) {
76+
return this.authHeaders(false)
77+
.then(headers => {
78+
return this._request(path, Object.assign({ headers }, options))
79+
});
80+
}
81+
82+
_authRequiredRequest(path, options = {}) {
83+
return this.authHeaders(true)
84+
.then(headers => {
85+
return this._request(path, Object.assign({ headers }, options))
86+
});
87+
}
88+
89+
_request(path, options = {}) {
90+
return this.api
91+
.request(path, options)
92+
.catch(err => {
93+
if (err instanceof JSONHTTPError && err.json) {
94+
if (err.json.msg) {
95+
err.message = err.json.msg;
96+
} else if (err.json.error) {
97+
err.message = `${err.json.error}: ${err.json.error_description}`;
98+
}
99+
}
100+
return Promise.reject(err);
101+
});
102+
}
103+
75104
setUser(user) {
76105
this.user = user;
77106
}
@@ -238,9 +267,8 @@ export default class GoCommerce {
238267
line_items.push(this.line_items[id]);
239268
}
240269

241-
return this.authHeaders().then((headers) => this.api.request("/orders", {
270+
return this._authOptionalRequest("/orders", {
242271
method: "POST",
243-
headers: headers,
244272
body: JSON.stringify({
245273
email,
246274
shipping_address, shipping_address_id,
@@ -251,7 +279,7 @@ export default class GoCommerce {
251279
data,
252280
line_items
253281
})
254-
})).then((order) => {
282+
}).then((order) => {
255283
const cart = this.getCart();
256284
return {cart, order};
257285
});
@@ -266,9 +294,8 @@ export default class GoCommerce {
266294
const {order_id, amount, provider, stripe_token, paypal_payment_id, paypal_user_id} = paymentDetails;
267295
if (order_id && amount && provider && (stripe_token || (paypal_payment_id && paypal_user_id))) {
268296
const cart = this.getCart();
269-
return this.authHeaders().then((headers) => this.api.request(`/orders/${order_id}/payments`, {
297+
return this._authOptionalRequest(`/orders/${order_id}/payments`, {
270298
method: "POST",
271-
headers: headers,
272299
body: JSON.stringify({
273300
amount,
274301
order_id,
@@ -278,7 +305,7 @@ export default class GoCommerce {
278305
paypal_user_id,
279306
currency: this.currency
280307
})
281-
}));
308+
});
282309
} else {
283310
return Promise.reject(
284311
"Invalid paymentDetails - must have an order_id, an amount, a provider, and a stripe_token or a paypal_payment_id and paypal_user_id"
@@ -288,29 +315,26 @@ export default class GoCommerce {
288315

289316
resendConfirmation(orderID, email) {
290317
const path = `/orders/${orderID}/receipt`;
291-
return this.authHeaders().then((headers) => this.api.request(path, {
292-
headers,
318+
return this._authOptionalRequest(path, {
293319
method: "POST",
294320
body: JSON.stringify({email})
295-
}));
321+
});
296322
}
297323

298324
claimOrders() {
299325
if (this.user) {
300-
return this.authHeaders().then((headers) => this.api.request("/claim", {
301-
headers,
326+
return this._authOptionalRequest("/claim", {
302327
method: "POST"
303-
}));
328+
});
304329
}
305330
return Promise.resolve(null);
306331
}
307332

308333
updateOrder(orderId, attributes) {
309-
return this.authHeaders(true).then((headers) => this.api.request(`/orders/${orderId}`, {
310-
headers,
334+
return this._authRequiredRequest(`/orders/${orderId}`, {
311335
method: "PUT",
312336
body: JSON.stringify(attributes)
313-
}));
337+
});
314338
}
315339

316340
orderHistory(params) {
@@ -320,33 +344,25 @@ export default class GoCommerce {
320344
delete params.user_id;
321345
}
322346
path = pathWithQuery(path, params);
323-
return this.authHeaders(true).then((headers) => this.api.request(path, {
324-
headers
325-
})).then(({items, pagination}) => ({orders: items, pagination}));
347+
return this._authRequiredRequest(path).then(({items, pagination}) => ({orders: items, pagination}));
326348
}
327349

328350
orderDetails(orderID) {
329-
return this.authHeaders(true).then((headers) => this.api.request(`/orders/${orderID}`, {
330-
headers
331-
}));
351+
return this._authRequiredRequest(`/orders/${orderID}`);
332352
}
333353

334354
orderReceipt(orderID, template) {
335355
let path = `/orders/${orderID}/receipt`;
336356
if (template) {
337357
path += `?template=${template}`;
338358
}
339-
return this.authHeaders(true).then((headers) => this.api.request(path, {
340-
headers
341-
}));
359+
return this._authRequiredRequest(path);
342360
}
343361

344362
userDetails(userId) {
345363
userId = userId || (this.user && this.user.id);
346364

347-
return this.authHeaders(true).then((headers) => this.api.request(`/users/${userId}`, {
348-
headers
349-
}));
365+
return this._authRequiredRequest(`/users/${userId}`);
350366
}
351367

352368
downloads(params) {
@@ -356,30 +372,22 @@ export default class GoCommerce {
356372
delete params.order_id;
357373
}
358374
path = pathWithQuery(path, params);
359-
return this.authHeaders().then((headers) => this.api.request(path, {
360-
headers
361-
})).then(({items, pagination}) => ({downloads: items, pagination}));
375+
return this._authOptionalRequest(path).then(({items, pagination}) => ({downloads: items, pagination}));
362376
}
363377

364378
downloadURL(downloadId) {
365379
const path = `/downloads/${downloadId}`;
366-
return this.authHeaders().then((headers) => this.api.request(path, {
367-
headers
368-
})).then((response) => response.url);
380+
return this._authOptionalRequest(path).then((response) => response.url);
369381
}
370382

371383
users(params) {
372384
const path = pathWithQuery("/users", params);
373-
return this.authHeaders(true).then((headers) => this.api.request(path, {
374-
headers
375-
})).then(({items, pagination}) => ({users: items, pagination}));
385+
return this._authRequiredRequest(path).then(({items, pagination}) => ({users: items, pagination}));
376386
}
377387

378388
report(name, params) {
379389
const path = pathWithQuery(`/reports/${name}`, params);
380-
return this.authHeaders(true).then((headers) => this.api.request(path, {
381-
headers
382-
}));
390+
return this._authRequiredRequest(path);
383391
}
384392

385393
authHeaders(required) {
@@ -437,17 +445,15 @@ export default class GoCommerce {
437445
return Promise.resolve(false);
438446
}
439447

440-
return this.api.request(`/vatnumbers/${vatnumber}`).then((response) => {
448+
return this._request(`/vatnumbers/${vatnumber}`).then((response) => {
441449
vatnumbers[vatnumber] = response;
442450
this.vatnumber_valid = response.valid;
443451
return response.valid;
444452
});
445453
}
446454

447455
verifyCoupon(code) {
448-
return this.authHeaders(false).then((headers) => this.api.request(`/coupons/${code}`, {
449-
headers
450-
}));
456+
return this._authOptionalRequest(`/coupons/${code}`);
451457
}
452458

453459
persistCart() {

yarn.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -1859,9 +1859,9 @@ merge@^1.1.3:
18591859
version "1.2.0"
18601860
resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.0.tgz#7531e39d4949c281a66b8c5a6e0265e8b05894da"
18611861

1862-
micro-api-client@^2.0.0:
1863-
version "2.0.0"
1864-
resolved "https://registry.yarnpkg.com/micro-api-client/-/micro-api-client-2.0.0.tgz#c078a8003e2b70668be9c5ff331131e3fd739150"
1862+
micro-api-client@^3.0.2:
1863+
version "3.0.2"
1864+
resolved "https://registry.yarnpkg.com/micro-api-client/-/micro-api-client-3.0.2.tgz#9680f2bdd4ebe6851ae90af0843fd3a3c8bfa992"
18651865

18661866
micromatch@^2.1.5, micromatch@^2.3.11:
18671867
version "2.3.11"

0 commit comments

Comments
 (0)