From 9f59823e9586235b53315021ab7440d3b309ea3f Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 30 Aug 2024 23:36:35 -0600 Subject: [PATCH] feat(client): adding support for wildcard cache entries - to be used by email image generator only --- .../snap-client/src/Client/Client.test.ts | 39 +++++++++---------- .../snap-client/src/Client/apis/Abstract.ts | 6 +-- .../snap-client/src/Client/apis/Legacy.ts | 6 +-- .../snap-client/src/Client/apis/Recommend.ts | 6 +-- packages/snap-client/src/Client/apis/Snap.ts | 6 +-- .../snap-client/src/Client/apis/Suggest.ts | 8 ++-- packages/snap-preact-demo/public/email.html | 8 ++-- 7 files changed, 39 insertions(+), 40 deletions(-) diff --git a/packages/snap-client/src/Client/Client.test.ts b/packages/snap-client/src/Client/Client.test.ts index b9001416f..5e38059f7 100644 --- a/packages/snap-client/src/Client/Client.test.ts +++ b/packages/snap-client/src/Client/Client.test.ts @@ -162,7 +162,7 @@ describe('Snap Client', () => { }, }; - const metaCacheKey = '/api/meta/meta.json{"siteId":"8uyt2m"}'; + const metaCacheKey = '{"siteId":"8uyt2m"}'; expect(metaRequesterSpy).toHaveBeenCalledTimes(1); expect(metaRequesterSpy.mock.calls).toEqual([ @@ -180,8 +180,7 @@ describe('Snap Client', () => { suggestionCount: 5, }, }; - const suggestCacheKey = - '/api/suggest/query{"siteId":["8uyt2m"],"language":"en","query":["hello"],"suggestionCount":5,"disableSpellCorrect":true}'; + const suggestCacheKey = '{"siteId":["8uyt2m"],"language":"en","query":["hello"],"suggestionCount":5,"disableSpellCorrect":true}'; expect(suggestRequesterSpy).toHaveBeenCalledTimes(1); expect(suggestRequesterSpy.mock.calls).toEqual([ @@ -199,7 +198,7 @@ describe('Snap Client', () => { siteId: ['8uyt2m'], }, }; - const acCacheKey = '/api/search/autocomplete.json{"siteId":["8uyt2m"],"redirectResponse":"full","ajaxCatalog":"Snap","resultsFormat":"native"}'; + const acCacheKey = '{"siteId":["8uyt2m"],"redirectResponse":"full","ajaxCatalog":"Snap","resultsFormat":"native"}'; expect(acRequesterSpy).toHaveBeenCalledTimes(1); expect(acRequesterSpy.mock.calls).toEqual([ @@ -236,7 +235,7 @@ describe('Snap Client', () => { }, }; - const metaCacheKey = '/api/meta/meta.json{"siteId":"8uyt2m"}'; + const metaCacheKey = '{"siteId":"8uyt2m"}'; expect(metaRequesterSpy).toHaveBeenCalledTimes(1); expect(metaRequesterSpy.mock.calls).toEqual([[metaRequest, metaCacheKey]]); @@ -274,7 +273,7 @@ describe('Snap Client', () => { query: { resultsFormat: 'native', siteId: ['8uyt2m'], ajaxCatalog: 'Snap' }, }; - const searchcacheKey = '/api/search/search.json{"siteId":["8uyt2m"],"ajaxCatalog":"Snap","resultsFormat":"native"}'; + const searchcacheKey = '{"siteId":["8uyt2m"],"ajaxCatalog":"Snap","resultsFormat":"native"}'; expect(searchRequesterSpy).toHaveBeenCalledTimes(1); expect(searchRequesterSpy.mock.calls).toEqual([[searchparams, searchcacheKey]]); @@ -288,7 +287,7 @@ describe('Snap Client', () => { }, }; - const metaCacheKey = '/api/meta/meta.json{"siteId":"8uyt2m"}'; + const metaCacheKey = '{"siteId":"8uyt2m"}'; expect(metaRequesterSpy).toHaveBeenCalledTimes(1); expect(metaRequesterSpy.mock.calls).toEqual([[metaRequest, metaCacheKey]]); @@ -315,7 +314,7 @@ describe('Snap Client', () => { const trendingparams = { headers: {}, method: 'GET', path: '/api/suggest/trending', query: { siteId: '8uyt2m' } }; - const trendingcacheKey = '/api/suggest/trending{"siteId":"8uyt2m"}'; + const trendingcacheKey = '{"siteId":"8uyt2m"}'; expect(suggestRequesterSpy).toHaveBeenCalledTimes(1); expect(suggestRequesterSpy.mock.calls).toEqual([[trendingparams, trendingcacheKey]]); @@ -349,7 +348,7 @@ describe('Snap Client', () => { }, }; - const profileCacheKey = '/api/personalized-recommendations/profile.json{"tag":"dress","siteId":"8uyt2m"}'; + const profileCacheKey = '{"tag":"dress","siteId":"8uyt2m"}'; const recommendParams = { headers: {}, @@ -363,7 +362,7 @@ describe('Snap Client', () => { }, }; - const recommendCacheKey = '/boost/8uyt2m/recommend{"tags":["dress"],"limits":[20],"siteId":"8uyt2m","test":true}'; + const recommendCacheKey = '{"tags":["dress"],"limits":[20],"siteId":"8uyt2m","test":true}'; expect(recommendRequesterSpy).toHaveBeenCalledTimes(2); expect(recommendRequesterSpy.mock.calls).toEqual([ @@ -413,7 +412,7 @@ describe('Snap Client', () => { }, }; - const metaCacheKey = '/api/meta/meta.json{"siteId":"8uyt2m"}'; + const metaCacheKey = '{"siteId":"8uyt2m"}'; expect(metaRequesterSpy).toHaveBeenCalledTimes(1); expect(metaRequesterSpy.mock.calls).toEqual([ @@ -431,8 +430,7 @@ describe('Snap Client', () => { suggestionCount: 5, }, }; - const suggestCacheKey = - '/api/suggest/query{"siteId":["8uyt2m"],"language":"en","query":["hello"],"suggestionCount":5,"disableSpellCorrect":true}'; + const suggestCacheKey = '{"siteId":["8uyt2m"],"language":"en","query":["hello"],"suggestionCount":5,"disableSpellCorrect":true}'; expect(suggestRequesterSpy).toHaveBeenCalledTimes(1); expect(suggestRequesterSpy.mock.calls).toEqual([ @@ -450,8 +448,7 @@ describe('Snap Client', () => { siteId: ['8uyt2m'], }, }; - const acCacheKey = - '/api/search/autocomplete.json{"siteId":["8uyt2m"],"redirectResponse":"full","ajaxCatalog":"Snap","resultsFormat":"native"}'; + const acCacheKey = '{"siteId":["8uyt2m"],"redirectResponse":"full","ajaxCatalog":"Snap","resultsFormat":"native"}'; expect(acRequesterSpy).toHaveBeenCalledTimes(1); expect(acRequesterSpy.mock.calls).toEqual([ @@ -486,7 +483,7 @@ describe('Snap Client', () => { }, }; - const metaCacheKey = '/api/meta/meta.json{"siteId":"8uyt2m"}'; + const metaCacheKey = '{"siteId":"8uyt2m"}'; expect(metaRequesterSpy).toHaveBeenCalledTimes(1); expect(metaRequesterSpy.mock.calls).toEqual([[metaRequest, metaCacheKey]]); @@ -521,7 +518,7 @@ describe('Snap Client', () => { query: { resultsFormat: 'native', siteId: ['8uyt2m'], ajaxCatalog: 'Snap' }, }; - const searchcacheKey = '/api/search/search.json{"siteId":["8uyt2m"],"ajaxCatalog":"Snap","resultsFormat":"native"}'; + const searchcacheKey = '{"siteId":["8uyt2m"],"ajaxCatalog":"Snap","resultsFormat":"native"}'; expect(searchRequesterSpy).toHaveBeenCalledTimes(1); expect(searchRequesterSpy.mock.calls).toEqual([[searchparams, searchcacheKey]]); @@ -535,7 +532,7 @@ describe('Snap Client', () => { }, }; - const metaCacheKey = '/api/meta/meta.json{"siteId":"8uyt2m"}'; + const metaCacheKey = '{"siteId":"8uyt2m"}'; expect(metaRequesterSpy).toHaveBeenCalledTimes(1); expect(metaRequesterSpy.mock.calls).toEqual([[metaRequest, metaCacheKey]]); @@ -560,7 +557,7 @@ describe('Snap Client', () => { const trendingparams = { headers: {}, method: 'GET', path: '/api/suggest/trending', query: { siteId: '8uyt2m' } }; - const trendingcacheKey = '/api/suggest/trending{"siteId":"8uyt2m"}'; + const trendingcacheKey = '{"siteId":"8uyt2m"}'; expect(suggestRequesterSpy).toHaveBeenCalledTimes(1); expect(suggestRequesterSpy.mock.calls).toEqual([[trendingparams, trendingcacheKey]]); @@ -592,7 +589,7 @@ describe('Snap Client', () => { }, }; - const profileCacheKey = '/api/personalized-recommendations/profile.json{"tag":"dress","siteId":"8uyt2m"}'; + const profileCacheKey = '{"tag":"dress","siteId":"8uyt2m"}'; const recommendParams = { headers: {}, @@ -606,7 +603,7 @@ describe('Snap Client', () => { }, }; - const recommendCacheKey = '/boost/8uyt2m/recommend{"tags":["dress"],"limits":[20],"siteId":"8uyt2m","test":true}'; + const recommendCacheKey = '{"tags":["dress"],"limits":[20],"siteId":"8uyt2m","test":true}'; expect(recommendRequesterSpy).toHaveBeenCalledTimes(2); expect(recommendRequesterSpy.mock.calls).toEqual([ diff --git a/packages/snap-client/src/Client/apis/Abstract.ts b/packages/snap-client/src/Client/apis/Abstract.ts index 87bebc8d4..f4291c1d1 100644 --- a/packages/snap-client/src/Client/apis/Abstract.ts +++ b/packages/snap-client/src/Client/apis/Abstract.ts @@ -34,11 +34,11 @@ export class API { return this.configuration.mode; } - protected async request(context: RequestOpts, cacheKey?: any): Promise { + protected async request(context: RequestOpts, cacheKey?: string): Promise { const { url, init } = this.createFetchParams(context); if (cacheKey) { - const cachedResponse = this.cache.get(cacheKey); + const cachedResponse = this.cache.get(`${context.path}/${cacheKey}`) || this.cache.get(`${context.path}/*`); if (cachedResponse) { this.retryCount = 0; // reset count and delay incase rate limit occurs again before a page refresh this.retryDelay = 1000; @@ -57,7 +57,7 @@ export class API { this.retryDelay = 1000; if (cacheKey) { // save in the cache before returning - this.cache.set(cacheKey, responseJSON); + this.cache.set(`${context.path}/${cacheKey}`, responseJSON); } return responseJSON; } else if (response.status == 429) { diff --git a/packages/snap-client/src/Client/apis/Legacy.ts b/packages/snap-client/src/Client/apis/Legacy.ts index f052d6f0b..dea393a11 100644 --- a/packages/snap-client/src/Client/apis/Legacy.ts +++ b/packages/snap-client/src/Client/apis/Legacy.ts @@ -20,7 +20,7 @@ export class LegacyAPI extends API { headers: headerParameters, query: queryParameters, }, - path + JSON.stringify(cacheParameters) + JSON.stringify(cacheParameters) ); return legacyResponse; @@ -38,7 +38,7 @@ export class LegacyAPI extends API { headers: headerParameters, body: requestParameters, }, - '/api/meta/meta.json' + JSON.stringify(requestParameters) + JSON.stringify(requestParameters) ); return response as MetaResponseModel; @@ -54,7 +54,7 @@ export class LegacyAPI extends API { headers: headerParameters, query: queryParameters as unknown as HTTPQuery, }, - '/api/meta/meta.json' + JSON.stringify(queryParameters) + JSON.stringify(queryParameters) ); return response as MetaResponseModel; diff --git a/packages/snap-client/src/Client/apis/Recommend.ts b/packages/snap-client/src/Client/apis/Recommend.ts index da781d5d5..9c77ba1c8 100644 --- a/packages/snap-client/src/Client/apis/Recommend.ts +++ b/packages/snap-client/src/Client/apis/Recommend.ts @@ -47,7 +47,7 @@ export class RecommendAPI extends API { headers: headerParameters, query: queryParameters, }, - '/api/personalized-recommendations/profile.json' + JSON.stringify(queryParameters) + JSON.stringify(queryParameters) ); return response as unknown as ProfileResponseModel; @@ -177,7 +177,7 @@ export class RecommendAPI extends API { headers: headerParameters, query: queryParameters, }, - path + JSON.stringify(queryParameters) + JSON.stringify(queryParameters) ); return response as unknown as RecommendResponseModel; @@ -197,7 +197,7 @@ export class RecommendAPI extends API { headers: headerParameters, body: requestParameters, }, - path + JSON.stringify(requestParameters) + JSON.stringify(requestParameters) ); return response as unknown as RecommendResponseModel; diff --git a/packages/snap-client/src/Client/apis/Snap.ts b/packages/snap-client/src/Client/apis/Snap.ts index 4c8dc989a..d24e7534c 100644 --- a/packages/snap-client/src/Client/apis/Snap.ts +++ b/packages/snap-client/src/Client/apis/Snap.ts @@ -22,7 +22,7 @@ export class SnapAPI extends API { headers: headerParameters, body: requestParameters, }, - '/api/v1/meta' + JSON.stringify(requestParameters) + JSON.stringify(requestParameters) ); return response as MetaResponseModel; @@ -39,7 +39,7 @@ export class SnapAPI extends API { headers: headerParameters, body: requestParameters, }, - '/api/v1/search' + JSON.stringify(requestParameters) + JSON.stringify(requestParameters) ); return response as SearchResponseModel; @@ -56,7 +56,7 @@ export class SnapAPI extends API { headers: headerParameters, body: requestParameters, }, - '/api/v1/autocomplete' + JSON.stringify(requestParameters) + JSON.stringify(requestParameters) ); return response as AutocompleteResponseModel; diff --git a/packages/snap-client/src/Client/apis/Suggest.ts b/packages/snap-client/src/Client/apis/Suggest.ts index af3c58681..ddb16da72 100644 --- a/packages/snap-client/src/Client/apis/Suggest.ts +++ b/packages/snap-client/src/Client/apis/Suggest.ts @@ -12,7 +12,7 @@ export class SuggestAPI extends API { headers: headerParameters, query: queryParameters, }, - '/api/suggest/query' + JSON.stringify(queryParameters) + JSON.stringify(queryParameters) ); return response as unknown as SuggestResponseModel; @@ -29,7 +29,7 @@ export class SuggestAPI extends API { headers: headerParameters, body: requestParameters, }, - '/api/suggest/query' + JSON.stringify(requestParameters) + JSON.stringify(requestParameters) ); return response as unknown as SuggestResponseModel; @@ -45,7 +45,7 @@ export class SuggestAPI extends API { headers: headerParameters, query: queryParameters, }, - '/api/suggest/trending' + JSON.stringify(queryParameters) + JSON.stringify(queryParameters) ); return response as unknown as TrendingResponseModel; @@ -62,7 +62,7 @@ export class SuggestAPI extends API { headers: headerParameters, body: requestParameters, }, - '/api/suggest/trending' + JSON.stringify(requestParameters) + JSON.stringify(requestParameters) ); return response as unknown as TrendingResponseModel; diff --git a/packages/snap-preact-demo/public/email.html b/packages/snap-preact-demo/public/email.html index bc4b91950..01d6d8206 100644 --- a/packages/snap-preact-demo/public/email.html +++ b/packages/snap-preact-demo/public/email.html @@ -28,8 +28,9 @@ const recommendResponse = [{"results":results,"profile":{"tag":profile}}]; const profileResponse = {"profile":{"tag":profile,"display":{"template":{"component":component},"templateParameters":{}}}}; - const profileKey = `/api/personalized-recommendations/profile.json{"tag":"${profile}","siteId":"${siteID}","branch":"production"}`; - const recommendKey = `/boost/${siteID}/recommend{"tags":["${profile}"],"limits":[20],"siteId":"8uyt2m"}`; + const profileKey = `/api/personalized-recommendations/profile.json/{"tag":"${profile}","siteId":"${siteID}","branch":"production"}`; + const recommendKey = `/boost/${siteID}/recommend/{"tags":["${profile}"],"limits":[20],"siteId":"8uyt2m"}`; + const wildCardKey = `/boost/${siteID}/recommend/*`; document.addEventListener('RecsReady', () => { @@ -54,7 +55,8 @@ cache: { entries: { [profileKey]: profileResponse, - [recommendKey]: recommendResponse, + //[recommendKey]: recommendResponse, + [wildCardKey]: recommendResponse, } },