33import { PersistanceStore } from '../persistance' ;
44import { CacheOptions , Policy } from './types' ;
55
6+ /**
7+ * Extracts entry UID from request URL if available
8+ * @param config - Request config object
9+ * @returns entry UID if found, null otherwise
10+ */
11+ function extractEntryUidFromUrl ( config : any ) : string | null {
12+ if ( ! config . url ) return null ;
13+
14+ // Match patterns like: /content_types/{content_type_uid}/entries/{entry_uid}
15+ const entryUrlPattern = / \/ c o n t e n t _ t y p e s \/ [ ^ \/ ] + \/ e n t r i e s \/ ( [ ^ \/ \? ] + ) / ;
16+ const match = config . url . match ( entryUrlPattern ) ;
17+
18+ return match ? match [ 1 ] : null ;
19+ }
20+
21+ /**
22+ * Generates an improved cache key using content type UID and entry UID
23+ * @param originalKey - Original cache key (apiKey)
24+ * @param contentTypeUid - Content type UID
25+ * @param entryUid - Entry UID (optional)
26+ * @returns Enhanced cache key
27+ */
28+ function generateEnhancedCacheKey ( originalKey : string , contentTypeUid ?: string , entryUid ?: string ) : string {
29+ let cacheKey = originalKey ;
30+
31+ if ( contentTypeUid ) {
32+ cacheKey = `${ contentTypeUid } _${ cacheKey } ` ;
33+ }
34+
35+ if ( entryUid ) {
36+ cacheKey = `${ cacheKey } _entry_${ entryUid } ` ;
37+ }
38+
39+ return cacheKey ;
40+ }
41+
642export async function handleRequest (
743 cacheOptions : CacheOptions ,
844 apiKey : string ,
@@ -12,16 +48,23 @@ export async function handleRequest(
1248 config : any
1349) {
1450 const cacheStore = new PersistanceStore ( cacheOptions ) ;
51+
52+ // Extract entry UID from URL or config
53+ const entryUid = config . entryUid || extractEntryUidFromUrl ( config ) ;
54+
55+ // Generate enhanced cache key using content type UID and entry UID
56+ const enhancedCacheKey = generateEnhancedCacheKey ( apiKey , config . contentTypeUid , entryUid ) ;
57+
1558 switch ( cacheOptions . policy ) {
1659 case Policy . NETWORK_ELSE_CACHE : {
1760 const apiResponse = await defaultAdapter ( config ) ;
1861
1962 if ( apiResponse . data ) {
20- cacheStore . setItem ( apiKey , JSON . parse ( apiResponse . data ) , config . contentTypeUid , cacheOptions . maxAge ) ;
63+ cacheStore . setItem ( enhancedCacheKey , JSON . parse ( apiResponse . data ) , config . contentTypeUid , cacheOptions . maxAge ) ;
2164
2265 return resolve ( { data : JSON . parse ( apiResponse . data ) } ) ;
2366 } else {
24- const cacheResponse = cacheStore . getItem ( apiKey , config . contentTypeUid ) ;
67+ const cacheResponse = cacheStore . getItem ( enhancedCacheKey , config . contentTypeUid ) ;
2568 if ( cacheResponse )
2669 return resolve ( {
2770 data : cacheResponse ,
@@ -35,7 +78,7 @@ export async function handleRequest(
3578 return reject ( apiResponse ) ;
3679 }
3780 case Policy . CACHE_THEN_NETWORK : {
38- const cacheResponse = cacheStore . getItem ( apiKey , config . contentTypeUid ) ;
81+ const cacheResponse = cacheStore . getItem ( enhancedCacheKey , config . contentTypeUid ) ;
3982 if ( cacheResponse )
4083 return resolve ( {
4184 data : cacheResponse ,
@@ -48,15 +91,15 @@ export async function handleRequest(
4891 const apiResponse = await defaultAdapter ( config ) ;
4992
5093 if ( apiResponse . data ) {
51- cacheStore . setItem ( apiKey , JSON . parse ( apiResponse . data ) , config . contentTypeUid , cacheOptions . maxAge ) ;
94+ cacheStore . setItem ( enhancedCacheKey , JSON . parse ( apiResponse . data ) , config . contentTypeUid , cacheOptions . maxAge ) ;
5295
5396 return resolve ( { data : JSON . parse ( apiResponse . data ) } ) ;
5497 } else {
5598 return reject ( apiResponse ) ;
5699 }
57100 }
58101 case Policy . CACHE_ELSE_NETWORK : {
59- const cacheResponse = cacheStore . getItem ( apiKey , config . contentTypeUid ) ;
102+ const cacheResponse = cacheStore . getItem ( enhancedCacheKey , config . contentTypeUid ) ;
60103
61104 if ( cacheResponse )
62105 return resolve ( {
@@ -70,7 +113,7 @@ export async function handleRequest(
70113 const apiResponse = await defaultAdapter ( config ) ;
71114
72115 if ( apiResponse . data ) {
73- cacheStore . setItem ( apiKey , JSON . parse ( apiResponse . data ) , config . contentTypeUid , cacheOptions . maxAge ) ;
116+ cacheStore . setItem ( enhancedCacheKey , JSON . parse ( apiResponse . data ) , config . contentTypeUid , cacheOptions . maxAge ) ;
74117
75118 return resolve ( { data : JSON . parse ( apiResponse . data ) } ) ;
76119 } else {
@@ -79,4 +122,4 @@ export async function handleRequest(
79122 }
80123 }
81124 }
82- }
125+ }
0 commit comments