Initial commit - Rentall App
- Full-stack rental marketplace application - React frontend with TypeScript - Node.js/Express backend with JWT authentication - Features: item listings, rental requests, calendar availability, user profiles
This commit is contained in:
60
frontend/src/services/api.ts
Normal file
60
frontend/src/services/api.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import axios from 'axios';
|
||||
|
||||
const API_BASE_URL = 'http://localhost:5001/api';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
api.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
localStorage.removeItem('token');
|
||||
window.location.href = '/login';
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export const authAPI = {
|
||||
register: (data: any) => api.post('/auth/register', data),
|
||||
login: (data: any) => api.post('/auth/login', data),
|
||||
};
|
||||
|
||||
export const userAPI = {
|
||||
getProfile: () => api.get('/users/profile'),
|
||||
updateProfile: (data: any) => api.put('/users/profile', data),
|
||||
};
|
||||
|
||||
export const itemAPI = {
|
||||
getItems: (params?: any) => api.get('/items', { params }),
|
||||
getItem: (id: string) => api.get(`/items/${id}`),
|
||||
createItem: (data: any) => api.post('/items', data),
|
||||
updateItem: (id: string, data: any) => api.put(`/items/${id}`, data),
|
||||
deleteItem: (id: string) => api.delete(`/items/${id}`),
|
||||
getRecommendations: () => api.get('/items/recommendations'),
|
||||
};
|
||||
|
||||
export const rentalAPI = {
|
||||
createRental: (data: any) => api.post('/rentals', data),
|
||||
getMyRentals: () => api.get('/rentals/my-rentals'),
|
||||
getMyListings: () => api.get('/rentals/my-listings'),
|
||||
updateRentalStatus: (id: string, status: string) =>
|
||||
api.put(`/rentals/${id}/status`, { status }),
|
||||
addReview: (id: string, data: any) =>
|
||||
api.post(`/rentals/${id}/review`, data),
|
||||
};
|
||||
|
||||
export default api;
|
||||
Reference in New Issue
Block a user