@@ -59,10 +59,7 @@ export default class OfflineFirstAPI {
59
59
{ headers : ( options && options . headers ) || { } }
60
60
) ;
61
61
const fetchHeaders = options && options . fetchHeaders ;
62
- // Should be cached if : not disabled globally, not disabled for this service definition and not
63
- // disabled from the option parameter of fetch ()
64
- const shouldCache = ! this . _APIOptions . disableCache &&
65
- ! ( serviceDefinition . disableCache || ( options && options . disableCache ) ) ;
62
+ const shouldUseCache = this . _shouldUseCache ( serviceDefinition , options ) ;
66
63
let requestId ;
67
64
let expiration ;
68
65
@@ -77,13 +74,13 @@ export default class OfflineFirstAPI {
77
74
expiration = Date . now ( ) + expirationDelay ;
78
75
const cachedData = await this . _getCachedData ( service , requestId , fullPath ) ;
79
76
80
- if ( cachedData . success && cachedData . fresh ) {
77
+ if ( cachedData . success && cachedData . fresh && shouldUseCache ) {
81
78
this . _log ( `Using fresh cache for ${ fullPath } ` ) ;
82
79
return cachedData . data ;
83
80
}
84
81
85
82
// Network fetch
86
- this . _logNetwork ( serviceDefinition , fetchHeaders , options ) ;
83
+ this . _logNetwork ( serviceDefinition , fullPath , fetchHeaders , options ) ;
87
84
this . _log ( 'full URL for request' , fullPath ) ;
88
85
this . _log ( 'full fetch options for request' , fetchOptions ) ;
89
86
let parsedResponseData ;
@@ -107,7 +104,7 @@ export default class OfflineFirstAPI {
107
104
}
108
105
109
106
// Cache if it hasn't been disabled and if the network request has been successful
110
- if ( res . data . ok && shouldCache ) {
107
+ if ( res . data . ok && shouldUseCache ) {
111
108
this . _cache ( service , requestId , parsedResponseData , expiration ) ;
112
109
}
113
110
@@ -174,7 +171,6 @@ export default class OfflineFirstAPI {
174
171
this . _log ( 'custom driver set' ) ;
175
172
}
176
173
177
-
178
174
/**
179
175
* Simple helper that won't ever throw an error into the stack if the network request
180
176
* isn't successful. This is useful to implement the cache's logic when the API is unreachable.
@@ -192,7 +188,6 @@ export default class OfflineFirstAPI {
192
188
}
193
189
}
194
190
195
-
196
191
/**
197
192
* Cache the network response for a request. Create the service dictionary if it hasn't been done yet,
198
193
* store the expiration date to the requestId and finally store the data itself.
@@ -216,7 +211,6 @@ export default class OfflineFirstAPI {
216
211
}
217
212
}
218
213
219
-
220
214
/**
221
215
* Promise that tries to fetch a cached data. Resolves the data if successful and its freshness.
222
216
* If this request hasn't been cached yet, resolves with success set to false.
@@ -253,6 +247,27 @@ export default class OfflineFirstAPI {
253
247
}
254
248
255
249
250
+ /**
251
+ * Helper returning if caching should be enabled for a request.
252
+ * Cache enabling priority :
253
+ * option parameter of fetch() > service definition > default setting
254
+ * @private
255
+ * @param {IAPIService } serviceDefinition
256
+ * @param {IFetchOptions } options
257
+ * @returns {boolean }
258
+ * @memberof OfflineFirstAPI
259
+ */
260
+ private _shouldUseCache ( serviceDefinition : IAPIService , options : IFetchOptions ) : boolean {
261
+ const cacheDisabledFromOptions = options && options . disableCache ;
262
+ if ( typeof cacheDisabledFromOptions !== 'undefined' ) {
263
+ return ! cacheDisabledFromOptions ;
264
+ } else if ( typeof serviceDefinition . disableCache !== 'undefined' ) {
265
+ return ! serviceDefinition . disableCache ;
266
+ } else {
267
+ return ! this . _APIOptions . disableCache ;
268
+ }
269
+ }
270
+
256
271
/**
257
272
* Pushes a requestId into a service's dictionary and associate its expiration date to it.
258
273
* @private
@@ -279,7 +294,6 @@ export default class OfflineFirstAPI {
279
294
}
280
295
}
281
296
282
-
283
297
/**
284
298
* Promise that resolves every cache key associated to a service : the service dictionary's name, and all requestId
285
299
* stored. This is useful to clear the cache without affecting the user's stored data not related to this API.
@@ -305,7 +319,6 @@ export default class OfflineFirstAPI {
305
319
}
306
320
}
307
321
308
-
309
322
/**
310
323
* Simple helper getting a service's dictionary cache key.
311
324
* @private
@@ -317,7 +330,6 @@ export default class OfflineFirstAPI {
317
330
return `${ CACHE_PREFIX } :dictionary:${ service } ` ;
318
331
}
319
332
320
-
321
333
/**
322
334
* Simple helper getting a request's cache key.
323
335
* @private
@@ -329,7 +341,6 @@ export default class OfflineFirstAPI {
329
341
return `${ CACHE_PREFIX } :${ requestId } ` ;
330
342
}
331
343
332
-
333
344
/**
334
345
* Resolve each middleware provided and merge them into a single object that will be passed to
335
346
* the network request.
@@ -355,7 +366,6 @@ export default class OfflineFirstAPI {
355
366
}
356
367
}
357
368
358
-
359
369
/**
360
370
* Helper returning the full URL of a service and its options.
361
371
* @private
@@ -374,7 +384,6 @@ export default class OfflineFirstAPI {
374
384
return domainURL + prefix + '/' + parsedPath ;
375
385
}
376
386
377
-
378
387
/**
379
388
* Helper replacing the pathParameters from the service definition's path and appending
380
389
* any supplied query parameters. For instance :
@@ -408,7 +417,6 @@ export default class OfflineFirstAPI {
408
417
return path ;
409
418
}
410
419
411
-
412
420
/**
413
421
* Merge the supplied API options with the default ones.
414
422
* @private
@@ -427,7 +435,6 @@ export default class OfflineFirstAPI {
427
435
} ;
428
436
}
429
437
430
-
431
438
/**
432
439
* For each suppliedservice, map the default service options to it and throw errors if the required
433
440
* options are missing.
@@ -457,7 +464,6 @@ export default class OfflineFirstAPI {
457
464
} ) ;
458
465
}
459
466
460
-
461
467
/**
462
468
* Debug helper logging every network request.
463
469
* @private
@@ -466,17 +472,16 @@ export default class OfflineFirstAPI {
466
472
* @param {IFetchOptions } [options]
467
473
* @memberof OfflineFirstAPI
468
474
*/
469
- private _logNetwork ( serviceDefinition : IAPIService , fetchHeaders : boolean , options ?: IFetchOptions ) : void {
475
+ private _logNetwork ( serviceDefinition : IAPIService , fullPath : string , fetchHeaders : boolean , options ?: IFetchOptions ) : void {
470
476
if ( this . _APIOptions . printNetworkRequests ) {
471
477
console . log (
472
- `%c Network request ${ fetchHeaders ? '(headers only)' : '' } for ${ serviceDefinition . path } ` +
478
+ `%c Network request ${ fetchHeaders ? '(headers only)' : '' } for ${ fullPath } ` +
473
479
`(${ ( options && options . method ) || serviceDefinition . method } )` ,
474
480
'font-weight: bold; color: blue'
475
481
) ;
476
482
}
477
483
}
478
484
479
-
480
485
/**
481
486
* Debug helper logging every major logic step when user has enabled debugging.
482
487
* @private
0 commit comments