Skip to content

Commit

Permalink
feat(client): adding support for wildcard cache entries - to be used …
Browse files Browse the repository at this point in the history
…by email image generator only
  • Loading branch information
korgon committed Aug 31, 2024
1 parent 3106e08 commit 9f59823
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 40 deletions.
39 changes: 18 additions & 21 deletions packages/snap-client/src/Client/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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([
Expand All @@ -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([
Expand Down Expand Up @@ -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]]);
Expand Down Expand Up @@ -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]]);
Expand All @@ -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]]);
Expand All @@ -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]]);
Expand Down Expand Up @@ -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: {},
Expand All @@ -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([
Expand Down Expand Up @@ -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([
Expand All @@ -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([
Expand All @@ -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([
Expand Down Expand Up @@ -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]]);
Expand Down Expand Up @@ -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]]);
Expand All @@ -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]]);
Expand All @@ -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]]);
Expand Down Expand Up @@ -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: {},
Expand All @@ -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([
Expand Down
6 changes: 3 additions & 3 deletions packages/snap-client/src/Client/apis/Abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export class API {
return this.configuration.mode;
}

protected async request(context: RequestOpts, cacheKey?: any): Promise<Response> {
protected async request(context: RequestOpts, cacheKey?: string): Promise<Response> {
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;
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/snap-client/src/Client/apis/Legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class LegacyAPI extends API {
headers: headerParameters,
query: queryParameters,
},
path + JSON.stringify(cacheParameters)
JSON.stringify(cacheParameters)
);

return legacyResponse;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions packages/snap-client/src/Client/apis/Recommend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions packages/snap-client/src/Client/apis/Snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions packages/snap-client/src/Client/apis/Suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions packages/snap-preact-demo/public/email.html
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -54,7 +55,8 @@
cache: {
entries: {
[profileKey]: profileResponse,
[recommendKey]: recommendResponse,
//[recommendKey]: recommendResponse,
[wildCardKey]: recommendResponse,
}
},

Expand Down

0 comments on commit 9f59823

Please sign in to comment.