@selkirk-systems/fetch 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,11 @@
1
1
  import { unwrapResponseData } from '../Fetch';
2
2
  import { DELETE_FROM_CACHE, UPDATE_CACHE } from '../constants/FetchConstants';
3
3
  import { dispatch, serializeData } from "@selkirk-systems/state-management";
4
+ const grayCSS = "color:gray;font-weight:lighter";
5
+ const blueCSS = "color:#87A7DB;font-weight:lighter";
6
+ const bodyCSS = "font-weight:bold";
7
+ const prevCSS = "color:gray;font-weight:bold";
8
+ const nextCSS = "color:#6bc679;font-weight:bold";
4
9
 
5
10
  //30 minutes
6
11
  const CACHED_EXPIRY_TIMESTAMP = 30 * 60000;
@@ -22,6 +27,7 @@ const CacheAPI = Fetch => (url, options = {}) => {
22
27
 
23
28
  //HANDLE: Service worker BS, environment is different don't cache if were in a service worker.
24
29
  if (!self || !cacheName) {
30
+ console.log(`CacheAPI.getCacheNameFromUrl: No cacheName found in - ${url.toString()} `);
25
31
  return Fetch(url, options);
26
32
  }
27
33
  let _cache = _caches[cacheName];
@@ -126,6 +132,7 @@ const CacheAPI = Fetch => (url, options = {}) => {
126
132
  value: uuid.id
127
133
  }).then(match => {
128
134
  if (match) {
135
+ logCacheHit(url, match.match);
129
136
  //Behind the scenes still fetch the record and update the cache but don't make the user wait for it.
130
137
  Fetch(url, options).then(cacheResponse);
131
138
  const responseObj = new Response(JSON.stringify(match.match));
@@ -140,6 +147,7 @@ const CacheAPI = Fetch => (url, options = {}) => {
140
147
  }
141
148
  }, false]);
142
149
  }
150
+ logCacheMiss(url);
143
151
  return Fetch(url, options).then(cacheResponse);
144
152
  });
145
153
  }
@@ -147,10 +155,12 @@ const CacheAPI = Fetch => (url, options = {}) => {
147
155
  if (response) {
148
156
  const timeCached = response.headers.get('Time-Cached');
149
157
  if (expiredCache(timeCached)) {
158
+ logCacheExpired(url, response);
150
159
  _cache.delete(url);
151
160
  return Fetch(url, options).then(cacheResponse);
152
161
  }
153
162
  return unwrapResponseData(response).then(obj => {
163
+ logCacheHit(url, obj, timeCached);
154
164
  Fetch(url, options).then(cacheResponse);
155
165
  return Promise.resolve([{
156
166
  request: null,
@@ -164,6 +174,7 @@ const CacheAPI = Fetch => (url, options = {}) => {
164
174
  }, false]);
165
175
  });
166
176
  }
177
+ logCacheMiss(url);
167
178
  return Fetch(url, options).then(cacheResponse);
168
179
  });
169
180
  }
@@ -291,7 +302,7 @@ function getUUID(str) {
291
302
  };
292
303
  }
293
304
  function getCacheNameFromUrl(url) {
294
- const API_REG_EXP = /p\/.+com\/(.*?)\//;
305
+ const API_REG_EXP = /\/.+com\/(.*?)\//;
295
306
  const matchArray = url.toString().match(API_REG_EXP);
296
307
  return matchArray && matchArray.length >= 1 ? matchArray[1] : null;
297
308
  }
@@ -299,4 +310,22 @@ function expiredCache(timeCached) {
299
310
  const now = new Date().getTime();
300
311
  return Math.abs(now - timeCached) >= CACHED_EXPIRY_TIMESTAMP;
301
312
  }
313
+ function logCacheHit(url, cache, timeCached) {
314
+ console.groupCollapsed(`%ccache %cHIT %c${url}`, blueCSS, bodyCSS, grayCSS);
315
+ console.log(`Cached at: ${new Date(parseInt(timeCached, 10))}`);
316
+ console.log(`Data`, cache);
317
+ console.log(`URL`, url);
318
+ console.groupEnd();
319
+ }
320
+ function logCacheExpired(url, cache) {
321
+ console.groupCollapsed(`%ccache %cEXPIRED %c${url}`, blueCSS, bodyCSS, grayCSS);
322
+ console.log(`URL`, url);
323
+ console.log(`Cache`, cache);
324
+ console.groupEnd();
325
+ }
326
+ function logCacheMiss(url) {
327
+ console.groupCollapsed(`%ccache %cMISS %c${url}`, blueCSS, bodyCSS, grayCSS);
328
+ console.log(`URL`, url);
329
+ console.groupEnd();
330
+ }
302
331
  export default CacheAPI;
@@ -2,6 +2,12 @@ import { unwrapResponseData } from '../Fetch';
2
2
  import { DELETE_FROM_CACHE, UPDATE_CACHE } from '../constants/FetchConstants';
3
3
  import { dispatch, serializeData } from "@selkirk-systems/state-management";
4
4
 
5
+ const grayCSS = "color:gray;font-weight:lighter";
6
+ const blueCSS = "color:#87A7DB;font-weight:lighter";
7
+ const bodyCSS = "font-weight:bold";
8
+ const prevCSS = "color:gray;font-weight:bold";
9
+ const nextCSS = "color:#6bc679;font-weight:bold";
10
+
5
11
  //30 minutes
