UNPKG

@yoroi/common

Version:

The Common package of Yoroi SDK

70 lines 2.31 kB
import { AxiosRequestConfig } from 'axios'; import { Api } from '@yoroi/types'; declare module 'axios' { interface AxiosRequestConfig { metadata?: { startTime: number; duration: number; }; } } type GetRequestConfig = { url: string; method?: 'get'; headers?: Record<string, string>; }; type OtherRequestConfig<D = any> = { url: string; method: 'post' | 'put' | 'delete'; data?: D; headers?: Record<string, string>; }; export type RequestConfig<D = any> = GetRequestConfig | OtherRequestConfig<D>; export type FetchData = <T, D = any>(config: RequestConfig<D>, fetcherConfig?: AxiosRequestConfig<D>) => Promise<Api.Response<T>>; /** * Performs an HTTP request using Axios based on the specified configuration. * This function simplifies making HTTP requests by handling different * request methods and their respective data and headers. * * @param config - The configuration object for the request. * This includes the URL, HTTP method, optional data, and headers. * The type of `config` varies based on the HTTP method: * - For `GET` requests, `data` should not be provided. * - For `POST`, `PUT`, and `DELETE` requests, `data` is optional. * * @returns A `Promise` that resolves to the response data on a successful request * or an error object on failure. The error object includes the HTTP status * code and error message. * * @template T - The expected type of the response data. * @template D - The type of the data to be sent with the request (for `POST`, `PUT`, `DELETE`). * * @example * ```typescript * // Example of a GET request * fetchData<{ someDataType }>({ * url: 'https://example.com/data', * }).then(response => { * // Handle response * }).catch(error => { * // Handle error * }) * ``` * * @example * ```typescript * // Example of a POST request with data * fetchData<{ someDataType }, { somePayloadType }>({ * url: 'https://example.com/data', * method: 'post', * data: {...somePayload} * }).then(response => { * // Handle response * }).catch(error => { * // Handle error * }) * ``` */ export declare const fetchData: FetchData; export {}; //# sourceMappingURL=fetchData.d.ts.map