@selkirk-systems/fetch 1.2.0 → 1.2.1

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.
package/dist/Fetch.js CHANGED
@@ -438,17 +438,24 @@ export const putJsonInCache = (cache, url, json) => {
438
438
  const response = new Response(JSON.stringify(json), responseObjectJson);
439
439
  return cache.put(url, response);
440
440
  };
441
- let _cache = null;
441
+ const _caches = {};
442
442
  const DATA_METHODS = {
443
443
  "GET": null,
444
444
  "PATCH": null,
445
445
  "POST": null,
446
446
  "PUT": null
447
447
  };
448
+ const API_REG_EXP = /p\/.+com\/(.*?)\//;
449
+ export const getCacheNameFromUrl = url => {
450
+ const matchArray = url.toString().match(API_REG_EXP);
451
+ return matchArray && matchArray.length >= 1 ? matchArray[1] : null;
452
+ };
448
453
  const _fetch = (url, options = {}) => {
449
- if (self) {
454
+ const cacheName = getCacheNameFromUrl(url);
455
+ if (!self || !cacheName) {
450
456
  return Fetch(url, options);
451
457
  }
458
+ let _cache = _caches[cacheName];
452
459
  function cacheResponse([response, isAbort]) {
453
460
  const status = response.status.code;
454
461
  const headers = response.request.headers;
@@ -464,7 +471,7 @@ const _fetch = (url, options = {}) => {
464
471
  ...responseObjectJson
465
472
  };
466
473
  finalOptions.headers['Time-Cached'] = new Date().getTime();
467
- const responseObj = new Response(JSON.stringify(response), finalOptions);
474
+ const responseObj = new Response(JSON.stringify(response.data), finalOptions);
468
475
  _cache.put(url, responseObj);
469
476
  dispatch(UPDATE_CACHE, {
470
477
  url: url,
@@ -496,7 +503,7 @@ const _fetch = (url, options = {}) => {
496
503
  return Promise.resolve([{
497
504
  request: null,
498
505
  response: response,
499
- data: obj.data,
506
+ data: obj,
500
507
  status: {
501
508
  code: response.status,
502
509
  text: response.statusText,
@@ -511,7 +518,8 @@ const _fetch = (url, options = {}) => {
511
518
  if (_cache) {
512
519
  return cacheMatch();
513
520
  }
514
- return caches.open('fetch').then(cache => {
521
+ return caches.open(cacheName).then(cache => {
522
+ _caches[cacheName] = cache;
515
523
  _cache = cache;
516
524
  }).then(cacheMatch);
517
525
  };
package/lib/Fetch.js CHANGED
@@ -574,7 +574,8 @@ export const putJsonInCache = ( cache, url, json ) => {
574
574
 
575
575
  }
576
576
 
577
- let _cache = null;
577
+ const _caches = {};
578
+
578
579
  const DATA_METHODS = {
579
580
  "GET": null,
580
581
  "PATCH": null,
@@ -582,12 +583,23 @@ const DATA_METHODS = {
582
583
  "PUT": null
583
584
  }
584
585
 
586
+ const API_REG_EXP = /p\/.+com\/(.*?)\//;
587
+
588
+ export const getCacheNameFromUrl = ( url ) => {
589
+ const matchArray = url.toString().match( API_REG_EXP );
590
+ return matchArray && matchArray.length >= 1 ? matchArray[1] : null;
591
+ }
592
+
585
593
  const _fetch = ( url, options = {} ) => {
586
594
 
587
- if ( self ) {
595
+ const cacheName = getCacheNameFromUrl( url );
596
+
597
+ if ( !self || !cacheName ) {
588
598
  return Fetch( url, options );
589
599
  }
590
600
 
601
+ let _cache = _caches[cacheName];
602
+
591
603
  function cacheResponse( [response, isAbort] ) {
592
604
 
593
605
  const status = response.status.code;
@@ -612,7 +624,7 @@ const _fetch = ( url, options = {} ) => {
612
624
  const finalOptions = { ...responseObjectJson };
613
625
  finalOptions.headers['Time-Cached'] = new Date().getTime();
614
626
 
615
- const responseObj = new Response( JSON.stringify( response ), finalOptions );
627
+ const responseObj = new Response( JSON.stringify( response.data ), finalOptions );
616
628
  _cache.put( url, responseObj );
617
629
 
618
630
  dispatch( UPDATE_CACHE, { url: url, response: response } );
@@ -663,7 +675,7 @@ const _fetch = ( url, options = {} ) => {
663
675
  return Promise.resolve( [{
664
676
  request: null,
665
677
  response: response,
666
- data: obj.data,
678
+ data: obj,
667
679
  status: {
668
680
  code: response.status,
669
681
  text: response.statusText,
@@ -679,12 +691,15 @@ const _fetch = ( url, options = {} ) => {
679
691
  } )
680
692
  }
681
693
 
682
- if ( _cache ) {
683
694
 
695
+
696
+ if ( _cache ) {
684
697
  return cacheMatch()
685
698
  }
686
699
 
687
- return caches.open( 'fetch' ).then( ( cache ) => {
700
+
701
+ return caches.open( cacheName ).then( ( cache ) => {
702
+ _caches[cacheName] = cache;
688
703
  _cache = cache;
689
704
  } ).then( cacheMatch )
690
705
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@selkirk-systems/fetch",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
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": "5898e1276808bd3fe25d44f3935befe00c54513e"
39
+ "gitHead": "319e3d6212d678f409052a628b9a28455ecc7ecd"
40
40
  }