🐛 (api.tsx): fix syntax errors in API interceptor to ensure proper error handling and request rejection

💡 (api.tsx): improve error message for duplicate requests to provide more context
This commit is contained in:
cristhianzl 2024-05-29 11:11:12 -03:00
commit a98b86a991

View file

@ -47,7 +47,7 @@ function ApiInterceptor() {
} }
await clearBuildVerticesState(error); await clearBuildVerticesState(error);
return Promise.reject(error); return Promise.reject(error);
}, }
); );
const isAuthorizedURL = (url) => { const isAuthorizedURL = (url) => {
@ -64,10 +64,10 @@ function ApiInterceptor() {
const parsedURL = new URL(url); const parsedURL = new URL(url);
const isDomainAllowed = authorizedDomains.some( const isDomainAllowed = authorizedDomains.some(
(domain) => parsedURL.origin === new URL(domain).origin, (domain) => parsedURL.origin === new URL(domain).origin
); );
const isEndpointAllowed = authorizedEndpoints.some((endpoint) => const isEndpointAllowed = authorizedEndpoints.some((endpoint) =>
parsedURL.pathname.includes(endpoint), parsedURL.pathname.includes(endpoint)
); );
return isDomainAllowed || isEndpointAllowed; return isDomainAllowed || isEndpointAllowed;
@ -87,7 +87,11 @@ function ApiInterceptor() {
config?.url !== "/health" && config?.url !== "/health" &&
config?.method === "get" config?.method === "get"
) { ) {
return Promise.reject("Duplicate request"); return Promise.reject(`
Duplicate request detected.
URL: ${config.url}
Method: ${config.method}
`);
} }
localStorage.setItem("lastUrlCalled", config.url ?? ""); localStorage.setItem("lastUrlCalled", config.url ?? "");
@ -101,7 +105,7 @@ function ApiInterceptor() {
}, },
(error) => { (error) => {
return Promise.reject(error); return Promise.reject(error);
}, }
); );
return () => { return () => {
@ -133,7 +137,7 @@ function ApiInterceptor() {
if (error?.config?.headers) { if (error?.config?.headers) {
delete error.config.headers["Authorization"]; delete error.config.headers["Authorization"];
error.config.headers["Authorization"] = `Bearer ${cookies.get( error.config.headers["Authorization"] = `Bearer ${cookies.get(
"access_token_lf", "access_token_lf"
)}`; )}`;
const response = await axios.request(error.config); const response = await axios.request(error.config);
return response; return response;