-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
63 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,29 @@ | ||
import axios from "axios"; | ||
import { NavigateFunction } from "react-router-dom"; | ||
import { StorageKeys } from "../constants/storageKeys"; | ||
import { ROUTES } from "@/constants/routes"; | ||
|
||
interface InterceptorParams { | ||
navigate?: NavigateFunction; | ||
showNotification?: (message: string) => void; | ||
} | ||
|
||
export class ApiError extends Error { | ||
statusCode; | ||
|
||
constructor(message: string, statusCode?: number) { | ||
super(message); | ||
this.statusCode = statusCode; | ||
} | ||
} | ||
|
||
export const request = axios.create({ | ||
baseURL: import.meta.env.VITE_BASE_URL || "", | ||
}); | ||
|
||
export const requestInterceptor = () => | ||
request.interceptors.request.use((config) => { | ||
const token = localStorage.getItem(StorageKeys.TOKEN); | ||
if (token) { | ||
config.headers.Authorization = `Bearer ${token}`; | ||
} | ||
|
||
return config; | ||
}); | ||
|
||
export const responseInterceptor = ({ | ||
navigate, | ||
showNotification, | ||
}: InterceptorParams) => | ||
request.interceptors.response.use( | ||
(response) => response, | ||
(error) => { | ||
if (error.response.status === 403) { | ||
showNotification?.("Нет доступа к ресурсу"); | ||
|
||
throw new ApiError("Нет доступа к ресурсу", 403); | ||
} else if ( | ||
error.response.status === 401 && | ||
!error.request.responseURL.includes("login") | ||
) { | ||
localStorage.removeItem(StorageKeys.TOKEN); | ||
|
||
navigate?.(ROUTES.LOGIN); | ||
showNotification?.("Пожалуйста авторизуйтесь"); | ||
request.interceptors.request.use((config) => { | ||
const token = localStorage.getItem(StorageKeys.TOKEN); | ||
if (token) { | ||
config.headers.Authorization = `Bearer ${token}`; | ||
} | ||
|
||
throw new ApiError("Пожалуйста авторизуйтесь", 401); | ||
} | ||
return config; | ||
}); | ||
|
||
return Promise.reject(error.response || error.message); | ||
request.interceptors.response.use( | ||
(response) => response, | ||
(error) => { | ||
if ( | ||
error.response.status === 401 && | ||
!error.request.responseURL.includes("login") | ||
) { | ||
localStorage.removeItem(StorageKeys.TOKEN); | ||
} | ||
); | ||
|
||
export const setInterceptors = ({ | ||
navigate, | ||
showNotification, | ||
}: InterceptorParams) => { | ||
requestInterceptor(); | ||
responseInterceptor({ navigate, showNotification }); | ||
}; | ||
|
||
export const cleanInterceptors = () => { | ||
request.interceptors.request.eject(requestInterceptor()); | ||
request.interceptors.response.eject(responseInterceptor({})); | ||
}; | ||
return Promise.reject(error.response || error.message); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters