File size: 5,784 Bytes
1fff71f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Base API client with fetch wrapper
import {
	API_URL,
	API_TIMEOUT,
	HEADER_AUTHORIZATION,
	HEADER_USER_ID,
	HEADER_CONTENT_TYPE,
	MAX_RETRIES,
	RETRY_DELAY
} from '../utils/constants';
import type {
	Session,
	SessionMetadata,
	ListSessionsResponse,
	CreateSessionRequest,
	CreateSessionResponse,
	SendMessageRequest,
	SendMessageResponse,
	UserProfile,
	APIError,
	ComparisonResult
} from '../types/api';
import type { ClientError } from '../types/client';

class APIClient {
	private baseURL: string;
	private token: string | null = null;
	private userId: string | null = null;

	constructor(baseURL: string = API_URL) {
		this.baseURL = baseURL;
	}

	/**
	 * Set authentication token and user ID
	 */
	setAuth(token: string, userId: string): void {
		this.token = token;
		this.userId = userId;
	}

	/**
	 * Clear authentication
	 */
	clearAuth(): void {
		this.token = null;
		this.userId = null;
	}

	/**
	 * Make HTTP request with retry logic and timeout
	 */
	private async request<T>(
		endpoint: string,
		options: RequestInit = {},
		retries: number = MAX_RETRIES
	): Promise<T> {
		const url = `${this.baseURL}${endpoint}`;

		// Add authentication headers
		const headers: Record<string, string> = {
			[HEADER_CONTENT_TYPE]: 'application/json'
		};

		// Merge existing headers
		if (options.headers) {
			Object.entries(options.headers).forEach(([key, value]) => {
				if (typeof value === 'string') {
					headers[key] = value;
				}
			});
		}

		if (this.token) {
			headers[HEADER_AUTHORIZATION] = `Bearer ${this.token}`;
		}

		if (this.userId && !endpoint.includes('/user/profile')) {
			headers[HEADER_USER_ID] = this.userId;
		}

		// Create abort controller for timeout
		const controller = new AbortController();
		const timeoutId = setTimeout(() => controller.abort(), API_TIMEOUT);

		try {
			const response = await fetch(url, {
				...options,
				headers,
				signal: controller.signal
			});

			clearTimeout(timeoutId);

			if (!response.ok) {
				const error: APIError = await response.json();
				throw this.createClientError(error, response.status, false);
			}

			// Handle 204 No Content
			if (response.status === 204) {
				return {} as T;
			}

			return await response.json();
		} catch (error) {
			clearTimeout(timeoutId);

			// Handle abort/timeout
			if (error instanceof Error && error.name === 'AbortError') {
				if (retries > 0) {
					await this.delay(RETRY_DELAY);
					return this.request<T>(endpoint, options, retries - 1);
				}
				throw this.createClientError(
					{ error: 'Request timeout', status: 408 },
					408,
					true
				);
			}

			// Handle network errors
			if (error instanceof TypeError) {
				if (retries > 0) {
					await this.delay(RETRY_DELAY);
					return this.request<T>(endpoint, options, retries - 1);
				}
				throw this.createClientError(
					{ error: 'Network error', status: 0 },
					0,
					true
				);
			}

			// Re-throw client errors
			if (this.isClientError(error)) {
				throw error;
			}

			// Unknown error
			throw this.createClientError({ error: String(error), status: 500 }, 500, false);
		}
	}

	/**
	 * Create typed client error
	 */
	private createClientError(apiError: APIError, status: number, retryable: boolean): ClientError {
		let type: ClientError['type'] = 'unknown';

		if (status === 401 || status === 403) {
			type = 'auth';
		} else if (status >= 400 && status < 500) {
			type = 'validation';
		} else if (status >= 500) {
			type = 'server';
		} else if (status === 0 || status === 408) {
			type = 'network';
		}

		return {
			message: apiError.error,
			type,
			cause: apiError,
			retryable
		};
	}

	/**
	 * Type guard for ClientError
	 */
	private isClientError(error: unknown): error is ClientError {
		return (
			typeof error === 'object' &&
			error !== null &&
			'message' in error &&
			'type' in error &&
			'retryable' in error
		);
	}

	/**
	 * Delay utility for retry logic
	 */
	private delay(ms: number): Promise<void> {
		return new Promise((resolve) => setTimeout(resolve, ms));
	}

	// Session Management Methods
	async createSession(title: string): Promise<CreateSessionResponse> {
		const request: CreateSessionRequest = {
			title,
			user_id: this.userId || ''
		};

		return this.request<CreateSessionResponse>('/v1/sessions', {
			method: 'POST',
			body: JSON.stringify(request)
		});
	}

	async listSessions(): Promise<SessionMetadata[]> {
		const response = await this.request<ListSessionsResponse>('/v1/sessions', {
			method: 'GET'
		});
		return response.sessions;
	}

	async getSession(sessionId: string): Promise<Session> {
		return this.request<Session>(`/v1/sessions/${sessionId}`, {
			method: 'GET'
		});
	}

	async updateSession(sessionId: string, isReference: boolean): Promise<CreateSessionResponse> {
		return this.request<CreateSessionResponse>(`/v1/sessions/${sessionId}`, {
			method: 'PATCH',
			body: JSON.stringify({ is_reference: isReference })
		});
	}

	async deleteSession(sessionId: string): Promise<void> {
		await this.request<void>(`/v1/sessions/${sessionId}`, {
			method: 'DELETE'
		});
	}

	async sendMessage(
		sessionId: string,
		request: SendMessageRequest
	): Promise<SendMessageResponse> {
		return this.request<SendMessageResponse>(`/v1/sessions/${sessionId}/messages`, {
			method: 'POST',
			body: JSON.stringify(request)
		});
	}

	async compareSession(sessionId: string, referenceSessionId: string): Promise<ComparisonResult> {
		return this.request<ComparisonResult>(
			`/v1/sessions/${sessionId}/compare?reference_id=${referenceSessionId}`,
			{
				method: 'GET'
			}
		);
	}

	async getUserProfile(): Promise<UserProfile> {
		return this.request<UserProfile>('/v1/user/profile', {
			method: 'GET'
		});
	}
}

// Export singleton instance
export const apiClient = new APIClient();