@@ -72,6 +72,35 @@ export default class GoCommerce {
72
72
this . loadCart ( ) ;
73
73
}
74
74
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
+
75
104
setUser ( user ) {
76
105
this . user = user ;
77
106
}
@@ -238,9 +267,8 @@ export default class GoCommerce {
238
267
line_items . push ( this . line_items [ id ] ) ;
239
268
}
240
269
241
- return this . authHeaders ( ) . then ( ( headers ) => this . api . request ( "/orders" , {
270
+ return this . _authOptionalRequest ( "/orders" , {
242
271
method : "POST" ,
243
- headers : headers ,
244
272
body : JSON . stringify ( {
245
273
email,
246
274
shipping_address, shipping_address_id,
@@ -251,7 +279,7 @@ export default class GoCommerce {
251
279
data,
252
280
line_items
253
281
} )
254
- } ) ) . then ( ( order ) => {
282
+ } ) . then ( ( order ) => {
255
283
const cart = this . getCart ( ) ;
256
284
return { cart, order} ;
257
285
} ) ;
@@ -266,9 +294,8 @@ export default class GoCommerce {
266
294
const { order_id, amount, provider, stripe_token, paypal_payment_id, paypal_user_id} = paymentDetails ;
267
295
if ( order_id && amount && provider && ( stripe_token || ( paypal_payment_id && paypal_user_id ) ) ) {
268
296
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` , {
270
298
method : "POST" ,
271
- headers : headers ,
272
299
body : JSON . stringify ( {
273
300
amount,
274
301
order_id,
@@ -278,7 +305,7 @@ export default class GoCommerce {
278
305
paypal_user_id,
279
306
currency : this . currency
280
307
} )
281
- } ) ) ;
308
+ } ) ;
282
309
} else {
283
310
return Promise . reject (
284
311
"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 {
288
315
289
316
resendConfirmation ( orderID , email ) {
290
317
const path = `/orders/${ orderID } /receipt` ;
291
- return this . authHeaders ( ) . then ( ( headers ) => this . api . request ( path , {
292
- headers,
318
+ return this . _authOptionalRequest ( path , {
293
319
method : "POST" ,
294
320
body : JSON . stringify ( { email} )
295
- } ) ) ;
321
+ } ) ;
296
322
}
297
323
298
324
claimOrders ( ) {
299
325
if ( this . user ) {
300
- return this . authHeaders ( ) . then ( ( headers ) => this . api . request ( "/claim" , {
301
- headers,
326
+ return this . _authOptionalRequest ( "/claim" , {
302
327
method : "POST"
303
- } ) ) ;
328
+ } ) ;
304
329
}
305
330
return Promise . resolve ( null ) ;
306
331
}
307
332
308
333
updateOrder ( orderId , attributes ) {
309
- return this . authHeaders ( true ) . then ( ( headers ) => this . api . request ( `/orders/${ orderId } ` , {
310
- headers,
334
+ return this . _authRequiredRequest ( `/orders/${ orderId } ` , {
311
335
method : "PUT" ,
312
336
body : JSON . stringify ( attributes )
313
- } ) ) ;
337
+ } ) ;
314
338
}
315
339
316
340
orderHistory ( params ) {
@@ -320,33 +344,25 @@ export default class GoCommerce {
320
344
delete params . user_id ;
321
345
}
322
346
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} ) ) ;
326
348
}
327
349
328
350
orderDetails ( orderID ) {
329
- return this . authHeaders ( true ) . then ( ( headers ) => this . api . request ( `/orders/${ orderID } ` , {
330
- headers
331
- } ) ) ;
351
+ return this . _authRequiredRequest ( `/orders/${ orderID } ` ) ;
332
352
}
333
353
334
354
orderReceipt ( orderID , template ) {
335
355
let path = `/orders/${ orderID } /receipt` ;
336
356
if ( template ) {
337
357
path += `?template=${ template } ` ;
338
358
}
339
- return this . authHeaders ( true ) . then ( ( headers ) => this . api . request ( path , {
340
- headers
341
- } ) ) ;
359
+ return this . _authRequiredRequest ( path ) ;
342
360
}
343
361
344
362
userDetails ( userId ) {
345
363
userId = userId || ( this . user && this . user . id ) ;
346
364
347
- return this . authHeaders ( true ) . then ( ( headers ) => this . api . request ( `/users/${ userId } ` , {
348
- headers
349
- } ) ) ;
365
+ return this . _authRequiredRequest ( `/users/${ userId } ` ) ;
350
366
}
351
367
352
368
downloads ( params ) {
@@ -356,30 +372,22 @@ export default class GoCommerce {
356
372
delete params . order_id ;
357
373
}
358
374
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} ) ) ;
362
376
}
363
377
364
378
downloadURL ( downloadId ) {
365
379
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 ) ;
369
381
}
370
382
371
383
users ( params ) {
372
384
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} ) ) ;
376
386
}
377
387
378
388
report ( name , params ) {
379
389
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 ) ;
383
391
}
384
392
385
393
authHeaders ( required ) {
@@ -437,17 +445,15 @@ export default class GoCommerce {
437
445
return Promise . resolve ( false ) ;
438
446
}
439
447
440
- return this . api . request ( `/vatnumbers/${ vatnumber } ` ) . then ( ( response ) => {
448
+ return this . _request ( `/vatnumbers/${ vatnumber } ` ) . then ( ( response ) => {
441
449
vatnumbers [ vatnumber ] = response ;
442
450
this . vatnumber_valid = response . valid ;
443
451
return response . valid ;
444
452
} ) ;
445
453
}
446
454
447
455
verifyCoupon ( code ) {
448
- return this . authHeaders ( false ) . then ( ( headers ) => this . api . request ( `/coupons/${ code } ` , {
449
- headers
450
- } ) ) ;
456
+ return this . _authOptionalRequest ( `/coupons/${ code } ` ) ;
451
457
}
452
458
453
459
persistCart ( ) {
0 commit comments