@tmlmobilidade/utils
Version:
This package provides a collection of common utility functions used across projects within the organization.
63 lines (62 loc) • 2.34 kB
TypeScript
export interface HttpResponse<T> {
data: null | T;
error: null | string;
status: number;
}
/**
* Fetches data from a URL with configurable HTTP method, body, headers, and options.
* @param url - The URL to fetch from
* @param method - The HTTP method to use (DELETE, GET, POST, PUT). Defaults to GET.
* @param body - Optional request body data
* @param headers - Optional request headers
* @param options - Optional fetch options (excluding body, headers, method)
* @returns Promise resolving to HttpResponse containing data, error and status
* @example
* ```ts
* // GET request
* const response = await fetchData<User>('/api/users/123');
*
* // POST request with body
* const response = await fetchData<User>(
* '/api/users',
* 'POST',
* { name: 'John', email: 'john@example.com' }
* );
* ```
*/
export declare function fetchData<T>(url: string, method?: 'DELETE' | 'GET' | 'POST' | 'PUT', body?: unknown, headers?: Record<string, string>, options?: Omit<RequestInit, 'body' | 'headers' | 'method'>): Promise<HttpResponse<T>>;
/**
* Sends a multipart form data request to a URL.
* @param url - The URL to send the request to
* @param formData - The FormData object containing the multipart form data
* @returns Promise resolving to HttpResponse containing data, error and status
* @example
* ```ts
* const formData = new FormData();
* formData.append('file', fileBlob);
* formData.append('name', 'profile.jpg');
* const response = await multipartFetch('/api/upload', formData);
* ```
*/
export declare function multipartFetch<T>(url: string, formData: FormData): Promise<HttpResponse<T>>;
/**
* Uploads a file to a URL using multipart form data.
* @param url - The URL to send the request to
* @param file - The file to upload
* @returns Promise resolving to HttpResponse containing data, error and status
* @example
* ```ts
* const response = await uploadFile('/api/upload', file);
* ```
*/
export declare function uploadFile<T>(url: string, file: File): Promise<HttpResponse<T>>;
/**
* Fetches data from a URL using the SWR fetcher function.
* @param url - The URL to fetch from
* @returns Promise resolving to the fetched data
* @example
* ```ts
* const data = await swrFetcher('/api/users/123');
* ```
*/
export declare const swrFetcher: <T>(url: string) => Promise<T>;