-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcache.ts
More file actions
125 lines (104 loc) · 3.67 KB
/
cache.ts
File metadata and controls
125 lines (104 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// import { PersistanceStore } from '../persistance';
import { PersistanceStore } from '../persistance';
import { CacheOptions, Policy } from './types';
/**
* Extracts entry UID from request URL if available
* @param config - Request config object
* @returns entry UID if found, null otherwise
*/
function extractEntryUidFromUrl(config: any): string | null {
if (!config.url) return null;
// Match patterns like: /content_types/{content_type_uid}/entries/{entry_uid}
const entryUrlPattern = /\/content_types\/[^\/]+\/entries\/([^\/\?]+)/;
const match = config.url.match(entryUrlPattern);
return match ? match[1] : null;
}
/**
* Generates an improved cache key using content type UID and entry UID
* @param originalKey - Original cache key (apiKey)
* @param contentTypeUid - Content type UID
* @param entryUid - Entry UID (optional)
* @returns Enhanced cache key
*/
function generateEnhancedCacheKey(originalKey: string, contentTypeUid?: string, entryUid?: string): string {
let cacheKey = originalKey;
if (contentTypeUid) {
cacheKey = `${contentTypeUid}_${cacheKey}`;
}
if (entryUid) {
cacheKey = `${cacheKey}_entry_${entryUid}`;
}
return cacheKey;
}
export async function handleRequest(
cacheOptions: CacheOptions,
apiKey: string,
defaultAdapter: any,
resolve: any,
reject: any,
config: any
) {
const cacheStore = new PersistanceStore(cacheOptions);
// Extract entry UID from URL or config
const entryUid = config.entryUid || extractEntryUidFromUrl(config);
// Generate enhanced cache key using content type UID and entry UID
const enhancedCacheKey = generateEnhancedCacheKey(apiKey, config.contentTypeUid, entryUid);
switch (cacheOptions.policy) {
case Policy.NETWORK_ELSE_CACHE: {
const apiResponse = await defaultAdapter(config);
if (apiResponse.data) {
cacheStore.setItem(enhancedCacheKey, JSON.parse(apiResponse.data), config.contentTypeUid, cacheOptions.maxAge);
return resolve({data: JSON.parse(apiResponse.data)});
} else {
const cacheResponse = cacheStore.getItem(enhancedCacheKey, config.contentTypeUid);
if (cacheResponse)
return resolve({
data: cacheResponse,
status: 200,
statusText: 'OK',
headers: {},
config: {},
});
}
return reject(apiResponse);
}
case Policy.CACHE_THEN_NETWORK: {
const cacheResponse = cacheStore.getItem(enhancedCacheKey, config.contentTypeUid);
if (cacheResponse)
return resolve({
data: cacheResponse,
status: 200,
statusText: 'OK',
headers: {},
config: {},
});
const apiResponse = await defaultAdapter(config);
if (apiResponse.data) {
cacheStore.setItem(enhancedCacheKey, JSON.parse(apiResponse.data), config.contentTypeUid, cacheOptions.maxAge);
return resolve({data: JSON.parse(apiResponse.data)});
} else {
return reject(apiResponse);
}
}
case Policy.CACHE_ELSE_NETWORK: {
const cacheResponse = cacheStore.getItem(enhancedCacheKey, config.contentTypeUid);
if (cacheResponse)
return resolve({
data: cacheResponse,
status: 200,
statusText: 'OK',
headers: {},
config: {},
});
else {
const apiResponse = await defaultAdapter(config);
if (apiResponse.data) {
cacheStore.setItem(enhancedCacheKey, JSON.parse(apiResponse.data), config.contentTypeUid, cacheOptions.maxAge);
return resolve({data: JSON.parse(apiResponse.data)});
} else {
return reject(apiResponse);
}
}
}
}
}