|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- /**
- * @deprecated This method is deprecated.
- */
- export const useFetch = () => {
- interface FetchConfig {
- baseUrl: string;
- headers: HeadersInit;
- }
-
- interface ApiResponse<T> {
- data: T;
- status: number;
- }
-
- const DEFAULT_CONFIG: FetchConfig = {
- baseUrl: '/api/v1',
- headers: {
- 'Content-Type': 'application/json'
- }
- };
-
- const buildQueryString = (params?: Record<string, string | number>): string => {
- if (!params) return '';
- return '?' + Object.entries(params)
- .map(([key, value]) => `${key}=${value}`)
- .join('&');
- };
-
- /**
- * @deprecated This function is deprecated.
- */
- const createRequest = async <T>(
- endpoint: string,
- method: 'GET' | 'POST',
- options: RequestInit = {}
- ): Promise<ApiResponse<T>> => {
- const response = await fetch(`${DEFAULT_CONFIG.baseUrl}${endpoint}`, {
- method,
- mode: 'cors',
- headers: new Headers(DEFAULT_CONFIG.headers),
- ...options
- });
-
- const data = await response.json();
- return {data, status: response.status};
- };
-
- /**
- * @deprecated This function is deprecated.
- */
- const get = async <T>(
- endpoint: string,
- queryParams?: Record<string, string | number>
- ): Promise<T> => {
- const query = buildQueryString(queryParams);
- const response = await createRequest<T>(`${endpoint}${query}`, 'GET');
- return response.data;
- };
-
- /**
- * @deprecated This function is deprecated.
- */
- const post = async <T, U>(endpoint: string, payload: T): Promise<U> => {
- const response = await createRequest<U>(endpoint, 'POST', {
- body: JSON.stringify(payload)
- });
- return response.data;
- };
-
- return { get, post }
- }
|