Skip to content

Commit 72d66ef

Browse files
committed
Merge branch 'development' into enhancement/DX-3430-Plugin-architecture
2 parents 1627f1a + 8211103 commit 72d66ef

File tree

12 files changed

+616
-40
lines changed

12 files changed

+616
-40
lines changed

.talismanrc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
fileignoreconfig:
22
- filename: package-lock.json
3-
checksum: dffbcb14c5761976a9b6ab90b96c7e4fc5784eebe381cf48014618cc93355dbc
3+
checksum: 17c3944c70209ee40840cee33e891b57cf1c672ecb03b410b71ad6fb0242a86e
44
- filename: test/unit/query-optimization-comprehensive.spec.ts
55
checksum: f5aaf6c784d7c101a05ca513c584bbd6e95f963d1e42779f2596050d9bcbac96
6-
- filename: src/lib/types.ts
7-
checksum: 57d2f7eede6ab19ff92db9799e752e7dfa44c1368314525a41ab1a241778a230
8-
- filename: examples/custom-plugin.ts
9-
checksum: 51a4b01f7c5f76c97c588bb0575cefd1f9462b28d88b24bb9f7dc2780aa323c5
10-
- filename: test/unit/plugin-comprehensive.spec.ts
11-
checksum: d508f95f4a7eb5bed873ca91a5e1914aa4d5261f27593edb568502cb5855042e
12-
- filename: test/unit/plugin-interceptor-comprehensive.spec.ts
13-
checksum: b9fbe9da166fd209853307f9f60464d25f805f82e9dbe09d5f476fdd5eb05d47
6+
- filename: src/lib/entries.ts
7+
checksum: f6a19da15baed75062ad0cc599573ed08926e28fffe3f6e4890a0efb4d58c910
8+
- filename: src/lib/cache.ts
9+
checksum: d8d32089b8a4b247e4ba71c22b56cbb0a54440ebf35b102af222eb8032919f02
10+
- filename: test/unit/cache.spec.ts
11+
checksum: e96f913a466a1f4d55a422e7032fc2c06eeed5fea86cdcc86a05fbe3eba29b7a
12+
- filename: src/lib/query.ts
13+
checksum: 073c47e46755eb79d1d7e9fcaf2864296a218bf650888dd37c42480cce7df379
14+
- filename: test/api/retry-integration.spec.ts
15+
checksum: dc07b0a8111fd8e155b99f56c31ccdddd4f46c86f1b162b17d73e15dfed8e3c8
16+
- filename: test/unit/retry-configuration.spec.ts
17+
checksum: 359c8601c6205a65f3395cc209a93b278dfe7f5bb547c91b2eeab250b2c85aa3
1418
version: ""

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### Version: 4.9.1
2+
#### Date: Aug-25-2025
3+
Fix: Enhance retry logic to use configured retryDelay
4+
Enhancement: Caching logic to use combination of content type uid and entry uid
5+
16
### Version: 4.9.0
27
#### Date: Aug-25-2025
38
Fix: Fixed Timeout parameter which is being ignored & Removed custom error object

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/delivery-sdk",
3-
"version": "4.9.0",
3+
"version": "4.9.1",
44
"type": "module",
55
"license": "MIT",
66
"main": "./dist/legacy/index.cjs",
@@ -37,7 +37,7 @@
3737
"dependencies": {
3838
"@contentstack/core": "^1.3.0",
3939
"@contentstack/utils": "^1.4.1",
40-
"axios": "^1.11.0",
40+
"axios": "^1.12.2",
4141
"humps": "^2.0.1"
4242
},
4343
"files": [

src/lib/base-query.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,11 @@ export class BaseQuery extends Pagination {
215215
requestParams = { ...this._queryParams, query: queryParams };
216216
}
217217

218-
const getRequestOptions: any = { params: requestParams };
218+
const getRequestOptions: any = {
219+
params: requestParams,
220+
// Add contentTypeUid to config for improved caching (extract from URL if possible)
221+
contentTypeUid: this.extractContentTypeUidFromUrl()
222+
};
219223

220224
if (this._variants) {
221225
getRequestOptions.headers = {
@@ -227,4 +231,18 @@ export class BaseQuery extends Pagination {
227231

228232
return response as FindResponse<T>;
229233
}
234+
235+
/**
236+
* Extracts content type UID from the URL path
237+
* @returns content type UID if found, null otherwise
238+
*/
239+
private extractContentTypeUidFromUrl(): string | null {
240+
if (!this._urlPath) return null;
241+
242+
// Match patterns like: /content_types/{content_type_uid}/entries
243+
const contentTypePattern = /\/content_types\/([^\/]+)/;
244+
const match = this._urlPath.match(contentTypePattern);
245+
246+
return match ? match[1] : null;
247+
}
230248
}

src/lib/cache.ts

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,42 @@
33
import { PersistanceStore } from '../persistance';
44
import { 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 = /\/content_types\/[^\/]+\/entries\/([^\/\?]+)/;
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+
642
export 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+
}

src/lib/entries.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { AxiosInstance, getData } from '@contentstack/core';
22
import { Query } from './query';
33
import { BaseQuery } from './base-query';
4+
import { FindResponse } from './types';
5+
import { encodeQueryParams } from './utils';
46

57
export class Entries extends BaseQuery {
68
private _contentTypeUid: string;
@@ -266,4 +268,37 @@ export class Entries extends BaseQuery {
266268
}
267269
return this;
268270
}
271+
272+
/**
273+
* Override find method to include content type UID directly for better caching
274+
*/
275+
override async find<T>(encode: boolean = false): Promise<FindResponse<T>> {
276+
let requestParams: { [key: string]: any } = this._queryParams;
277+
278+
if (Object.keys(this._parameters).length > 0) {
279+
let queryParams = { ...this._parameters };
280+
281+
if (encode) {
282+
queryParams = encodeQueryParams(queryParams);
283+
}
284+
285+
requestParams = { ...this._queryParams, query: queryParams };
286+
}
287+
288+
const getRequestOptions: any = {
289+
params: requestParams,
290+
// Add contentTypeUid directly for improved caching
291+
contentTypeUid: this._contentTypeUid
292+
};
293+
294+
if (this._variants) {
295+
getRequestOptions.headers = {
296+
...getRequestOptions.headers,
297+
'x-cs-variant-uid': this._variants
298+
};
299+
}
300+
const response = await getData(this._client, this._urlPath, getRequestOptions);
301+
302+
return response as FindResponse<T>;
303+
}
269304
}

src/lib/entry.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@ export class Entry {
180180
* const result = await stack.contentType(contentType_uid).entry(entry_uid).fetch();
181181
*/
182182
async fetch<T>(): Promise<T> {
183-
const getRequestOptions: any = { params: this._queryParams};
183+
const getRequestOptions: any = {
184+
params: this._queryParams,
185+
// Add contentTypeUid and entryUid to config for improved caching
186+
contentTypeUid: this._contentTypeUid,
187+
entryUid: this._entryUid
188+
};
184189
if (this._variants) {
185190
getRequestOptions.headers = {
186191
...getRequestOptions.headers,

src/lib/query.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { AxiosInstance } from '@contentstack/core';
1+
import { AxiosInstance, getData } from '@contentstack/core';
22
import { BaseQuery } from './base-query';
3-
import { BaseQueryParameters, QueryOperation, QueryOperator, TaxonomyQueryOperation, params, queryParams } from './types';
3+
import { BaseQueryParameters, QueryOperation, QueryOperator, TaxonomyQueryOperation, params, queryParams, FindResponse } from './types';
4+
import { encodeQueryParams } from './utils';
45

56
export class Query extends BaseQuery {
67
private _contentTypeUid?: string;
@@ -594,4 +595,37 @@ export class Query extends BaseQuery {
594595
this._parameters[key] = { '$gte': value };
595596
return this;
596597
}
598+
599+
/**
600+
* Override find method to include content type UID directly for better caching
601+
*/
602+
override async find<T>(encode: boolean = false): Promise<FindResponse<T>> {
603+
let requestParams: { [key: string]: any } = this._queryParams;
604+
605+
if (Object.keys(this._parameters).length > 0) {
606+
let queryParams = { ...this._parameters };
607+
608+
if (encode) {
609+
queryParams = encodeQueryParams(queryParams);
610+
}
611+
612+
requestParams = { ...this._queryParams, query: queryParams };
613+
}
614+
615+
const getRequestOptions: any = {
616+
params: requestParams,
617+
// Add contentTypeUid directly for improved caching
618+
contentTypeUid: this._contentTypeUid
619+
};
620+
621+
if (this._variants) {
622+
getRequestOptions.headers = {
623+
...getRequestOptions.headers,
624+
'x-cs-variant-uid': this._variants
625+
};
626+
}
627+
const response = await getData(this._client, this._urlPath, getRequestOptions);
628+
629+
return response as FindResponse<T>;
630+
}
597631
}

0 commit comments

Comments
 (0)