24 lines
791 B
TypeScript
24 lines
791 B
TypeScript
export const http = {
|
|
async get<T>(url: string, options?: RequestInit): Promise<T> {
|
|
const response = await fetch(url, { ...options, method: 'GET' });
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP GET failed with status ${response.status}`);
|
|
}
|
|
return response.json() as Promise<T>;
|
|
},
|
|
async post<T>(url: string, body?: unknown, options?: RequestInit): Promise<T> {
|
|
const response = await fetch(url, {
|
|
...options,
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...(options?.headers ?? {}),
|
|
},
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP POST failed with status ${response.status}`);
|
|
}
|
|
return response.json() as Promise<T>;
|
|
},
|
|
};
|