Skip to content

Commit

Permalink
Revert changes to requestWithParameters, excluding accessToken
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Mar 27, 2024
1 parent 4713df2 commit 1994bb3
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions modules/carto/src/api/request-with-parameters.ts
Expand Up @@ -22,44 +22,38 @@ export async function requestWithParameters<T = any>({
errorContext: APIErrorContext;
}): Promise<T> {
const key = createCacheKey(baseUrl, parameters || {}, customHeaders || {});
if (REQUEST_CACHE.has(key)) {
return REQUEST_CACHE.get(key) as Promise<T>;
}

if (!REQUEST_CACHE.has(key)) {
const url = parameters ? createURLWithParameters(baseUrl, parameters) : baseUrl;
const headers = {...DEFAULT_HEADERS, ...customHeaders};

/* global fetch */
const fetchPromise =
url.length > MAX_GET_LENGTH
? fetch(baseUrl, {method: 'POST', body: JSON.stringify(parameters), headers})
: fetch(url, {headers});
const url = parameters ? createURLWithParameters(baseUrl, parameters) : baseUrl;
const headers = {...DEFAULT_HEADERS, ...customHeaders};

let response: Response | undefined;
const jsonPromise: Promise<T> = fetchPromise
.then((_response: Response) => {
response = _response;
return response.json();
})
.then((json: any) => {
if (!response || !response.ok) {
throw new Error(json.error);
}
return json;
})
.catch((error: Error) => {
REQUEST_CACHE.delete(key);
throw new CartoAPIError(error, errorContext, response);
});
/* global fetch */
const fetchPromise =
url.length > MAX_GET_LENGTH
? fetch(baseUrl, {method: 'POST', body: JSON.stringify(parameters), headers})
: fetch(url, {headers});

REQUEST_CACHE.set(key, jsonPromise);
}
let response: Response | undefined;
const jsonPromise: Promise<T> = fetchPromise
.then((_response: Response) => {
response = _response;
return response.json();
})
.then((json: any) => {
if (!response || !response.ok) {
throw new Error(json.error);
}
return json;
})
.catch((error: Error) => {
REQUEST_CACHE.delete(key);
throw new CartoAPIError(error, errorContext, response);
});

// Cached requests do not share access tokens and error context.
return (REQUEST_CACHE.get(key) as Promise<T>).catch((error: Error) => {
if (error instanceof CartoAPIError) {
throw new CartoAPIError(error.error, errorContext, error.response);
}
throw new CartoAPIError(error, errorContext);
});
REQUEST_CACHE.set(key, jsonPromise);
return jsonPromise;
}

function createCacheKey(
Expand Down

0 comments on commit 1994bb3

Please sign in to comment.