6
12
  const CACHED_EXPIRY_TIMESTAMP = 30 * 60000;
7
13
 
@@ -29,6 +35,7 @@ const CacheAPI = ( Fetch ) => ( url, options = {} ) => {
29
35
 
30
36
  //HANDLE: Service worker BS, environment is different don't cache if were in a service worker.
31
37
  if ( !self || !cacheName ) {
38
+ console.log(`CacheAPI.getCacheNameFromUrl: No cacheName found in - ${url.toString()} `)
32
39
  return Fetch( url, options );
33
40
  }
34
41
 
@@ -157,7 +164,7 @@ const CacheAPI = ( Fetch ) => ( url, options = {} ) => {
157
164
  return cacheFindLike( { cache: _cache, url: uuid.shortPath, property: "id", value: uuid.id } ).then( ( match ) => {
158
165
 
159
166
  if ( match ) {
160
-
167
+ logCacheHit(url,match.match);
161
168
  //Behind the scenes still fetch the record and update the cache but don't make the user wait for it.
162
169
  Fetch( url, options ).then( cacheResponse );
163
170
 
@@ -175,6 +182,7 @@ const CacheAPI = ( Fetch ) => ( url, options = {} ) => {
175
182
  }, false] )
176
183
  }
177
184
 
185
+ logCacheMiss(url);
178
186
 
179
187
  return Fetch( url, options ).then( cacheResponse );
180
188
 
@@ -191,13 +199,19 @@ const CacheAPI = ( Fetch ) => ( url, options = {} ) => {
191
199
 
192
200
  if ( expiredCache( timeCached ) ) {
193
201
 
202
+ logCacheExpired(url,response);
203
+
194
204
  _cache.delete( url );
195
205
  return Fetch( url, options ).then( cacheResponse );
196
206
 
197
207
  }
198
208
 
199
209
  return unwrapResponseData( response ).then( ( obj ) => {
210
+
211
+ logCacheHit(url,obj,timeCached);
212
+
200
213
  Fetch( url, options ).then( cacheResponse )
214
+
201
215
  return Promise.resolve( [{
202
216
  request: null,
203
217
  response: response,
@@ -213,6 +227,8 @@ const CacheAPI = ( Fetch ) => ( url, options = {} ) => {
213
227
 
214
228
  }
215
229
 
230
+ logCacheMiss(url);
231
+
216
232
  return Fetch( url, options ).then( cacheResponse );
217
233
  } )
218
234
  }
@@ -232,6 +248,7 @@ const CacheAPI = ( Fetch ) => ( url, options = {} ) => {
232
248
  }
233
249
 
234
250
 
251
+
235
252
  function deleteFromCache( _cache, url, response ) {
236
253
 
237
254
  _cache.delete( url );
@@ -403,7 +420,7 @@ function getUUID( str ) {
403
420
 
404
421
  function getCacheNameFromUrl( url ) {
405
422
 
406
- const API_REG_EXP = /p\/.+com\/(.*?)\//;
423
+ const API_REG_EXP = /\/.+com\/(.*?)\//;
407
424
  const matchArray = url.toString().match( API_REG_EXP );
408
425
 
409
426
  return matchArray && matchArray.length >= 1 ? matchArray[1] : null;
@@ -418,5 +435,35 @@ function expiredCache( timeCached ) {
418
435
 
419
436
  }
420
437
 
438
+ function logCacheHit(url, cache,timeCached){
439
+ console.groupCollapsed( `%ccache %cHIT %c${url}`, blueCSS,bodyCSS,grayCSS );
440
+
441
+ console.log(`Cached at: ${new Date(parseInt(timeCached,10))}`);
442
+ console.log( `Data`, cache);
443
+ console.log( `URL`, url );
444
+
445
+ console.groupEnd();
446
+ }
447
+
448
+ function logCacheExpired(url,cache){
449
+
450
+ console.groupCollapsed( `%ccache %cEXPIRED %c${url}`, blueCSS,bodyCSS,grayCSS );
451
+
452
+ console.log( `URL`, url );
453
+ console.log( `Cache`, cache);
454
+
455
+ console.groupEnd();
456
+ }
457
+
458
+ function logCacheMiss(url){
459
+
460
+ console.groupCollapsed( `%ccache %cMISS %c${url}`, blueCSS,bodyCSS,grayCSS );
461
+
462
+ console.log( `URL`, url );
463
+
464
+ console.groupEnd();
465
+
466
+ }
467
+
421
468
 
422
469
  export default CacheAPI
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@selkirk-systems/fetch",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "description": "Abortable fetch library",
5
5
  "keywords": [],
6
6
  "author": "Marcos Bernal <mbernal@selkirksystems.com>",
@@ -36,5 +36,5 @@
36
36
  "peerDependencies": {
37
37
  "@selkirk-systems/state-management": ">=1.0.0"
38
38
  },
39
- "gitHead": "66fcf1754a7a7feaece908c6368ae08efbf8d63b"
39
+ "gitHead": "ad61a2964c2561d8b0efa3858189319523415a29"
40
40
  }