feat(api): create api instance
This commit is contained in:
61
src/shared/api/api.ts
Normal file
61
src/shared/api/api.ts
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
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' }),
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user