Using Axios
In this section, we will explore one of the most popular libraries for working with the server, called Axios. This library is similar to the Fetch API but also provides additional features that make it a powerful tool for handling requests.
Let’s take our previous project and make some changes to it. First, let’s install Axios as a dependency:
npm install axios
One of Axios’s features is the ability to create instances with specific configurations, such as headers, base URLs, interceptors, and more. This allows us to have a preconfigured instance tailored to our needs, reducing code repetition and making it more scalable.
Let’s create an API class that encapsulates all the necessary logic for working with the server:
class API {
private apiInstance: AxiosInstance;
constructor() {
this.apiInstance = axios.create({
baseURL: "https://api.github.com",
});
this.apiInstance.interceptors.request...