-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.service.ts
65 lines (54 loc) · 1.93 KB
/
api.service.ts
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
65
// api.service.ts
import { ChatMessage, ChatSession, ApiResponse, ChatbotConfig } from './types';
const API_BASE_URL = 'https://chatgpt-ciplak.web.app/api';
export async function sendChatMessage(sessionId: string, message: string): Promise<ApiResponse<ChatMessage>> {
try {
const response = await fetch(`${API_BASE_URL}/chat`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ sessionId, message }),
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
return { success: true, data: data.message };
} catch (error) {
console.error('Error sending chat message:', error);
return { success: false, error: 'Failed to send message' };
}
}
export async function getChatSession(sessionId: string): Promise<ApiResponse<ChatSession>> {
try {
const response = await fetch(`${API_BASE_URL}/session/${sessionId}`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
return { success: true, data: data.session };
} catch (error) {
console.error('Error fetching chat session:', error);
return { success: false, error: 'Failed to fetch chat session' };
}
}
export async function updateChatbotConfig(config: Partial<ChatbotConfig>): Promise<ApiResponse<ChatbotConfig>> {
try {
const response = await fetch(`${API_BASE_URL}/config`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(config),
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
return { success: true, data: data.config };
} catch (error) {
console.error('Error updating chatbot config:', error);
return { success: false, error: 'Failed to update chatbot configuration' };
}
}