Configuring the API client
For the API client of our application, we will be using Axios, a very popular library for handling HTTP requests. It is supported in both the browser and the server and has an API for creating instances, intercepting requests and responses, canceling requests, and so on.
Let’s start by creating an instance of Axios, which will include some common things we want to be done on every request.
Create the src/lib/api-client.ts
file and add the following:
import Axios from 'axios'; import { API_URL } from '@/config/constants'; export const apiClient = Axios.create({ baseURL: API_URL, headers: { 'Content-Type': 'application/json', }, }); apiClient.interceptors.response.use( (response) => { return response.data; }, (error) => { const message = ...