-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapiService.ts
More file actions
64 lines (54 loc) · 2.07 KB
/
apiService.ts
File metadata and controls
64 lines (54 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// ekho-frontend/src/services/apiService.ts
import type {
AvatarCreationRequest,
VideoGenerationResponse,
VideoStatusResponse,
ChatRequest,
ChatResponse,
HealthCheckResponse,
CloneVoiceResponse
} from '../types/api';
const API_BASE = import.meta.env.VITE_API_URL;
const fetchApi = async <T>(endpoint: string, options: RequestInit): Promise<T> => {
const url = `${API_BASE}${endpoint}`;
const response = await fetch(url, options);
if (!response.ok) {
const error = await response.json().catch(() => ({ detail: 'Unknown API error' }));
throw new Error(`API Error: ${response.status} - ${error.detail || 'Failed to fetch'}`);
}
return response.json() as Promise<T>;
};
export const createAvatar = (data: AvatarCreationRequest): Promise<VideoGenerationResponse> => {
return fetchApi<VideoGenerationResponse>('/generate-avatar', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
};
export const getVideoStatus = (jobId: string): Promise<VideoStatusResponse> => {
return fetchApi<VideoStatusResponse>(`/video-status/${jobId}`, {
method: 'GET',
});
};
export const sendMessage = (data: ChatRequest): Promise<ChatResponse> => {
return fetchApi<ChatResponse>('/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
};
export const getHealthStatus = (): Promise<HealthCheckResponse> => {
// Calls the FastAPI endpoint GET /api/v1/health
return fetchApi<HealthCheckResponse>('/health', {
method: 'GET',
});
};
export const cloneVoice = (userId: string, audioFile: File): Promise<CloneVoiceResponse> => {
const formData = new FormData();
formData.append("audio_file", audioFile);
// Note: No 'Content-Type': 'application/json' header for FormData
return fetchApi<CloneVoiceResponse>(`/clone-voice/${userId}`, {
method: 'POST',
body: formData,
});
};