File size: 1,709 Bytes
c59d808 |
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 |
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8080";
export class ApiError extends Error {
constructor(
message: string,
public statusCode: number,
public code?: string
) {
super(message);
this.name = "ApiError";
}
}
export async function apiRequest<T>(
endpoint: string,
options: RequestInit = {}
): Promise<T> {
const url = `${API_BASE_URL}${endpoint}`;
const defaultHeaders = {
"Content-Type": "application/json",
};
const config: RequestInit = {
...options,
headers: {
...defaultHeaders,
...options.headers,
},
};
try {
const response = await fetch(url, config);
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new ApiError(
errorData.message || `HTTP error! status: ${response.status}`,
response.status,
errorData.code
);
}
return await response.json();
} catch (error) {
if (error instanceof ApiError) {
throw error;
}
throw new ApiError("Network error", 0);
}
}
export const api = {
get: <T>(endpoint: string, headers?: HeadersInit) =>
apiRequest<T>(endpoint, { method: "GET", headers }),
post: <T>(endpoint: string, data?: unknown, headers?: HeadersInit) =>
apiRequest<T>(endpoint, {
method: "POST",
body: JSON.stringify(data),
headers,
}),
put: <T>(endpoint: string, data?: unknown, headers?: HeadersInit) =>
apiRequest<T>(endpoint, {
method: "PUT",
body: JSON.stringify(data),
headers,
}),
delete: <T>(endpoint: string, headers?: HeadersInit) =>
apiRequest<T>(endpoint, { method: "DELETE", headers }),
};
|