Spaces:
Sleeping
Sleeping
| import apiClient from './axios-instance'; | |
| import { AxiosRequestConfig, AxiosResponse } from 'axios'; | |
| interface ResponseData<T> { | |
| data: T; | |
| success: boolean; | |
| message: string; | |
| } | |
| /** | |
| * Generic GET request | |
| * @param url - API endpoint | |
| * @param config - Optional Axios config | |
| * @returns Promise with typed response | |
| */ | |
| export async function get<T>(url: string, config?: AxiosRequestConfig): Promise<ResponseData<T>> { | |
| try { | |
| const response: AxiosResponse<T> = await apiClient.get(url, config); | |
| return { | |
| data: response.data, | |
| success: true, | |
| message: 'Data fetched successfully' | |
| }; | |
| } catch (error: any) { | |
| return { | |
| data: {} as T, | |
| success: false, | |
| message: error.response?.data?.message || error.message || 'Failed to fetch data' | |
| }; | |
| } | |
| } | |
| /** | |
| * Generic GET request that returns an array | |
| * @param url - API endpoint | |
| * @param config - Optional Axios config | |
| * @returns Promise with typed array response | |
| */ | |
| export async function getList<T>(url: string, config?: AxiosRequestConfig): Promise<ResponseData<T[]>> { | |
| try { | |
| const response: AxiosResponse<T[]> = await apiClient.get(url, config); | |
| return { | |
| data: response.data, | |
| success: true, | |
| message: 'Data fetched successfully' | |
| }; | |
| } catch (error: any) { | |
| return { | |
| data: [] as T[], | |
| success: false, | |
| message: error.response?.data?.message || error.message || 'Failed to fetch data' | |
| }; | |
| } | |
| } | |
| /** | |
| * Generic POST request | |
| * @param url - API endpoint | |
| * @param data - Request payload | |
| * @param config - Optional Axios config | |
| * @returns Promise with typed response | |
| */ | |
| export async function post<T, D = any>( | |
| url: string, | |
| data: D, | |
| config?: AxiosRequestConfig | |
| ): Promise<ResponseData<T>> { | |
| try { | |
| const response: AxiosResponse<T> = await apiClient.post(url, data, config); | |
| return { | |
| data: response.data, | |
| success: true, | |
| message: 'Operation completed successfully' | |
| }; | |
| } catch (error: any) { | |
| return { | |
| data: {} as T, | |
| success: false, | |
| message: error.response?.data?.message || error.message || 'Operation failed' | |
| }; | |
| } | |
| } | |
| /** | |
| * Generic PUT request | |
| * @param url - API endpoint | |
| * @param data - Request payload | |
| * @param config - Optional Axios config | |
| * @returns Promise with typed response | |
| */ | |
| export async function put<T, D = any>( | |
| url: string, | |
| data: D, | |
| config?: AxiosRequestConfig | |
| ): Promise<ResponseData<T>> { | |
| try { | |
| const response: AxiosResponse<T> = await apiClient.put(url, data, config); | |
| return { | |
| data: response.data, | |
| success: true, | |
| message: 'Update completed successfully' | |
| }; | |
| } catch (error: any) { | |
| return { | |
| data: {} as T, | |
| success: false, | |
| message: error.response?.data?.message || error.message || 'Update failed' | |
| }; | |
| } | |
| } | |
| /** | |
| * Generic PATCH request | |
| * @param url - API endpoint | |
| * @param data - Request payload | |
| * @param config - Optional Axios config | |
| * @returns Promise with typed response | |
| */ | |
| export async function patch<T, D = any>( | |
| url: string, | |
| data: D, | |
| config?: AxiosRequestConfig | |
| ): Promise<ResponseData<T>> { | |
| try { | |
| const response: AxiosResponse<T> = await apiClient.patch(url, data, config); | |
| return { | |
| data: response.data, | |
| success: true, | |
| message: 'Partial update completed successfully' | |
| }; | |
| } catch (error: any) { | |
| return { | |
| data: {} as T, | |
| success: false, | |
| message: error.response?.data?.message || error.message || 'Partial update failed' | |
| }; | |
| } | |
| } | |
| /** | |
| * Generic DELETE request | |
| * @param url - API endpoint | |
| * @param config - Optional Axios config | |
| * @returns Promise with success response | |
| */ | |
| export async function del<T = boolean>(url: string, config?: AxiosRequestConfig): Promise<ResponseData<T>> { | |
| try { | |
| await apiClient.delete(url, config); | |
| return { | |
| data: true as unknown as T, | |
| success: true, | |
| message: 'Delete completed successfully' | |
| }; | |
| } catch (error: any) { | |
| return { | |
| data: false as unknown as T, | |
| success: false, | |
| message: error.response?.data?.message || error.message || 'Delete failed' | |
| }; | |
| } | |
| } | |
| /** | |
| * Handles file uploads | |
| * @param url - API endpoint | |
| * @param file - File to upload | |
| * @param additionalData - Any additional form data | |
| * @param onProgress - Optional progress callback | |
| * @returns Promise with typed response | |
| */ | |
| export async function uploadFile<T>( | |
| url: string, | |
| file: File, | |
| additionalData?: Record<string, any>, | |
| onProgress?: (percentage: number) => void | |
| ): Promise<ResponseData<T>> { | |
| try { | |
| const formData = new FormData(); | |
| formData.append('file', file); | |
| // Add any additional form data | |
| if (additionalData) { | |
| Object.entries(additionalData).forEach(([key, value]) => { | |
| formData.append(key, value); | |
| }); | |
| } | |
| const response = await apiClient.post(url, formData, { | |
| headers: { | |
| 'Content-Type': 'multipart/form-data', | |
| }, | |
| onUploadProgress: onProgress | |
| ? (progressEvent) => { | |
| if (progressEvent.total) { | |
| const percentage = Math.round((progressEvent.loaded * 100) / progressEvent.total); | |
| onProgress(percentage); | |
| } | |
| } | |
| : undefined, | |
| }); | |
| return { | |
| data: response.data, | |
| success: true, | |
| message: 'File uploaded successfully' | |
| }; | |
| } catch (error: any) { | |
| return { | |
| data: {} as T, | |
| success: false, | |
| message: error.response?.data?.message || error.message || 'File upload failed' | |
| }; | |
| } | |
| } |