File size: 6,922 Bytes
fc69e8e | 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 | // Shared API client utility
import type { ApiResponse, ApiError, PaginationParams, ListParams } from '../types';
const DEFAULT_BASE_URL = 'http://localhost:8080/api/v1';
const DEFAULT_TIMEOUT = 30000;
export interface ApiClientOptions {
baseUrl?: string;
timeout?: number;
headers?: Record<string, string>;
getToken?: () => string | null;
onError?: (error: ApiError) => void;
}
export class ApiClient {
private baseUrl: string;
private timeout: number;
private defaultHeaders: Record<string, string>;
private getToken: () => string | null;
private onError: (error: ApiError) => void;
constructor(options: ApiClientOptions = {}) {
this.baseUrl = options.baseUrl || DEFAULT_BASE_URL;
this.timeout = options.timeout || DEFAULT_TIMEOUT;
this.defaultHeaders = {
'Content-Type': 'application/json',
...options.headers,
};
this.getToken = options.getToken || (() => null);
this.onError = options.onError || (() => {});
}
private async request<T>(
method: string,
path: string,
options: RequestInit = {}
): Promise<ApiResponse<T>> {
const url = `${this.baseUrl}${path}`;
const token = this.getToken();
const headers: Record<string, string> = {
...this.defaultHeaders,
...(options.headers as Record<string, string>),
};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
try {
const response = await fetch(url, {
method,
headers,
body: options.body,
signal: controller.signal,
credentials: 'include',
});
clearTimeout(timeoutId);
const data = await response.json().catch(() => ({})) as ApiResponse<T>;
if (!response.ok) {
const error: ApiError = {
code: data.error?.code || 'UNKNOWN_ERROR',
message: data.error?.message || response.statusText,
details: data.error?.details,
status_code: response.status,
};
this.onError(error);
throw error;
}
return data;
} catch (error) {
clearTimeout(timeoutId);
if (error instanceof DOMException && error.name === 'AbortError') {
const timeoutError: ApiError = {
code: 'TIMEOUT',
message: 'Request timed out',
status_code: 408,
};
this.onError(timeoutError);
throw timeoutError;
}
throw error;
}
}
// Convenience methods
async get<T>(path: string, params?: Record<string, unknown>): Promise<ApiResponse<T>> {
const queryString = params ? '?' + new URLSearchParams(params as Record<string, string>).toString() : '';
return this.request<T>('GET', `${path}${queryString}`);
}
async post<T>(path: string, body?: unknown): Promise<ApiResponse<T>> {
return this.request<T>('POST', path, {
body: body ? JSON.stringify(body) : undefined,
});
}
async put<T>(path: string, body?: unknown): Promise<ApiResponse<T>> {
return this.request<T>('PUT', path, {
body: body ? JSON.stringify(body) : undefined,
});
}
async patch<T>(path: string, body?: unknown): Promise<ApiResponse<T>> {
return this.request<T>('PATCH', path, {
body: body ? JSON.stringify(body) : undefined,
});
}
async delete<T>(path: string): Promise<ApiResponse<T>> {
return this.request<T>('DELETE', path);
}
// Pagination helpers
async list<T>(path: string, params?: ListParams): Promise<ApiResponse<T[]>> {
return this.get<T[]>(path, params as Record<string, unknown>);
}
async *paginate<T>(path: string, params?: Omit<ListParams, 'page'>): AsyncGenerator<T[], void, unknown> {
let page = 1;
const pageSize = params?.page_size || 20;
while (true) {
const response = await this.get<T[]>(path, {
...params,
page,
page_size: pageSize,
});
const items = response.data || [];
yield items;
if (items.length < pageSize || !response.meta?.pagination?.has_next) {
break;
}
page++;
}
}
// File upload
async uploadFile<T>(
path: string,
file: File,
onProgress?: (progress: number) => void
): Promise<ApiResponse<T>> {
const formData = new FormData();
formData.append('file', file);
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', (event) => {
if (event.lengthComputable && onProgress) {
onProgress((event.loaded / event.total) * 100);
}
});
xhr.addEventListener('load', () => {
if (xhr.status >= 200 && xhr.status < 300) {
try {
const data = JSON.parse(xhr.responseText) as ApiResponse<T>;
resolve(data);
} catch {
resolve({ success: true, data: xhr.responseText as unknown as T });
}
} else {
try {
const data = JSON.parse(xhr.responseText) as ApiResponse<T>;
const error = data.error || {
code: 'UPLOAD_FAILED',
message: xhr.statusText,
status_code: xhr.status,
};
this.onError(error);
reject(error);
} catch {
const error: ApiError = {
code: 'UPLOAD_FAILED',
message: xhr.statusText,
status_code: xhr.status,
};
this.onError(error);
reject(error);
}
}
});
xhr.addEventListener('error', () => {
const error: ApiError = {
code: 'NETWORK_ERROR',
message: 'Network error during upload',
status_code: 0,
};
this.onError(error);
reject(error);
});
xhr.open('POST', `${this.baseUrl}${path}`);
const token = this.getToken();
if (token) {
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
}
xhr.send(formData);
});
}
// Update base URL
setBaseUrl(url: string): void {
this.baseUrl = url;
}
// Update default headers
setHeaders(headers: Record<string, string>): void {
this.defaultHeaders = { ...this.defaultHeaders, ...headers };
}
}
// Singleton instance
let apiClientInstance: ApiClient | null = null;
export function getApiClient(options?: ApiClientOptions): ApiClient {
if (!apiClientInstance) {
apiClientInstance = new ApiClient(options);
}
return apiClientInstance;
}
export function setApiClient(client: ApiClient): void {
apiClientInstance = client;
}
// React hook for API client (if using React)
// export function useApiClient(): ApiClient {
// const [client] = useState(() => getApiClient());
// return client;
// } |