Skip to content

Commit

Permalink
feat(carto): Sort params in request cache key (#8638)
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored Mar 12, 2024
1 parent eee10e5 commit f47609a
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions modules/carto/src/api/request-with-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ export async function requestWithParameters<T = any>({
headers: Record<string, string>;
errorContext: APIErrorContext;
}): Promise<T> {
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('&')}`;
}

Expand Down Expand Up @@ -66,3 +67,13 @@ export async function requestWithParameters<T = any>({
throw new CartoAPIError(error as Error, errorContext);
}
}

function createCacheKey(
baseUrl: string,
parameters: Record<string, string>,
headers: Record<string, string>
): 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});
}

0 comments on commit f47609a

Please sign in to comment.