init
This commit is contained in:
138
web/src/utils/api.ts
Normal file
138
web/src/utils/api.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
||||
|
||||
// Type definitions
|
||||
interface LoginCredentials {
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface LoginResponse {
|
||||
success: boolean;
|
||||
token: string;
|
||||
user: {
|
||||
id: string;
|
||||
email: string;
|
||||
name?: string;
|
||||
};
|
||||
}
|
||||
|
||||
// Create a reusable Axios instance with default configuration
|
||||
const apiClient: AxiosInstance = axios.create({
|
||||
baseURL: 'http://localhost:4000',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
timeout: 10000, // 10 seconds timeout
|
||||
});
|
||||
|
||||
// Request interceptor for adding auth token
|
||||
apiClient.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = localStorage.getItem('auth_token');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// Response interceptor for handling common errors
|
||||
apiClient.interceptors.response.use(
|
||||
(response) => {
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
// Handle errors globally
|
||||
if (error.response) {
|
||||
// Server responded with error status (4xx, 5xx)
|
||||
if (error.response.status === 401) {
|
||||
// Unauthorized - clear local storage
|
||||
localStorage.removeItem('auth_token');
|
||||
localStorage.removeItem('user');
|
||||
|
||||
// Redirect to login page if not already there
|
||||
if (window.location.pathname !== '/login') {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// Auth API
|
||||
export const authApi = {
|
||||
login: (credentials: LoginCredentials): Promise<AxiosResponse<LoginResponse>> =>
|
||||
apiClient.post('/api/auth/login', credentials),
|
||||
verify: (headers?: Record<string, string>): Promise<AxiosResponse> =>
|
||||
apiClient.get('/api/auth/verify', headers ? { headers } : undefined),
|
||||
register: (data: { email: string; password: string; name: string }): Promise<AxiosResponse> =>
|
||||
apiClient.post('/api/auth/register', data),
|
||||
refreshToken: (): Promise<AxiosResponse<{token: string}>> =>
|
||||
apiClient.post('/api/auth/refresh-token'),
|
||||
};
|
||||
|
||||
// Comments API
|
||||
export const commentsApi = {
|
||||
getComments: (params?: Record<string, string | number | boolean>): Promise<AxiosResponse> =>
|
||||
apiClient.get('/api/comments', { params }),
|
||||
getComment: (id: string): Promise<AxiosResponse> =>
|
||||
apiClient.get(`/api/comments/${id}`),
|
||||
createComment: (data: Record<string, unknown>): Promise<AxiosResponse> =>
|
||||
apiClient.post('/api/comments', data),
|
||||
updateComment: (id: string, data: Record<string, unknown>): Promise<AxiosResponse> =>
|
||||
apiClient.put(`/api/comments/${id}`, data),
|
||||
deleteComment: (id: string): Promise<AxiosResponse> =>
|
||||
apiClient.delete(`/api/comments/${id}`),
|
||||
};
|
||||
|
||||
// Posts API
|
||||
export const postsApi = {
|
||||
getPosts: (params?: Record<string, string | number | boolean>): Promise<AxiosResponse> =>
|
||||
apiClient.get('/api/posts', { params }),
|
||||
getPost: (id: string): Promise<AxiosResponse> =>
|
||||
apiClient.get(`/api/posts/${id}`),
|
||||
createPost: (data: Record<string, unknown>): Promise<AxiosResponse> =>
|
||||
apiClient.post('/api/posts', data),
|
||||
updatePost: (id: string, data: Record<string, unknown>): Promise<AxiosResponse> =>
|
||||
apiClient.put(`/api/posts/${id}`, data),
|
||||
deletePost: (id: string): Promise<AxiosResponse> =>
|
||||
apiClient.delete(`/api/posts/${id}`),
|
||||
};
|
||||
|
||||
// Analytics API
|
||||
export const analyticsApi = {
|
||||
getPlatforms: (timeRange: string): Promise<AxiosResponse> =>
|
||||
apiClient.get(`/api/analytics/platforms?timeRange=${timeRange}`),
|
||||
getTimeline: (timeRange: string): Promise<AxiosResponse> =>
|
||||
apiClient.get(`/api/analytics/timeline?timeRange=${timeRange}`),
|
||||
getSentiment: (timeRange: string): Promise<AxiosResponse> =>
|
||||
apiClient.get(`/api/analytics/sentiment?timeRange=${timeRange}`),
|
||||
getStatus: (timeRange: string): Promise<AxiosResponse> =>
|
||||
apiClient.get(`/api/analytics/status?timeRange=${timeRange}`),
|
||||
getPopularContent: (timeRange: string): Promise<AxiosResponse> =>
|
||||
apiClient.get(`/api/analytics/popular-content?timeRange=${timeRange}`),
|
||||
getInfluencers: (timeRange: string): Promise<AxiosResponse> =>
|
||||
apiClient.get(`/api/analytics/influencers?timeRange=${timeRange}`),
|
||||
getConversion: (timeRange: string): Promise<AxiosResponse> =>
|
||||
apiClient.get(`/api/analytics/conversion?timeRange=${timeRange}`),
|
||||
};
|
||||
|
||||
// Templates API
|
||||
export const templatesApi = {
|
||||
getTemplates: (): Promise<AxiosResponse> =>
|
||||
apiClient.get('/api/reply-templates'),
|
||||
getTemplate: (id: string): Promise<AxiosResponse> =>
|
||||
apiClient.get(`/api/reply-templates/${id}`),
|
||||
createTemplate: (data: Record<string, unknown>): Promise<AxiosResponse> =>
|
||||
apiClient.post('/api/reply-templates', data),
|
||||
updateTemplate: (id: string, data: Record<string, unknown>): Promise<AxiosResponse> =>
|
||||
apiClient.put(`/api/reply-templates/${id}`, data),
|
||||
deleteTemplate: (id: string): Promise<AxiosResponse> =>
|
||||
apiClient.delete(`/api/reply-templates/${id}`),
|
||||
};
|
||||
|
||||
export default apiClient;
|
||||
Reference in New Issue
Block a user