62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
import type { ApiResponse } from '$shared/types/common';
|
|
|
|
export class ApiError extends Error {
|
|
constructor(
|
|
public status: number,
|
|
message: string,
|
|
public response?: Response,
|
|
) {
|
|
super(message);
|
|
this.name = 'ApiError';
|
|
}
|
|
}
|
|
|
|
async function request<T>(
|
|
url: string,
|
|
options?: RequestInit,
|
|
): Promise<ApiResponse<T>> {
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...options?.headers,
|
|
},
|
|
...options,
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new ApiError(
|
|
response.status,
|
|
`Request failed: ${response.statusText}`,
|
|
response,
|
|
);
|
|
}
|
|
|
|
const data = await response.json() as T;
|
|
|
|
return {
|
|
data,
|
|
status: response.status,
|
|
};
|
|
}
|
|
|
|
export const api = {
|
|
get: <T>(url: string, options?: RequestInit) => request<T>(url, { ...options, method: 'GET' }),
|
|
|
|
post: <T>(url: string, body?: unknown, options?: RequestInit) =>
|
|
request<T>(url, {
|
|
...options,
|
|
method: 'POST',
|
|
body: JSON.stringify(body),
|
|
}),
|
|
|
|
put: <T>(url: string, body?: unknown, options?: RequestInit) =>
|
|
request<T>(url, {
|
|
...options,
|
|
method: 'PUT',
|
|
body: JSON.stringify(body),
|
|
}),
|
|
|
|
delete: <T>(url: string, options?: RequestInit) =>
|
|
request<T>(url, { ...options, method: 'DELETE' }),
|
|
};
|