mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-02 12:03:12 +00:00
Fix error swallowing in apiUtil and stale auth headers
- Restore error propagation in all fetch methods (was commented out) - Change static header objects to functions so auth token is fresh per request - Add missing return in putFormBody - Properly throw on 401 unauthorized responses
This commit is contained in:
parent
e7fb682f71
commit
58c6ac6bcc
1 changed files with 40 additions and 72 deletions
112
src/apiUtil.js
112
src/apiUtil.js
|
|
@ -1,108 +1,76 @@
|
|||
import { API_ERROR_MESSAGE } from "./constants";
|
||||
import { getAccessToken } from "./tokenUtility";
|
||||
|
||||
const STANDARD_HEADERS = {
|
||||
const getHeaders = () => ({
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
pragma: "no-cache",
|
||||
"cache-control": "no-cache",
|
||||
Authorization: getAccessToken()
|
||||
};
|
||||
Authorization: getAccessToken(),
|
||||
});
|
||||
|
||||
const STANDARD_HEADERS_FORM = {
|
||||
const getFormHeaders = () => ({
|
||||
accept: "application/json",
|
||||
pragma: "no-cache",
|
||||
"cache-control": "no-cache",
|
||||
Authorization: getAccessToken(),
|
||||
};
|
||||
});
|
||||
|
||||
export const get = (url) =>
|
||||
fetch(url, {
|
||||
headers: { ...STANDARD_HEADERS, Authorization: getAccessToken() },
|
||||
headers: getHeaders(),
|
||||
method: "GET",
|
||||
})
|
||||
.then(handleHttpResponse)
|
||||
.catch((ex) => {
|
||||
// throw new Error(ex.message || API_ERROR_MESSAGE);
|
||||
});
|
||||
}).then(handleHttpResponse);
|
||||
|
||||
export const post = (url, payload) =>
|
||||
fetch(url, {
|
||||
headers: STANDARD_HEADERS,
|
||||
headers: getHeaders(),
|
||||
method: "POST",
|
||||
body: JSON.stringify(payload),
|
||||
})
|
||||
.then(handleHttpResponse)
|
||||
.catch((ex) => {
|
||||
// throw new Error(ex.message || API_ERROR_MESSAGE);
|
||||
});
|
||||
}).then(handleHttpResponse);
|
||||
|
||||
export const postFormBody = (url, payload) => {
|
||||
const formData = new FormData();
|
||||
|
||||
for (const [key, value] of Object.entries(payload)) {
|
||||
formData.append(key, value);
|
||||
}
|
||||
|
||||
return fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
...STANDARD_HEADERS_FORM,
|
||||
},
|
||||
body: formData,
|
||||
})
|
||||
.then(handleHttpResponse)
|
||||
.catch((ex) => {
|
||||
// throw new Error(ex.message || API_ERROR_MESSAGE);
|
||||
});
|
||||
};
|
||||
|
||||
export const put = (url, payload) =>
|
||||
fetch(url, {
|
||||
headers: STANDARD_HEADERS,
|
||||
method: "PUT",
|
||||
body: JSON.stringify(payload),
|
||||
})
|
||||
.then(handleHttpResponse)
|
||||
.catch((ex) => {
|
||||
// throw new Error(ex.message || API_ERROR_MESSAGE);
|
||||
});
|
||||
|
||||
export const putFormBody = (url, payload) => {
|
||||
export const postFormBody = (url, payload) => {
|
||||
const formData = new FormData();
|
||||
|
||||
for (const [key, value] of Object.entries(payload)) {
|
||||
formData.append(key, value);
|
||||
}
|
||||
|
||||
fetch(url, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
...STANDARD_HEADERS_FORM,
|
||||
},
|
||||
return fetch(url, {
|
||||
method: "POST",
|
||||
headers: getFormHeaders(),
|
||||
body: formData,
|
||||
})
|
||||
.then(handleHttpResponse)
|
||||
.catch((ex) => {
|
||||
// throw new Error(ex.message || API_ERROR_MESSAGE);
|
||||
});
|
||||
}).then(handleHttpResponse);
|
||||
};
|
||||
|
||||
export const put = (url, payload) =>
|
||||
fetch(url, {
|
||||
headers: getHeaders(),
|
||||
method: "PUT",
|
||||
body: JSON.stringify(payload),
|
||||
}).then(handleHttpResponse);
|
||||
|
||||
export const putFormBody = (url, payload) => {
|
||||
const formData = new FormData();
|
||||
for (const [key, value] of Object.entries(payload)) {
|
||||
formData.append(key, value);
|
||||
}
|
||||
return fetch(url, {
|
||||
method: "PUT",
|
||||
headers: getFormHeaders(),
|
||||
body: formData,
|
||||
}).then(handleHttpResponse);
|
||||
};
|
||||
|
||||
export const deleteItem = (url, payload) =>
|
||||
fetch(url, {
|
||||
headers: STANDARD_HEADERS,
|
||||
method: "Delete",
|
||||
headers: getHeaders(),
|
||||
method: "DELETE",
|
||||
body: JSON.stringify(payload),
|
||||
})
|
||||
.then(handleHttpResponse)
|
||||
.catch((ex) => {
|
||||
// throw new Error(ex.message || API_ERROR_MESSAGE);
|
||||
});
|
||||
}).then(handleHttpResponse);
|
||||
|
||||
const handleHttpResponse = (response) => {
|
||||
if (response.ok)
|
||||
return response.json();
|
||||
else if (response.status === 401) {
|
||||
//throw new Error("You are not authorized to access this page.");
|
||||
if (response.ok) return response.json();
|
||||
if (response.status === 401) {
|
||||
throw new Error("You are not authorized to access this page.");
|
||||
}
|
||||
throw new Error(API_ERROR_MESSAGE);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue