@duongtrungnguyen/next-helper
Version:
Helper library for Next.js 15
71 lines • 3.44 kB
TypeScript
/**
* A class to handle HTTP requests.
*/
export declare class HttpClient {
private baseUrl;
/**
* Creates an instance of HttpClient.
* @param baseUrl - The base URL for the HTTP requests.
*/
constructor(baseUrl: string);
/**
* Makes an HTTP request.
* @template T - The expected response type.
* @param endpoint - The endpoint to send the request to.
* @param method - The HTTP method to use.
* @param body - The request body.
* @param config - Additional request configurations.
* @returns A promise that resolves to the response data or the raw response.
* @throws An error if the response is not ok.
*/
private request;
/**
* Serializes the request body into a format suitable for HTTP requests.
*
* @param body - The request body to be serialized. It can be of type `FormData`, `Blob`, `ArrayBuffer`, `URLSearchParams`, or a plain object.
* @returns An object containing the serialized body and appropriate headers. If the body is `FormData`, `Blob`, or `ArrayBuffer`, it returns the body as is.
* If the body is `URLSearchParams`, it returns the body with `Content-Type` set to `application/x-www-form-urlencoded`.
* If the body is a plain object, it returns the JSON stringified body with `Content-Type` set to `application/json`.
*/
private serialize;
/**
* Makes a GET request.
* @template T - The expected response type.
* @param endpoint - The endpoint to send the request to.
* @param config - Additional request configurations.
* @returns A promise that resolves to the response data or the raw response.
*/
get<T>(endpoint: string, config?: RequestConfigs<false>): Promise<T>;
get(endpoint: string, config?: RequestConfigs<true>): Promise<Response>;
/**
* Makes a POST request.
* @template T - The expected response type.
* @param endpoint - The endpoint to send the request to.
* @param body - The request body.
* @param config - Additional request configurations.
* @returns A promise that resolves to the response data or the raw response.
*/
post<T>(endpoint: string, body?: RequestBody, config?: RequestConfigs<false>): Promise<T>;
post(endpoint: string, body?: RequestBody, config?: RequestConfigs<true>): Promise<Response>;
/**
* Makes a PUT request.
* @template T - The expected response type.
* @param endpoint - The endpoint to send the request to.
* @param body - The request body.
* @param config - Additional request configurations.
* @returns A promise that resolves to the response data or the raw response.
*/
put<T>(endpoint: string, body?: RequestBody, config?: RequestConfigs<false>): Promise<T>;
put(endpoint: string, body?: RequestBody, config?: RequestConfigs<true>): Promise<Response>;
/**
* Makes a DELETE request.
* @template T - The expected response type.
* @param endpoint - The endpoint to send the request to.
* @param config - Additional request configurations.
* @returns A promise that resolves to the response data or the raw response.
*/
delete<T>(endpoint: string, config?: RequestConfigs<false>): Promise<T>;
delete(endpoint: string, config?: RequestConfigs<true>): Promise<Response>;
}
export declare const httpClient: HttpClient;
//# sourceMappingURL=http.d.ts.map