diff --git a/modules/carto/src/api/request-with-parameters.ts b/modules/carto/src/api/request-with-parameters.ts index 00e3e852d0d..81c7b20ff6e 100644 --- a/modules/carto/src/api/request-with-parameters.ts +++ b/modules/carto/src/api/request-with-parameters.ts @@ -23,17 +23,18 @@ export async function requestWithParameters({ headers: Record; errorContext: APIErrorContext; }): Promise { - const key = JSON.stringify({baseUrl, parameters, customHeaders}); + const key = createCacheKey(baseUrl, parameters || {}, customHeaders || {}); if (REQUEST_CACHE.has(key)) { return REQUEST_CACHE.get(key); } let url = baseUrl; if (parameters) { - const allParameters = {...DEFAULT_PARAMETERS, ...parameters}; - const encodedParameters = Object.entries(allParameters).map(([key, value]) => { - return encodeParameter(key, value); - }); + const encodedParameters = Object.entries({...DEFAULT_PARAMETERS, ...parameters}).map( + ([key, value]) => { + return encodeParameter(key, value); + } + ); url += `?${encodedParameters.join('&')}`; } @@ -66,3 +67,13 @@ export async function requestWithParameters({ throw new CartoAPIError(error as Error, errorContext); } } + +function createCacheKey( + baseUrl: string, + parameters: Record, + headers: Record +): string { + const parameterEntries = Object.entries(parameters).sort(([a], [b]) => (a > b ? 1 : -1)); + const headerEntries = Object.entries(headers).sort(([a], [b]) => (a > b ? 1 : -1)); + return JSON.stringify({baseUrl, parameters: parameterEntries, headers: headerEntries}); +}