@@ -47,21 +47,21 @@ const checkRedirect = (response) => {
47
47
return response ;
48
48
} ;
49
49
50
- function fetchEndpoint ( endpoint ) {
51
- return fetch ( endpoint . url , {
52
- method : endpoint . method ,
53
- mode : endpoint . mode ,
54
- redirect : endpoint . redirect ,
55
- headers : endpoint . headers ,
56
- body : endpoint . body
50
+ function fetchEndpoint ( request ) {
51
+ return fetch ( request . url , {
52
+ method : request . method ,
53
+ mode : request . mode ,
54
+ redirect : request . redirect ,
55
+ headers : request . headers ,
56
+ body : request . body
57
57
} )
58
58
. then ( checkStatus )
59
59
. then ( checkRedirect )
60
60
. then ( response => {
61
61
return response . json ( ) ;
62
62
} )
63
63
. then ( response => {
64
- return endpoint . transform ( response ) ;
64
+ return request . transform ( Object . assign ( { } , response ) ) ;
65
65
} ) . catch ( error => {
66
66
console . error ( error ) ; /* eslint no-console: 0 */
67
67
throw error ;
@@ -78,9 +78,32 @@ class AtlasSDKClient {
78
78
* Creates an AtlasSDK instance from config provided by config-api
79
79
* @constructor {ignore }
80
80
* @param {Object } config - config from config-api
81
+ * @param {Object } options
82
+ * @param {Object } options.mocksAPI - a service that intercepts fetch calls and provides it's own response
81
83
*/
82
- constructor ( config ) {
84
+ constructor ( config , options = { } ) {
83
85
this . config = config ;
86
+ this . mocks = options . mocks ;
87
+ }
88
+
89
+ fetchEndpoint ( endpoint , request ) {
90
+ const config = this . getConfig ( ) ;
91
+
92
+ if ( config . environment === 'development' ) {
93
+ const mocks = this . mocks ;
94
+
95
+ return new Promise ( ( resolve , reject ) => {
96
+ try {
97
+ const mock = mocks . fetch ( endpoint , request ) ;
98
+
99
+ return resolve ( request . transform ( Object . assign ( { } , mock ) ) ) ;
100
+ } catch ( e ) {
101
+ return reject ( e ) ;
102
+ }
103
+ } ) ;
104
+ }
105
+
106
+ return fetchEndpoint ( request ) ;
84
107
}
85
108
86
109
/**
@@ -176,7 +199,7 @@ class AtlasSDKClient {
176
199
*/
177
200
getArticle ( sku , options = { } ) {
178
201
const url = `${ this . config . catalogApi . url } /articles/${ sku } ?client_id=${ this . config . clientId } ` ;
179
- const CatalogEndpoint = {
202
+ const CatalogRequest = {
180
203
url : url ,
181
204
method : 'GET' ,
182
205
headers : {
@@ -191,7 +214,7 @@ class AtlasSDKClient {
191
214
}
192
215
} ;
193
216
194
- return fetchEndpoint ( CatalogEndpoint ) . then ( article => {
217
+ return this . fetchEndpoint ( 'getArticle' , CatalogRequest ) . then ( article => {
195
218
return article ;
196
219
} ) ;
197
220
}
@@ -205,9 +228,9 @@ class AtlasSDKClient {
205
228
* @param {String } token
206
229
* @return {GetCheckoutResponse } guest checkout object
207
230
*/
208
- getCheckout ( checkoutId , token ) {
231
+ getGuestCheckout ( checkoutId , token ) {
209
232
const url = `${ this . config . atlasCheckoutGateway . url } /guest-checkout/api/checkouts/${ checkoutId } /${ token } ` ;
210
- const GetCheckoutEndpoint = {
233
+ const GetCheckoutRequest = {
211
234
url : url ,
212
235
method : 'GET' ,
213
236
headers : {
@@ -223,8 +246,8 @@ class AtlasSDKClient {
223
246
}
224
247
} ;
225
248
226
- return fetchEndpoint ( GetCheckoutEndpoint ) . then ( getCheckoutResponse => {
227
- return getCheckoutResponse ;
249
+ return this . fetchEndpoint ( 'getGuestCheckout' , GetCheckoutRequest ) . then ( getGuestCheckoutResponse => {
250
+ return getGuestCheckoutResponse ;
228
251
} ) ;
229
252
}
230
253
@@ -236,13 +259,13 @@ class AtlasSDKClient {
236
259
* @param {String } token
237
260
* @return {CreateOrderResponse } object with order information
238
261
*/
239
- createOrder ( checkoutId , token ) {
262
+ createGuestOrder ( checkoutId , token ) {
240
263
const url = `${ this . config . atlasCheckoutGateway . url } /guest-checkout/api/orders` ;
241
264
const body = JSON . stringify ( {
242
265
checkout_id : checkoutId ,
243
266
token : token
244
267
} ) ;
245
- const CreateOrderEndpoint = {
268
+ const CreateOrderRequest = {
246
269
url : url ,
247
270
method : 'POST' ,
248
271
headers : {
@@ -257,7 +280,7 @@ class AtlasSDKClient {
257
280
}
258
281
} ;
259
282
260
- return fetchEndpoint ( CreateOrderEndpoint ) . then ( createOrderResponse => {
283
+ return this . fetchEndpoint ( 'createGuestOrder' , CreateOrderRequest ) . then ( createOrderResponse => {
261
284
return createOrderResponse ;
262
285
} ) ;
263
286
}
@@ -332,7 +355,7 @@ class AtlasSDKClient {
332
355
const catalogUrl = config . catalogApi . url ;
333
356
const type = config . recommendations [ 0 ] . type ;
334
357
const url = `${ catalogUrl } /articles/${ sku } /recommendations/?client_id=${ config . clientId } &anon_id=${ options . reco_id } ` ; /* eslint max-len: 0 */
335
- const GetRecommendationsEndpoint = {
358
+ const GetRecommendationsRequest = {
336
359
url : url ,
337
360
method : 'GET' ,
338
361
headers : {
@@ -357,7 +380,7 @@ class AtlasSDKClient {
357
380
}
358
381
} ;
359
382
360
- return fetchEndpoint ( GetRecommendationsEndpoint ) . then ( recommendedArticles => {
383
+ return this . fetchEndpoint ( 'getRecommendations' , GetRecommendationsRequest ) . then ( recommendedArticles => {
361
384
return recommendedArticles ;
362
385
} ) ;
363
386
}
@@ -372,7 +395,7 @@ class AtlasSDKClient {
372
395
*/
373
396
createGuestCheckout ( json ) {
374
397
const url = `${ this . config . atlasCheckoutGateway . url } /guest-checkout/api/orders` ;
375
- const GuestCheckoutEndpoint = {
398
+ const GuestCheckoutRequest = {
376
399
url : url ,
377
400
method : 'POST' ,
378
401
mode : 'cors' ,
@@ -393,7 +416,7 @@ class AtlasSDKClient {
393
416
}
394
417
} ;
395
418
396
- return fetchEndpoint ( GuestCheckoutEndpoint ) . then ( guestCheckoutResponse => {
419
+ return this . fetchEndpoint ( 'createGuestCheckout' , GuestCheckoutRequest ) . then ( guestCheckoutResponse => {
397
420
return guestCheckoutResponse ;
398
421
} ) ;
399
422
}
0 commit comments