import { HTTPError } from '../errors'; import { URLValidator } from '../validators'; import type { HttpClientConfig, InterceptorManagerMap } from './types'; /** * Strapi Client's HTTP Client * * Provides methods for configuring the base URL, timeout, interceptors, headers, * and for performing HTTP requests with automatic URL validation. */ export declare class HttpClient { readonly interceptors: InterceptorManagerMap; private _baseURL; private _timeout; private readonly _headers; private readonly _urlValidator; constructor(config: HttpClientConfig, urlValidator?: URLValidator); /** * Gets the currently set base URL. * * @returns The base URL used for HTTP requests. */ get baseURL(): string; /** * Gets the request timeout. * * @returns The timeout used for HTTP requests. */ get timeout(): number; /** * Gets the request headers. * * @returns The headers used for HTTP requests. */ get headers(): Record; /** * Sets a new base URL for the HTTP client and validates it. * * @param url - The new base URL to set. * * @returns The HttpClient instance for chaining. * * @throws {URLParsingError} If the URL cannot be parsed. * * @example * const client = new HttpClient('http://example.com'); * * client.setBaseURL('http://newexample.com'); */ setBaseURL(url: string): this; /** * Sets a new timeout value for the HTTP client and validates it. * * @param timeout The new timeout to set. * * @returns The HttpClient instance for chaining. * * @throws {TypeError} If the timeout is not a safe integer. * * @example * const client = new HttpClient({ baseURL: 'http://example.com' }); * * client.setTimeout(3000); */ setTimeout(timeout: number): this; /** * Sends an HTTP request to the specified relative path with the provided options, applying interceptors, * global headers, and a timeout mechanism. * * The `request` method handles low-level HTTP transactions and should be used * internally or via helper methods (`get`, `post`, `put`, `delete`). * * @param path - The relative URL (path) to use for the HTTP request. * This shouldn't include the base URL as it is automatically appended. * @param [init] - (Optional) The request initialization options, following the `RequestInit` interface. * This can include headers, method, body, signal, and other fetch options. * * @returns A `Promise` resolving with the HTTP response after being processed by response interceptors. * The response contains the server's reply to the HTTP call, which consumers can handle as needed. * * @throws {DOMException} If the request is aborted due to exceeding the timeout limit. * * @example * // Example usage of the request method to send a GET request: * const client = new HttpClient({ baseURL: 'https://api.example.com', timeout: 5000 }); * * try { * const response = await client.request('/users', { method: 'GET' }); * const data = await response.json(); * console.log(data); * } catch (error) { * console.error('Request failed:', error); * } * * @example * // Sending a POST request with a JSON payload: * const response = await client.request('/users', { * method: 'POST', * headers: { 'Content-Type': 'application/json' }, * body: JSON.stringify({ name: 'John Doe', email: 'john@example.com' }), * }); * console.log(await response.json()); * * @example * // Handling timeout: * try { * const response = await client.request('/slow-endpoint', { method: 'GET' }); * console.log(await response.text()); * } catch (error) { * if (error.name === 'AbortError') { * console.error('Request aborted due to timeout.'); * } else { * console.error('Request failed:', error); * } * } * * @additional-information * - **Global Headers**: The method appends headers defined in the `_headers` property of the `HttpClient`. * - **Interceptors**: Request and response interceptors are defined in the `interceptors` property * and are executed to modify or handle request and response lifecycle logic. * - **Timeout**: The timeout duration is specified when configuring the `HttpClient` instance. It overrides * any abort signals provided by the user. * - **Dependencies**: * - Relies on the `fetch` API to execute the HTTP request. * - `PathFormatter` is used to sanitize the relative path. * * @see {@link HttpClient.interceptors} for adding custom interceptors. * @see {@link HttpClient.baseURL} for setting the base URL of requests. * @see {@link PathFormatter.format} for formatting and sanitizing URL paths. */ request(path: string, init?: RequestInit): Promise; /** * Makes a direct HTTP request to the specified URL or request input using the Fetch API. * * This method provides a low-level interface to send HTTP requests using the global Fetch API. * It is protected and intended to be used internally by the `HttpClient` class. * * @param input - The target URL or a `RequestInfo` instance representing the request to be made. * @param [init] - (Optional) Configuration options for the request, compatible with the `RequestInit` interface. * * @returns A `Promise` that resolves with the raw HTTP Response returned by the server. * * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API Fetch API Documentation} */ protected fetch(input: RequestInfo | URL, init?: RequestInit): Promise; /** * Sends an HTTP GET request to the specified relative path. * * This method is a shortcut for making GET requests with the `request` method. * * @param path - The relative URL (path) to send the request to. * The base URL is automatically appended. * * @param [init] - (Optional) Additional request options such as headers or signals. * * @returns A `Promise` that resolves to the HTTP Response. * It contains the server's reply to the GET request. * * @example * // Example usage of the get method: * const client = new HttpClient({ baseURL: 'https://api.example.com' }); * const response = await client.get('/users'); * const data = await response.json(); * console.log(data); */ get(path: string, init?: RequestInit): Promise; /** * Sends an HTTP POST request to the specified relative path with an optional request body. * * This method is a shortcut for making POST requests with the `request` method. * * @param path - The relative URL (path) to send the request to. * The base URL is automatically appended. * * @param [body] - (Optional) The content to be sent in the request body, such as JSON or form data. * * @param [init] - (Optional) Additional request options such as headers or signals. * * @returns A `Promise` that resolves to the HTTP Response. * It contains the server's reply to the POST request. * * @example * // Example usage of the post method: * const client = new HttpClient({ * baseURL: 'https://api.example.com', * headers: { 'Content-Type': 'application/json' } * }); * * const response = await client.post('/users', JSON.stringify({ name: 'John' })); * const data = await response.json(); * * console.log(data); */ post(path: string, body?: BodyInit, init?: RequestInit): Promise; /** * Sends an HTTP PUT request to the specified relative path with an optional request body. * * This method is a shortcut for making PUT requests with the `request` method. * * @param path - The relative URL (path) to send the request to. The base URL is automatically appended. * @param [body] - (Optional) The content to be sent in the request body, such as JSON or form data. * @param [init] - (Optional) Additional request options such as headers or signals. * * @returns A `Promise` that resolves to the HTTP Response. It contains the server's reply to the PUT request. * * @example * // Example usage of the put method: * const client = new HttpClient({ * baseURL: 'https://api.example.com', * headers: { 'Content-Type': 'application/json' } * }); * * const response = await client.put('/users/1', JSON.stringify({ name: 'John Updated' })); * const data = await response.json(); * * console.log(data); */ put(path: string, body?: BodyInit, init?: RequestInit): Promise; /** * Sends an HTTP DELETE request to the specified relative path. * * This method is a shortcut for making DELETE requests with the `request` method. * * @param path - The relative URL (path) to send the request to. * The base URL is automatically appended. * * @param [init] - (Optional) Additional request options such as headers or signals. * * @returns A `Promise` that resolves to the HTTP Response. * It contains the server's reply to the DELETE request. * * @example * // Example usage of the delete method: * const client = new HttpClient({ baseURL: 'https://api.example.com' }); * * const response = await client.delete('/users/1'); * if (response.ok) { * console.log('User deleted successfully.'); * } else { * console.error('Failed to delete user.'); * } */ delete(path: string, init?: RequestInit): Promise; /** * Creates a new instance of `HttpClient`, inheriting the current instance's configuration * with the option to inherit request and response interceptors. * * This method is designed to enable the creation of a modified or isolated `HttpClient` instance * that preserves the prototype chain, allowing better extensibility for subclasses or testing setups. * * Defaults to inheriting all interceptors unless specified otherwise. * * @param [config={}] - An optional configuration object to override the client's existing settings. * If a property is not provided, it falls back to the current instance's configuration. * * @param [inheritInterceptors=true] - Whether to inherit the current instance's request and response interceptors. * If `false`, the new instance won't include any interceptors. * * @returns A new `HttpClient` instance with either the inherited or overridden configuration and interceptors. * * @throws {Error} If the created instance is not an instance of `HttpClient` (unexpected scenario). * * @example * // Creating a new client with default settings * const newClient = httpClient.create(); * * // Creating a new client with custom settings and no inherited interceptors * const customClient = httpClient.create({ baseURL: 'https://api.example.com' }, false); * * @example * // Using the new instance for isolated requests * const client = httpClient.create({ headers: { Authorization: 'Bearer token' } }); * const response = await client.get('/data'); * * @see {@link HttpClientConfig} for details on valid configuration options. */ create(config?: Partial, inheritInterceptors?: boolean): HttpClient; /** * Maps an HTTP response's status code to a specific HTTP error class. * * @param response - The HTTP response object obtained from a failed HTTP request, * which contains the status code and reason for failure. * @param request - The original HTTP request object that resulted in the error response. * * @returns A specific subclass instance of HTTPError based on the response status code. * * @throws {HTTPError} or any of its subclass. * * @see {@link StatusCode} for all possible HTTP status codes and their meanings. */ static mapResponseToHTTPError(response: Response, request: Request): HTTPError; }