128 lines
4.6 KiB
TypeScript
128 lines
4.6 KiB
TypeScript
import axios from "axios";
|
|
|
|
const API_BASE_URL = process.env.REACT_APP_API_URL;
|
|
|
|
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) {
|
|
// Only redirect to login if we have a token (user was logged in)
|
|
const token = localStorage.getItem("token");
|
|
|
|
if (token) {
|
|
// User was logged in but token expired/invalid
|
|
localStorage.removeItem("token");
|
|
window.location.href = "/login";
|
|
}
|
|
// For non-authenticated users, just reject the error without redirecting
|
|
// Let individual components handle 401 errors as needed
|
|
}
|
|
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),
|
|
uploadProfileImage: (formData: FormData) =>
|
|
api.post("/users/profile/image", formData, {
|
|
headers: {
|
|
"Content-Type": "multipart/form-data",
|
|
},
|
|
}),
|
|
getPublicProfile: (id: string) => api.get(`/users/${id}`),
|
|
getAvailability: () => api.get("/users/availability"),
|
|
updateAvailability: (data: any) => api.put("/users/availability", data),
|
|
};
|
|
|
|
export const addressAPI = {
|
|
getAddresses: () => api.get("/users/addresses"),
|
|
createAddress: (data: any) => api.post("/users/addresses", data),
|
|
updateAddress: (id: string, data: any) =>
|
|
api.put(`/users/addresses/${id}`, data),
|
|
deleteAddress: (id: string) => api.delete(`/users/addresses/${id}`),
|
|
};
|
|
|
|
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"),
|
|
getItemReviews: (id: string) => api.get(`/items/${id}/reviews`),
|
|
};
|
|
|
|
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 }),
|
|
markAsCompleted: (id: string) => api.post(`/rentals/${id}/mark-completed`),
|
|
reviewRenter: (id: string, data: any) =>
|
|
api.post(`/rentals/${id}/review-renter`, data),
|
|
reviewItem: (id: string, data: any) =>
|
|
api.post(`/rentals/${id}/review-item`, data),
|
|
getRefundPreview: (id: string) => api.get(`/rentals/${id}/refund-preview`),
|
|
cancelRental: (id: string, reason?: string) =>
|
|
api.post(`/rentals/${id}/cancel`, { reason }),
|
|
};
|
|
|
|
export const messageAPI = {
|
|
getMessages: () => api.get("/messages"),
|
|
getSentMessages: () => api.get("/messages/sent"),
|
|
getMessage: (id: string) => api.get(`/messages/${id}`),
|
|
sendMessage: (data: any) => api.post("/messages", data),
|
|
markAsRead: (id: string) => api.put(`/messages/${id}/read`),
|
|
getUnreadCount: () => api.get("/messages/unread/count"),
|
|
};
|
|
|
|
export const itemRequestAPI = {
|
|
getItemRequests: (params?: any) => api.get("/item-requests", { params }),
|
|
getItemRequest: (id: string) => api.get(`/item-requests/${id}`),
|
|
createItemRequest: (data: any) => api.post("/item-requests", data),
|
|
updateItemRequest: (id: string, data: any) =>
|
|
api.put(`/item-requests/${id}`, data),
|
|
deleteItemRequest: (id: string) => api.delete(`/item-requests/${id}`),
|
|
getMyRequests: () => api.get("/item-requests/my-requests"),
|
|
respondToRequest: (id: string, data: any) =>
|
|
api.post(`/item-requests/${id}/responses`, data),
|
|
updateResponseStatus: (responseId: string, status: string) =>
|
|
api.put(`/item-requests/responses/${responseId}/status`, { status }),
|
|
};
|
|
|
|
export const stripeAPI = {
|
|
getCheckoutSession: (sessionId: string) =>
|
|
api.get(`/stripe/checkout-session/${sessionId}`),
|
|
createConnectedAccount: () => api.post("/stripe/accounts"),
|
|
createAccountLink: (data: { refreshUrl: string; returnUrl: string }) =>
|
|
api.post("/stripe/account-links", data),
|
|
getAccountStatus: () => api.get("/stripe/account-status"),
|
|
createSetupCheckoutSession: (data: {
|
|
rentalData?: any;
|
|
}) => api.post("/stripe/create-setup-checkout-session", data),
|
|
};
|
|
|
|
export default api;
|