2026-03-02 22:19:13 +03:00
|
|
|
/**
|
|
|
|
|
* HTTP API client with error handling
|
|
|
|
|
*
|
|
|
|
|
* Provides a typed wrapper around fetch for JSON APIs.
|
|
|
|
|
* Automatically handles JSON serialization and error responses.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```ts
|
|
|
|
|
* import { api } from '$shared/api';
|
|
|
|
|
*
|
|
|
|
|
* // GET request
|
|
|
|
|
* const users = await api.get<User[]>('/api/users');
|
|
|
|
|
*
|
|
|
|
|
* // POST request
|
|
|
|
|
* const newUser = await api.post<User>('/api/users', { name: 'Alice' });
|
|
|
|
|
*
|
|
|
|
|
* // Error handling
|
|
|
|
|
* try {
|
|
|
|
|
* const data = await api.get('/api/data');
|
|
|
|
|
* } catch (error) {
|
|
|
|
|
* if (error instanceof ApiError) {
|
|
|
|
|
* console.error(error.status, error.message);
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
|
2026-01-02 11:15:20 +03:00
|
|
|
import type { ApiResponse } from '$shared/types/common';
|
|
|
|
|
|
2026-03-02 22:19:13 +03:00
|
|
|
/**
|
|
|
|
|
* Custom error class for API failures
|
|
|
|
|
*
|
|
|
|
|
* Includes HTTP status code and the original Response object
|
|
|
|
|
* for debugging and error handling.
|
|
|
|
|
*/
|
2026-01-02 11:15:20 +03:00
|
|
|
export class ApiError extends Error {
|
2026-03-02 22:19:13 +03:00
|
|
|
/**
|
|
|
|
|
* Creates a new API error
|
|
|
|
|
* @param status - HTTP status code
|
|
|
|
|
* @param message - Error message
|
|
|
|
|
* @param response - Original fetch Response object
|
|
|
|
|
*/
|
2026-01-02 11:15:20 +03:00
|
|
|
constructor(
|
2026-03-02 22:19:13 +03:00
|
|
|
/** HTTP status code */
|
2026-01-02 11:15:20 +03:00
|
|
|
public status: number,
|
|
|
|
|
message: string,
|
2026-03-02 22:19:13 +03:00
|
|
|
/** Original Response object for inspection */
|
2026-01-02 11:15:20 +03:00
|
|
|
public response?: Response,
|
|
|
|
|
) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = 'ApiError';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 22:19:13 +03:00
|
|
|
/**
|
|
|
|
|
* Internal request handler
|
|
|
|
|
*
|
|
|
|
|
* Performs fetch with JSON headers and throws ApiError on failure.
|
|
|
|
|
*
|
|
|
|
|
* @param url - Request URL
|
|
|
|
|
* @param options - Fetch options (method, headers, body, etc.)
|
|
|
|
|
* @returns Response data and status code
|
|
|
|
|
* @throws ApiError when response is not OK
|
|
|
|
|
*/
|
2026-01-02 11:15:20 +03:00
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 22:19:13 +03:00
|
|
|
/**
|
|
|
|
|
* API client methods
|
|
|
|
|
*
|
|
|
|
|
* Provides typed methods for common HTTP verbs.
|
|
|
|
|
* All methods return ApiResponse with data and status.
|
|
|
|
|
*/
|
2026-01-02 11:15:20 +03:00
|
|
|
export const api = {
|
2026-03-02 22:19:13 +03:00
|
|
|
/**
|
|
|
|
|
* Performs a GET request
|
|
|
|
|
* @param url - Request URL
|
|
|
|
|
* @param options - Additional fetch options
|
|
|
|
|
* @returns Response data
|
|
|
|
|
*/
|
2026-01-02 11:15:20 +03:00
|
|
|
get: <T>(url: string, options?: RequestInit) => request<T>(url, { ...options, method: 'GET' }),
|
|
|
|
|
|
2026-03-02 22:19:13 +03:00
|
|
|
/**
|
|
|
|
|
* Performs a POST request with JSON body
|
|
|
|
|
* @param url - Request URL
|
|
|
|
|
* @param body - Request body (will be JSON stringified)
|
|
|
|
|
* @param options - Additional fetch options
|
|
|
|
|
* @returns Response data
|
|
|
|
|
*/
|
2026-01-02 11:15:20 +03:00
|
|
|
post: <T>(url: string, body?: unknown, options?: RequestInit) =>
|
|
|
|
|
request<T>(url, {
|
|
|
|
|
...options,
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
|
}),
|
|
|
|
|
|
2026-03-02 22:19:13 +03:00
|
|
|
/**
|
|
|
|
|
* Performs a PUT request with JSON body
|
|
|
|
|
* @param url - Request URL
|
|
|
|
|
* @param body - Request body (will be JSON stringified)
|
|
|
|
|
* @param options - Additional fetch options
|
|
|
|
|
* @returns Response data
|
|
|
|
|
*/
|
2026-01-02 11:15:20 +03:00
|
|
|
put: <T>(url: string, body?: unknown, options?: RequestInit) =>
|
|
|
|
|
request<T>(url, {
|
|
|
|
|
...options,
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
|
}),
|
|
|
|
|
|
2026-03-02 22:19:13 +03:00
|
|
|
/**
|
|
|
|
|
* Performs a DELETE request
|
|
|
|
|
* @param url - Request URL
|
|
|
|
|
* @param options - Additional fetch options
|
|
|
|
|
* @returns Response data
|
|
|
|
|
*/
|
2026-01-30 01:09:39 +03:00
|
|
|
delete: <T>(url: string, options?: RequestInit) => request<T>(url, { ...options, method: 'DELETE' }),
|
2026-01-02 11:15:20 +03:00
|
|
|
};
|