requests for auth/register
This commit is contained in:
parent
9a3f05ef60
commit
bc293f6370
9 changed files with 348 additions and 63 deletions
|
|
@ -13,7 +13,8 @@ interface RequestConfig {
|
|||
* @returns Конфигурация для fetch-запроса
|
||||
*/
|
||||
const createRequestConfig = (method: string, isFormData: boolean = false): RequestConfig => {
|
||||
const token = localStorage.getItem("accessToken");
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
const config: RequestConfig = {
|
||||
method,
|
||||
headers: {},
|
||||
|
|
@ -37,14 +38,36 @@ const createRequestConfig = (method: string, isFormData: boolean = false): Reque
|
|||
* @param response Ответ от fetch
|
||||
* @returns Распарсенные данные или ошибку
|
||||
*/
|
||||
const handleResponse = async (response: Response) => {
|
||||
const data = await response.json();
|
||||
// const handleResponse = async (response: Response) => {
|
||||
// const data = await response.json();
|
||||
//
|
||||
// if (!response.ok) {
|
||||
// throw new Error(data.message || "Произошла ошибка");
|
||||
// }
|
||||
//
|
||||
// return data;
|
||||
// };
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || "Произошла ошибка");
|
||||
const handleResponse = async (response: Response) => {
|
||||
// Проверяем, есть ли контент в ответе
|
||||
const responseText = await response.text();
|
||||
|
||||
if (!responseText) {
|
||||
if (response.ok) {
|
||||
return null; // Если ответ пустой, но статус 200, возвращаем null
|
||||
}
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return data;
|
||||
try {
|
||||
const data = JSON.parse(responseText);
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || `HTTP ${response.status}`);
|
||||
}
|
||||
return data;
|
||||
} catch (e) {
|
||||
throw new Error(`Не удалось разобрать ответ сервера: ${e}`);
|
||||
}
|
||||
};
|
||||
|
||||
export { BASE_URL, createRequestConfig, handleResponse };
|
||||
Loading…
Add table
Add a link
Reference in a new issue