Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(carto): Sort params in request cache key. #8638

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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});
}
Loading