Files
ocs-portal-web/services/api/api-client.ts
2025-04-15 13:56:54 +07:00

20 lines
474 B
TypeScript

export const apiClient = async <T>(
url: string,
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
body?: any
): Promise<T> => {
const res = await fetch(url, {
method,
headers: { 'Content-Type': 'application/json' },
body: body ? JSON.stringify(body) : undefined,
});
if (!res.ok) {
const errorData = await res.json();
throw new Error(errorData.message || 'Terjadi kesalahan');
}
return res.json();
};