import type EventEmitter from "node:events"; import type * as Undici from "undici"; export declare const DEFAULT_TIMEOUT_IN_MILLISECONDS = 300000; export declare const DEFAULT_MAX_REDIRECTS = 10; export declare const DEFAULT_POOL_MAX_CONNECTIONS = 128; export declare const DEFAULT_USER_AGENT = "Hardhat"; export type Dispatcher = Undici.Dispatcher; export type TestDispatcher = Undici.MockAgent; export type Interceptable = Undici.Interceptable; /** * Options to configure the dispatcher. * * @param timeout The timeout in milliseconds. Defaults to {@link DEFAULT_TIMEOUT_IN_MILLISECONDS}. * @param proxy The proxy to use. If not provided, it's resolved from the * `https_proxy`/`HTTPS_PROXY` and `http_proxy`/`HTTP_PROXY` environment * variables, unless `NO_PROXY` excludes the url or it points at loopback. * @param pool Whether to use a pool dispatcher. Defaults to `false`. * @param maxConnections The maximum number of connections to use in the pool. Defaults to {@link DEFAULT_POOL_MAX_CONNECTIONS}. * @param isTestDispatcher Whether to use a test dispatcher. Defaults to `false`. It's highly recommended to use a test dispatcher in tests to avoid hanging tests. */ export interface DispatcherOptions { timeout?: number; proxy?: string; pool?: boolean; maxConnections?: number; isTestDispatcher?: boolean; } /** * Options to configure a request. * * @param queryParams The query parameters to append to the url. * @param extraHeaders Additional headers to include in the request. * @param abortSignal The signal to abort the request. */ export interface RequestOptions { queryParams?: Record; extraHeaders?: Record; abortSignal?: AbortSignal | EventEmitter; } export interface HttpResponse { statusCode: number; body: { json(): Promise; text(): Promise; }; } /** * Performs a HTTP request. * * @param url The url to make the request to. * @param requestOptions The options to configure the request. See {@link RequestOptions}. * @param dispatcherOrDispatcherOptions Either a dispatcher or dispatcher options. See {@link DispatcherOptions}. * @returns An object containing the status code and the response body. The body can be accessed as JSON or text. * `body` can not be consumed twice. For example, calling `text()` after `json()` throws `TypeError`. * @throws ConnectionRefusedError If the connection is refused by the server. * @throws RequestTimeoutError If the request times out. * @throws RequestError If the request fails for any other reason. */ export declare function getRequest(url: string, requestOptions?: RequestOptions, dispatcherOrDispatcherOptions?: Undici.Dispatcher | DispatcherOptions): Promise; /** * Performs a POST request with a JSON body. * * @param url The url to make the request to. * @param body The body of the request, represented as an object. * @param requestOptions The options to configure the request. See {@link RequestOptions}. * @param dispatcherOrDispatcherOptions Either a dispatcher or dispatcher options. See {@link DispatcherOptions}. * @returns An object containing the status code and the response body. The body can be accessed as JSON or text. * `body` can not be consumed twice. For example, calling `text()` after `json()` throws `TypeError`. * @throws ConnectionRefusedError If the connection is refused by the server. * @throws RequestTimeoutError If the request times out. * @throws RequestError If the request fails for any other reason. */ export declare function postJsonRequest(url: string, body: unknown, requestOptions?: RequestOptions, dispatcherOrDispatcherOptions?: Undici.Dispatcher | DispatcherOptions): Promise; /** * Performs a POST request with a form body. * * @param url The url to make the request to. * @param body The body of the request, represented as an object. * @param requestOptions The options to configure the request. See {@link RequestOptions}. * @param dispatcherOrDispatcherOptions Either a dispatcher or dispatcher options. See {@link DispatcherOptions}. * @returns An object containing the status code and the response body. The body can be accessed as JSON or text. * `body` can not be consumed twice. For example, calling `text()` after `json()` throws `TypeError`. * @throws ConnectionRefusedError If the connection is refused by the server. * @throws RequestTimeoutError If the request times out. * @throws RequestError If the request fails for any other reason. */ export declare function postFormRequest(url: string, body: unknown, requestOptions?: RequestOptions, dispatcherOrDispatcherOptions?: Undici.Dispatcher | DispatcherOptions): Promise; /** * Downloads a file from a url to a destination path. * * @param url The url to download from. * @param destination The absolute path to save the file to. * @param requestOptions The options to configure the request. See {@link RequestOptions}. * @param dispatcherOrDispatcherOptions Either a dispatcher or dispatcher options. See {@link DispatcherOptions}. * @throws ConnectionRefusedError If the connection is refused by the server. * @throws RequestTimeoutError If the request times out. * @throws DownloadFailedError If the download fails for any other reason. */ export declare function download(url: string, destination: string, requestOptions?: RequestOptions, dispatcherOrDispatcherOptions?: Undici.Dispatcher | DispatcherOptions): Promise; /** * Creates a dispatcher based on the provided options. * If the `proxy` option is set, or a proxy is configured in the environment for * the given url, it creates a {@link Undici.ProxyAgent} dispatcher. * If the `pool` option is set to `true`, it creates a {@link Undici.Pool} dispatcher. * Otherwise, it creates a basic {@link Undici.Agent} dispatcher. * * A proxy resolved from the environment takes precedence over `pool`, as a * {@link Undici.Pool} is bound to a single origin and can't tunnel. Set * `NO_PROXY` to opt a host out, or pass a {@link Undici.Dispatcher} to the * request helpers to bypass this entirely. * * @param url The url to make requests to. * @param options The options to configure the dispatcher. See {@link DispatcherOptions}. * @returns The configured dispatcher instance. * @throws DispatcherError If the dispatcher can't be created, including when the * proxy configured in the environment isn't a valid url. */ export declare function getDispatcher(url: string, { timeout, proxy, pool, maxConnections, isTestDispatcher, }?: DispatcherOptions): Promise; export declare function getTestDispatcher(options?: { timeout?: number; }): Promise; /** * Determines whether a proxy should be used for a given url. * * Loopback addresses are never proxied, so that a local node stays reachable * when a proxy is configured for everything else. Beyond those, `NO_PROXY` (or * `no_proxy`) is the opt-out: `*` disables proxying entirely, and every other * entry is matched as described in {@link isExcludedByNoProxy}. * * @param url The url to check. * @returns `true` if a proxy should be used for the url, `false` otherwise. */ export declare function shouldUseProxy(url: string): boolean; /** * Determines whether an absolute url is valid. * * @param url The url to check. * @returns `true` if the url is valid, `false` otherwise. */ export declare function isValidUrl(url: string): boolean; /** * Splits the query params out of a url, so they can be merged with other * params and passed as `queryParams` to the request functions. A request * fails if its url has a query string and `queryParams` is also passed. * * Urls without a query string, and invalid urls, are returned unchanged, with * no query params. * * @param url The url to split. * @returns The url without its query string and fragment, and its query * params. If a param is repeated, its last value is kept. */ export declare function splitQueryParams(url: string): { url: string; queryParams: Record; }; /** * Returns the proxy url from environment variables based on the target url. * For HTTPS urls, checks `https_proxy` then `HTTPS_PROXY`. * For HTTP urls, checks `http_proxy` then `HTTP_PROXY`. * Falls back to the other protocol's proxy if none found. * * Empty and whitespace-only values are treated as unset, and the returned value * is trimmed. It isn't validated: use {@link isValidUrl} if you need that. * * @param url The target url to determine proxy for. * @returns The proxy url, or `undefined` if none are set. */ export declare function getProxyUrl(url: string): string | undefined; export { ConnectionRefusedError, DispatcherError, DownloadError, InvalidProxyUrlError, RequestError, RequestTimeoutError, ResponseStatusCodeError, } from "./errors/request.js"; //# sourceMappingURL=request.d.ts.map