/
Infernocasis
/
X.com
Обзор
Документация
Войти
/
Infernocasis
/
X.com
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
x-clone/src/services/api.ts
115 строк
3 KB
Infernocasis
Initial commit
08 июн 2025, 20:43
08 июн 2025, 20:43
68371ab
Код
Авторство
О чём код?
import axios from 'axios'; const API_URL = process.env.REACT_APP_API_URL || 'http://localhost:3001'; const api = axios.create({ baseURL: API_URL, headers: { 'Content-Type': 'application/json', }, }); api.interceptors.request.use((config) => { const token = localStorage.getItem('token'); if (token && config.headers) { config.headers.Authorization = `Bearer ${token}`; } return config; }); export interface LoginData { email: string; password: string; } export interface RegisterData { username: string; email: string; password: string; } export interface User { id: number; username: string; email: string; avatar_url?: string; cover_url?: string; bio?: string; followers_count: number; following_count: number; created_at: string; updated_at: string; is_following?: boolean; } export interface Post { id: number; content: string; user: User; likes_count: number; retweets_count: number; is_liked: boolean; is_retweeted: boolean; created_at: string; updated_at: string; original_author?: User; retweeted_by?: User; } interface AuthResponse { token: string; user: User; } export const auth = { login: (data: LoginData) => api.post<AuthResponse>('/api/v1/auth/login', { email: data.email, password: data.password }), register: (data: RegisterData) => api.post<AuthResponse>('/api/v1/auth/register', { user: data }), me: () => api.get<User>('/api/v1/auth/me'), }; export const users = { get: (id: number) => api.get<User>(`/api/v1/users/${id}`), update: (id: number, data: Partial<User>) => api.put<User>(`/api/v1/users/${id}`, { user: data }), show: (id: number) => api.get<User>(`/api/v1/users/${id}`), posts: (id: number) => api.get<Post[]>(`/api/v1/users/${id}/posts`), follow: (id: number) => api.post<User>(`/api/v1/users/${id}/follow`), unfollow: (id: number) => api.delete<User>(`/api/v1/users/${id}/unfollow`), followers: (id: number) => api.get<User[]>(`/api/v1/users/${id}/followers`), following: (id: number) => api.get<User[]>(`/api/v1/users/${id}/following`), }; export const posts = { list: () => api.get<Post[]>('/api/v1/posts'), create: (content: string) => api.post<Post>('/api/v1/posts', { post: { content } }), like: (id: number) => api.post<Post>(`/api/v1/posts/${id}/like`), unlike: (id: number) => api.delete<Post>(`/api/v1/posts/${id}/unlike`), retweet: (id: number) => api.post<Post>(`/api/v1/posts/${id}/retweet`), unretweet: (id: number) => api.delete<Post>(`/api/v1/posts/${id}/unretweet`), }; interface SearchPostsParams { query?: string; hashtag?: string; } export const searchUsers = async (query: string): Promise<User[]> => { const response = await axios.get<User[]>(`${API_URL}/search/users`, { params: { query }, headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); return response.data; }; export const searchPosts = async (params: SearchPostsParams): Promise<Post[]> => { const response = await axios.get<Post[]>(`${API_URL}/search/posts`, { params, headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); return response.data; }; export default api;