API Calls
API calls
Set the default axios base URL to call the API
Open the .env file and edit REACT_APP_API_URL to your backend.
## Backend API URL
VITE_APP_API_URL=https://mock-data-api-nextjs.vercel.app/
You can configure the same for Next.js as well.
Axios has been configured in the folder ..src\utils\axios.js
With baseUrl
/**
* axios setup to use mock service
*/
import axios from 'axios';
const axiosServices = axios.create({ baseURL: import.meta.env.VITE_APP_API_URL || 'http://localhost:3010/' });
// ==============================|| AXIOS - FOR MOCK SERVICES ||============================== //
axiosServices.interceptors.request.use(
async (config) => {
const accessToken = localStorage.getItem('serviceToken');
if (accessToken) {
config.headers['Authorization'] = `Bearer ${accessToken}`;
}
return config;
},
(error) => {
return Promise.reject(error);
}
);
axiosServices.interceptors.response.use(
(response) => response,
(error) => {
if (error.response.status === 401 && !window.location.href.includes('/login')) {
window.location.pathname = '/login';
}
return Promise.reject((error.response && error.response.data) || 'Wrong Services');
}
);
export default axiosServices;
export const fetcher = async (args) => {
const [url, config] = Array.isArray(args) ? args : [args];
const res = await axiosServices.get(url, { ...config });
return res.data;
};Without baseUrl
You can set the entire URL in the Axios request. Do not use common Axios instances src\utils\axios.js Instead, use the Axios library.
Last updated
Was this helpful?