TypeScript · 2157 bytes Raw Blame History
1 import axios from 'axios';
2 import { GenerateWordsRequest, GenerateWordsResponse, Corpus } from '@/types';
3
4 const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000/api';
5
6 const api = axios.create({
7 baseURL: API_BASE_URL,
8 headers: {
9 'Content-Type': 'application/json',
10 },
11 withCredentials: true, // Important for session cookies
12 });
13
14 export const wordGeneratorApi = {
15 generateWords: async (params: GenerateWordsRequest): Promise<GenerateWordsResponse> => {
16 const response = await api.post<GenerateWordsResponse>('/generate/', params);
17 return response.data;
18 },
19
20 healthCheck: async (): Promise<{ status: string }> => {
21 const response = await api.get('/health/');
22 return response.data;
23 },
24
25 // Community features
26 trackCopy: async (word: string): Promise<{ success: boolean; copy_count: number }> => {
27 const response = await api.post('/track-copy/', { word });
28 return response.data;
29 },
30
31 addDefinition: async (word: string, definition: string): Promise<{
32 success: boolean;
33 definition_id: number;
34 definition: {
35 id: number;
36 definition: string;
37 upvotes: number;
38 downvotes: number;
39 created_at: string;
40 };
41 }> => {
42 const response = await api.post('/add-definition/', { word, definition });
43 return response.data;
44 },
45
46 voteDefinition: async (definitionId: number, voteType: 'up' | 'down'): Promise<{
47 success: boolean;
48 upvotes: number;
49 downvotes: number;
50 }> => {
51 const response = await api.post('/vote-definition/', {
52 definition_id: definitionId,
53 vote_type: voteType
54 });
55 return response.data;
56 },
57
58 getWordDefinitions: async (word: string): Promise<{
59 word: string;
60 definitions: Array<{
61 id: number;
62 definition: string;
63 upvotes: number;
64 downvotes: number;
65 created_at: string;
66 }>;
67 }> => {
68 const response = await api.get(`/word/${encodeURIComponent(word)}/definitions/`);
69 return response.data;
70 },
71
72 getCorpora: async (): Promise<{ corpora: Corpus[] }> => {
73 const response = await api.get('/corpora/');
74 return response.data;
75 },
76 };