UNPKG

uniqhtt

Version:

A sophisticated, enterprise-grade HTTP client for Node.js, Web, and edge environments featuring intelligent cookie management, advanced web crawling capabilities, comprehensive automation tools, and a new TypeScript-first Pro API with HTTP/2, HTTP/3, stre

1,218 lines (1,217 loc) 155 kB
import UniqFormData from 'form-data'; import uniqFormData from 'form-data'; import { Agent as httpsAgent } from 'https'; import { Blob as Blob$1, BlobOptions, Buffer } from 'node:buffer'; import { Agent as httpAgent, IncomingHttpHeaders, OutgoingHttpHeaders, RequestOptions } from 'node:http'; import PQueue from 'p-queue'; import { Options, QueueAddOptions } from 'p-queue'; import PriorityQueue from 'p-queue/dist/priority-queue'; import { Cookie as TouchCookie, CookieJar as TouchCookieJar, CreateCookieJarOptions, CreateCookieOptions, Nullable, Store } from 'tough-cookie'; import { YqCacher } from 'yq-store/file-adapter'; export interface SerializedCookie { key: string; value: string; expires?: string; maxAge?: number | "Infinity" | "-Infinity"; domain?: string; path?: string; secure?: boolean; hostOnly?: boolean; creation?: string; lastAccessed?: string; [key: string]: unknown; } export declare class Cookie extends TouchCookie { constructor(options?: CreateCookieOptions); private getExpires; toNetscapeFormat(): string; toSetCookieString(): string; /** * Retrieves the complete URL from the cookie object * @returns {string | undefined} The complete URL including protocol, domain and path. Returns undefined if domain is not set * @example * const cookie = new Cookie({ * domain: "example.com", * path: "/path", * secure: true * }); * cookie.getURL(); // Returns: "https://example.com/path" */ getURL(): string | undefined; } export declare class CookieJar extends TouchCookieJar { constructor(store?: Nullable<Store>, options?: CreateCookieJarOptions | boolean); private generateCookies; cookies(): Cookies; parseResponseCookies(cookies: Cookie[]): Cookies; static toNetscapeCookie(cookies: Cookie[] | SerializedCookie[]): string; static toCookieString(cookies: Cookie[] | SerializedCookie[]): string; toCookieString(): string; toNetscapeCookie(): string; toArray(): Cookie[]; toSetCookies(): string[]; toSerializedCookies(): SerializedCookie[]; setCookiesSync(setCookieArray: string[]): Cookies; setCookiesSync(setCookieArray: string[], url: string): Cookies; setCookiesSync(cookiesString: string): Cookies; setCookiesSync(cookiesString: string, url: string): Cookies; setCookiesSync(serializedCookies: SerializedCookie[]): Cookies; setCookiesSync(serializedCookies: SerializedCookie[], url: string): Cookies; setCookiesSync(cookieArray: Cookie[]): Cookies; setCookiesSync(cookieArray: Cookie[], url: string): Cookies; private splitSetCookiesString; private getUrlFromCookie; private parseNetscapeCookies; /** * Converts Netscape cookie format to an array of Set-Cookie header strings * * @param netscapeCookieText - Netscape format cookie string * @returns Array of Set-Cookie header strings */ static netscapeCookiesToSetCookieArray(netscapeCookieText: string): string[]; } export interface Cookies { array: Cookie[]; serialized: SerializedCookie[]; netscape: string; string: string; setCookiesString: string[]; } export interface UniqhttResponse<T = any> { data: T; status: number; statusText: string; finalUrl: string; cookies: Cookies; headers: IncomingHttpHeaders; contentType: string | null; contentLength: number | undefined; urls: string[]; config: UniqhttConfig; httpVersion?: string; } export interface UniqhttError extends Error { response: UniqhttResponse; } export interface DownloadResponse<T = any> { data: T; status: number; statusText: string; finalUrl: string; cookies: { array: Cookie[]; string: string; netscape: string; }; array: Cookie[]; string: string; netscape: string; headers: { [p: string]: string; }; contentType: string | null; fileName: string; totalTime: string; downloadSpeed: string; size: string; config?: HttpConfig; } export type HttpConfig = Omit<RequestInit, "body" | "signal" | "follow" | "referrer" | "credentials" | "highWaterMark" | "method" | "counter" | "insecureHTTPParser" | "size" | "agent" | "redirect"> & { /** Cookies to be sent with the request. Can be an array of cookie objects, serialized cookies, or a string. */ cookies?: Cookie[] | SerializedCookie[] | string | string[]; /** Determines whether to include cookies in the request. Defaults to true. */ enableCookieJar?: boolean; useHTTP2?: boolean; /** Query parameters to be appended to the URL. */ params?: { [key: string]: string | number | boolean; }; /** If true, starts a new request session and clear cookies. */ startNewRequest?: boolean; /** If true, returns the response as a buffer. */ returnBuffer?: boolean; /** Custom headers for the request. */ headers?: OutgoingHttpHeaders | Headers | Record<string, string>; /** JSON payload for the request body. */ json?: Record<string | number, any>; /** Form parameters to be sent in the request body. */ form_params?: Record<string, any>; /** If true, prevents the following redirects. */ dontFollowRedirects?: boolean; /** If true, sends the request without a Content-Type header. */ withoutContentType?: boolean; /** If true, enables debug logging for the request. */ debug?: boolean; /** Multipart form data to be sent in the request body. */ multipart?: Record<string, any>; /** Timeout for the request in milliseconds. */ timeout?: number; /** If true, caches the response data in memory. the default is false. */ cache?: boolean; /** Specifies the type of request data. */ requestType?: "text" | "json" | "form" | "formData"; /** If true, starts a new request session and clear cookies (alias for startNewRequest). */ startNew?: boolean; /** Specifies the Content-Type header for the request. */ contentType?: "application/json" | "application/x-www-form-urlencoded" | "multipart/form-data" | "text/plain"; /** Maximum number of redirects to follow (default 10). */ maxRedirects?: number; /** If true, logs the request headers. */ printHeaders?: boolean; /** If true, ignores HTTPS certificate errors (only works in non-node environments). */ ignoreHttpsError?: boolean; /** File path to save the response content. */ saveTo?: string; /** If true, treats 302 status code as 303 (default true to follow latest web standard). */ treat302As303?: boolean; retry?: { /** Delay in milliseconds between retry attempts. Used in conjunction with maxRetries (default 0). */ delay?: number; /** If true, increments the retry delay between attempts. */ incrementDelay?: boolean; /** Maximum number of retries for the request (default 0). */ retries?: number; }; /** If true, forces retry on 403 Forbidden responses. */ forceRetryForbiddenRequest?: boolean; /** If true, forces retry on 401 Unauthorized responses. */ forceRetryUnauthorizedRequest?: boolean; /** If true, automatically sets the Referer header based on the final URL. */ autoSetReferer?: boolean; /** * Automatically sets the Origin header to the request URL's origin (protocol + hostname + port). * This is useful for CORS requests where the Origin header is required. */ autoSetOrigin?: boolean; /** If true, uses the curl command to fetch the resource. To use this feature, you need to install the curl command in your system. */ useCurl?: boolean; /** * If true, the HTTP client will use the default configuration. * The default value is true. * This is useful because servers may use different approaches. */ mimicBrowser?: boolean; /** * Proxy configuration for the request. This feature is only available in Node.js and Deno environments. * The Worker version (for browser environments) only supports SOCKS5 proxies. * * In Node.js and Deno, you can use HTTP, HTTPS, and SOCKS5 proxies. However, in Worker environments, * only SOCKS5 proxies are supported due to limitations in browser APIs. * * When using a proxy, all network requests will be routed through the specified proxy server. * This can be useful for bypassing network restrictions, improving privacy, or accessing * geo-restricted content. * * Note that using a proxy may affect the performance of your requests, as they introduce * an additional hop in the network path. * * @property {string} host - The hostname or IP address of the proxy server. * @property {number} port - The port number on which the proxy server is listening. * @property {string} [username] - Optional. The username for proxy authentication, if required. * @property {string} [password] - Optional. The password for proxy authentication, if required. * @property {("socks5" | "http" | "https")} [protocol] - The protocol used by the proxy server. * Defaults to "null" if not specified. * In Worker environments, only "socks5" is supported. * * Example usage: * ``` * proxy: { * host: "proxy.example.com", * port: 8080, * username: "proxyuser", * password: "proxypass", * protocol: "socks5" * } * ``` * * Security note: Be cautious when using proxies, especially with authentication credentials. * Ensure you trust the proxy provider and use secure connections whenever possible. */ proxy?: { protocol: "socks5" | "http" | "https"; host: string; port: number; username?: string; password?: string; keepAlive?: boolean; timeout?: number; rejectUnauthorized?: boolean; keepAliveMsecs?: number; maxSockets?: number; maxFreeSockets?: number; }; /** * A callback function that is invoked when a redirect response is received. * This function allows you to control the behavior of the redirect, such as whether to follow the redirect, * modify the redirect URL, or change the HTTP method for the redirected request. * * @callback onRedirect * @param {Object} options - The options object containing details about the redirect response. * @param {URL} options.url - The URL to which the request is being redirected. * @param {number} options.status - The HTTP status code of the redirect response. * @param {IncomingHttpHeaders} options.headers - The headers of the redirect response. * @param {boolean} options.sameDomain - A boolean indicating whether the redirect URL is on the same domain as the original request. * * @returns {boolean | Object} - The return value determines the behavior of the redirect. * If a boolean is returned: * - `true`: Follow the redirect using the default behavior. * - `false`: Do not follow the redirect. * If an object is returned, it allows for more granular control over the redirect: * - `redirect`: A boolean indicating whether to follow the redirect. * - `url`: A string specifying the new URL to which the request should be redirected. * - `method`: An optional string specifying the HTTP method to use for the redirected request. * * Example usage: * ``` * onRedirect: ({ url, status, headers, sameDomain }) => { * if (status === 301 || status === 302) { * // Always follow permanent and temporary redirects * return true; * } else if (status === 307 || status === 308) { * // For 307 and 308, follow the redirect but change the method to GET * return { redirect: true, url: url.toString(), method: 'GET' }; * } else { * // Do not follow other types of redirects * return false; * } * } * ``` * * This callback provides flexibility in handling redirects, allowing you to implement custom logic based on the * specific requirements of your application. For example, you can choose to follow redirects only for certain * status codes, modify the redirect URL to include additional query parameters, or change the HTTP method for * the redirected request. * * Note: Be cautious when modifying the redirect URL or method, as it may affect the behavior of the request and * the server's response. Ensure that the changes are appropriate for the specific use case and do not introduce * security vulnerabilities or unexpected behavior. */ onRedirect?: (options: OnRedirectOptions) => OnRedirectResponse; /** Determines whether to reject unauthorized server certificates. */ rejectUnauthorized?: boolean; /** The HTTP agent to be used for the request. */ httpAgent?: httpAgent; /** The HTTPS agent to be used for the request. */ httpsAgent?: httpsAgent; /** The priority queue to be used for the request. */ pqueue?: PQueue; /** The authentication credentials to be used for the request. */ auth?: { username: string; password: string; }; }; export type DownloadOptions = HttpConfig & { /** Fetches a resource from the given URL and saves it to the specified file path to maintain memory efficiency. * * Only works with GET and POST requests, this is useful for downloading large files, and it will make the response.data returns null. * */ saveTo: string; }; export type HttpConfigInner = RequestInit & { /** Cookies to be sent with the request. Can be an array of cookie objects, serialized cookies, or a string. */ cookies?: any[] | string | string[]; /** Determines whether to include cookies in the request. Defaults to true. */ enableCookieJar?: boolean; useHTTP2?: boolean; /** Query parameters to be appended to the URL. */ params?: { [key: string]: string | number | boolean; }; /** If true, starts a new request session and clear cookies. */ startNewRequest?: boolean; /** If true, returns the response as a buffer. */ returnBuffer?: boolean; /** Custom headers for the request. */ headers?: Headers | Record<string, string>; /** JSON payload for the request body. */ json?: Record<string | number, any>; /** Form parameters to be sent in the request body. */ form_params?: Record<string, any>; /** If true, prevents following redirects. */ dontFollowRedirects?: boolean; /** If true, sends the request without a Content-Type header. */ withoutContentType?: boolean; /** If true, enables debug logging for the request. */ debug?: boolean; /** Multipart form data to be sent in the request body. */ multipart?: Record<string, any>; /** If true, caches the response data in memory. the default is false. */ cache?: boolean; /** Timeout for the request in milliseconds. */ timeout?: number; /** Specifies the type of request data. */ requestType?: "text" | "json" | "form" | "formData"; /** If true, starts a new request session and clear cookies (alias for startNewRequest). */ startNew?: boolean; /** Specifies the Content-Type header for the request. */ contentType?: "application/json" | "application/x-www-form-urlencoded" | "multipart/form-data" | "text/plain"; /** Maximum number of redirects to follow (default 10). */ maxRedirects?: number; /** If true, logs the request headers. */ printHeaders?: boolean; /** If true, ignores HTTPS certificate errors (only works in non-node environments). */ iggnoreHttpsError?: boolean; /** If true, treats 302 status code as 303 (default true to follow latest web standard). */ treat302As303?: boolean; /** Fetches a resource from the given URL and saves it to the specified file path to maintain memory efficiency. * * Only works with GET and POST requests, this is useful for downloading large files, and it will make the response.data returns null. * */ saveTo?: string; /** The name of the file to save the response content (alias for saveTo). */ fileName?: string; retry?: { /** Delay in milliseconds between retry attempts. Used in conjunction with maxRetries. */ delay?: number; /** If true, increments the retry delay between attempts. */ incrementDelay?: boolean; /** Maximum number of retries for the request (default 0). */ retries?: number; }; /** If true, forces retry on 403 Forbidden responses. */ forceRetryForbiddenRequest?: boolean; /** If true, forces retry on 401 Unauthorized responses. */ forceRetryUnauthorizedRequest?: boolean; /** If true, automatically sets the Referer header based on the final URL. */ autoSetReferer?: boolean; /** * An AbortSignal object that allows you to cancel the request. */ signal?: AbortSignal | undefined; /** * Proxy configuration for the request. This feature is only available in Node.js and Deno environments. * The Worker version (for browser environments) only supports SOCKS5 proxies. * * In Node.js and Deno, you can use HTTP, HTTPS, and SOCKS5 proxies. However, in Worker environments, * only SOCKS5 proxies are supported due to limitations in browser APIs. * * When using a proxy, all network requests will be routed through the specified proxy server. * This can be useful for bypassing network restrictions, improving privacy, or accessing * geo-restricted content. * * Note that using a proxy may affect the performance of your requests, as they introduce * an additional hop in the network path. * * @property {string} host - The hostname or IP address of the proxy server. * @property {number} port - The port number on which the proxy server is listening. * @property {string} [username] - Optional. The username for proxy authentication, if required. * @property {string} [password] - Optional. The password for proxy authentication, if required. * @property {("socks5" | "http" | "https")} [protocol] - The protocol used by the proxy server. * Defaults to "null" if not specified. * In Worker environments, only "socks5" is supported. * * Example usage: * ``` * proxy: { * host: "proxy.example.com", * port: 8080, * username: "proxyuser", * password: "proxypass", * protocol: "socks5" * } * ``` * * Security note: Be cautious when using proxies, especially with authentication credentials. * Ensure you trust the proxy provider and use secure connections whenever possible. */ proxy?: IProxy; /** * A callback function that is invoked when a redirect response is received. * This function allows you to control the behavior of the redirect, such as whether to follow the redirect, * modify the redirect URL, or change the HTTP method for the redirected request. * * @callback onRedirect * @param {Object} options - The options object containing details about the redirect response. * @param {URL} options.url - The URL to which the request is being redirected. * @param {number} options.status - The HTTP status code of the redirect response. * @param {IncomingHttpHeaders} options.headers - The headers of the redirect response. * @param {boolean} options.sameDomain - A boolean indicating whether the redirect URL is on the same domain as the original request. * * @returns {boolean | Object} - The return value determines the behavior of the redirect. * If a boolean is returned: * - `true`: Follow the redirect using the default behavior. * - `false`: Do not follow the redirect. * If an object is returned, it allows for more granular control over the redirect: * - `redirect`: A boolean indicating whether to follow the redirect. * - `url`: A string specifying the new URL to which the request should be redirected. * - `method`: An optional string specifying the HTTP method to use for the redirected request. * * Example usage: * ``` * onRedirect: ({ url, status, headers, sameDomain }) => { * if (status === 301 || status === 302) { * // Always follow permanent and temporary redirects * return true; * } else if (status === 307 || status === 308) { * // For 307 and 308, follow the redirect but change the method to GET * return { redirect: true, url: url.toString(), method: 'GET' }; * } else { * // Do not follow other types of redirects * return false; * } * } * ``` * * This callback provides flexibility in handling redirects, allowing you to implement custom logic based on the * specific requirements of your application. For example, you can choose to follow redirects only for certain * status codes, modify the redirect URL to include additional query parameters, or change the HTTP method for * the redirected request. * * Note: Be cautious when modifying the redirect URL or method, as it may affect the behavior of the request and * the server's response. Ensure that the changes are appropriate for the specific use case and do not introduce * security vulnerabilities or unexpected behavior. */ onRedirect?: (options: OnRedirectOptions) => OnRedirectResponse; /** * The authentication credentials to be used for the request. */ auth?: { username: string; password: string; }; }; export interface OnRedirectOptions { url: URL; status: number; headers: IncomingHttpHeaders; sameDomain: boolean; method: string; } export type OnRedirectResponse = boolean | ToRedirectOptions | undefined; export type ToRedirectOptions = { redirect: false; message?: string; } | { redirect: true; url: string; method?: "POST" | "GET" | "PUT" | "DELETE" | "PATCH" | "OPTIONS"; body?: any; withoutBody?: boolean; setHeaders?: IncomingHttpHeaders; setHeadersOnRedirects?: IncomingHttpHeaders; }; export type queueOptions = Options<PriorityQueue, QueueAddOptions>; /** * Represents the configuration options for making HTTP requests or file downloads. * * This type is a union of `HttpConfig` and `DownloadOptions`, allowing for flexible * configuration of different types of network operations. * * Use `HttpConfig` for standard HTTP requests, which includes options like method, * headers, and body. This is suitable for general API interactions, data fetching, * and sending data to servers. * * Use `DownloadOptions` when you need to download files or large data streams. * This typically includes options specific to file downloads, such as the destination * path, progress tracking, and handling of existing files. * * @example * // HTTP request configuration * const httpConfig: RequestConfig = { * url: 'https://api.example.com/data', * method: 'POST', * headers: { 'Content-Type': 'application/json' }, * body: JSON.stringify({ key: 'value' }) * }; * * @example * // Download configuration * const downloadConfig: RequestConfig = { * url: 'https://example.com/large-file.zip', * destination: '/path/to/save/file.zip', * onProgress: (progress) => console.log(`Downloaded: ${progress.percent}%`) * }; */ export type RequestConfig = (HttpConfig | DownloadOptions); export interface IProxy { protocol: "socks5" | "http" | "https"; host: string; port: number; username?: string; password?: string; keepAlive?: boolean; timeout?: number; rejectUnauthorized?: boolean; keepAliveMsecs?: number; maxSockets?: number; maxFreeSockets?: number; } export interface UniqhttConfig { requestHeader: OutgoingHttpHeaders; requestBody: FormData | { [key: string]: any; } | string | null; method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS"; requestCookies: Cookie[]; cookiesEnabled: boolean; adapter: any; url: URL; maxRedirection: number; mimicBrowser: boolean; proxy: IProxy | null; timeout: number; retry: { maxRetries?: number; retryDelay?: number; incrementDelay?: boolean; } | null; queueOptions: { enable: boolean; options?: queueOptions; } | null; signal: AbortSignal | null; isCurl: boolean; httpAgent: httpAgent | httpsAgent | null; redirectOptions: { method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS"; url: URL; requestHeader: OutgoingHttpHeaders; requestBody: FormData | { [key: string]: any; } | string | null; }[] | null; } /** * Represents the structured response from a crawler request. * Contains both the response data and associated metadata. * * @template T - The type of the response data. Defaults to string. * * @property {T} data - The response body data, typed according to template parameter T * @property {string} contentType - The MIME type of the response content * @property {string} finalUrl - The final URL after any redirects * @property {string} url - The original requested URL * @property {OutgoingHttpHeaders} headers - Response headers received from the server * @property {number} status - HTTP status code of the response * @property {string} statusText - HTTP status message corresponding to the status code * @property {SerializedCookie[]} cookies - Array of cookies received in the response * @property {number} contentLength - Size of the response content in bytes * * @example * ```typescript * // HTML response * const response: CrawlerResponse<string> = { * data: "<html>...</html>", * contentType: "text/html", * finalUrl: "https://example.com/page", * url: "https://example.com/page", * headers: { "content-type": "text/html" }, * status: 200, * statusText: "OK", * cookies: [], * contentLength: 1234 * }; * * // JSON response * const jsonResponse: CrawlerResponse<object> = { * data: { key: "value" }, * contentType: "application/json", * // ... other properties * }; * ``` */ export interface CrawlerResponse<T = string> { /** Response data */ data: T; /** Content type (MIME type) */ contentType: string; /** Final URL after redirects */ finalUrl: string; /** Original requested URL */ url: string; /** Response headers */ headers: OutgoingHttpHeaders; /** HTTP status code */ status: number; /** HTTP status text */ statusText: string; /** Response cookies */ cookies: SerializedCookie[]; /** Response content length in bytes */ contentLength: number; } export interface DefaultOptions { queueOptions?: { enable: boolean; options?: queueOptions; }; headers?: OutgoingHttpHeaders; baseURL?: string | URL | null; debug?: boolean; mimicBrowser?: boolean | undefined; timeout?: number; retry?: { maxRetries?: number; retryDelay?: number; incrementDelay?: boolean; }; customJar?: CookieJar; enableCookieJar?: boolean; } declare class UniqhttEdge extends Base { constructor(init?: DefaultOptions); setDefaultOptions(options: DefaultOptions): void; postMultipart<T = any>(input: string | URL, formData: FormData): Promise<UniqhttResponse<T>>; postMultipart<T = any>(input: string | URL, formData: UniqFormData): Promise<UniqhttResponse<T>>; postMultipart<T = any>(input: string | URL, dataObject: Record<string, any>): Promise<UniqhttResponse<T>>; postMultipart<T = any>(input: string | URL, formData: UniqFormData | FormData, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; postMultipart<T = any>(input: string | URL, dataObject: Record<string, any>, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; postMultipart<T = any>(input: string | URL, formData: UniqFormData | FormData, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; postMultipart<T = any>(input: string | URL, dataObject: Record<string, any>, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; protected request(input: string | URL, method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS", data?: any, _config?: (HttpConfig | DownloadOptions) & { body?: any; }): Promise<any>; private checkENV; private makeRequest; private errorName; } declare const ERROR_INFO: { ECONNREFUSED: { code: number; message: string; }; ECONNRESET: { code: number; message: string; }; ETIMEDOUT: { code: number; message: string; }; ENOTFOUND: { code: number; message: string; }; EAI_AGAIN: { code: number; message: string; }; EPROTO: { code: number; message: string; }; ERR_INVALID_PROTOCOL: { code: number; message: string; }; ERR_TLS_CERT_ALTNAME_INVALID: { code: number; message: string; }; ERR_TLS_HANDSHAKE_TIMEOUT: { code: number; message: string; }; ERR_TLS_INVALID_PROTOCOL_VERSION: { code: number; message: string; }; ERR_TLS_RENEGOTIATION_DISABLED: { code: number; message: string; }; ERR_TLS_CERT_SIGNATURE_ALGORITHM_UNSUPPORTED: { code: number; message: string; }; ERR_HTTP_HEADERS_SENT: { code: number; message: string; }; ERR_INVALID_ARG_TYPE: { code: number; message: string; }; ERR_INVALID_URL: { code: number; message: string; }; ERR_STREAM_DESTROYED: { code: number; message: string; }; ERR_STREAM_PREMATURE_CLOSE: { code: number; message: string; }; UND_ERR_CONNECT_TIMEOUT: { code: number; message: string; }; UND_ERR_HEADERS_TIMEOUT: { code: number; message: string; }; UND_ERR_SOCKET: { code: number; message: string; }; UND_ERR_INFO: { code: number; message: string; }; UND_ERR_ABORTED: { code: number; message: string; }; ABORT_ERR: { code: number; message: string; }; UND_ERR_REQUEST_TIMEOUT: { code: number; message: string; }; UNQ_UNKOWN_ERROR: { code: number; message: string; }; UNQ_FILE_PERMISSION_ERROR: { code: number; message: string; }; UNQ_MISSING_REDIRECT_LOCATION: { code: number; message: string; }; UNQ_DECOMPRESSION_ERROR: { code: number; message: string; }; UNQ_DOWNLOAD_FAILED: { code: number; message: string; }; UNQ_HTTP_ERROR: { code: number; message: string; }; UNQ_REDIRECT_DENIED: { code: number; message: string; }; UNQ_PROXY_INVALID_PROTOCOL: { code: number; message: string; }; UNQ_PROXY_INVALID_HOSTPORT: { code: number; message: string; }; UNQ_SOCKS_CONNECTION_FAILED: { code: number; message: string; }; UNQ_SOCKS_AUTHENTICATION_FAILED: { code: number; message: string; }; UNQ_SOCKS_TARGET_CONNECTION_FAILED: { code: number; message: string; }; UNQ_SOCKS_PROTOCOL_ERROR: { code: number; message: string; }; UNQ_PROXY_ERROR: { code: number; message: string; }; }; export type ErrorCodeKey = keyof typeof ERROR_INFO; declare class UniqhttError$1 extends Error { #private; response: UniqhttResponse; code: ErrorCodeKey; config: UniqhttConfig; constructor(message: string, response: Response | IResponse, data: any, code: ErrorCodeKey, headers: IncomingHttpHeaders, config: UniqhttConfig, urls?: string[]); toJSON(): { name: string; message: string; method: string; url: string; headers: IncomingHttpHeaders; status: number; config: UniqhttConfig; code: "ECONNREFUSED" | "ECONNRESET" | "ETIMEDOUT" | "ENOTFOUND" | "EAI_AGAIN" | "EPROTO" | "ERR_INVALID_PROTOCOL" | "ERR_TLS_CERT_ALTNAME_INVALID" | "ERR_TLS_HANDSHAKE_TIMEOUT" | "ERR_TLS_INVALID_PROTOCOL_VERSION" | "ERR_TLS_RENEGOTIATION_DISABLED" | "ERR_TLS_CERT_SIGNATURE_ALGORITHM_UNSUPPORTED" | "ERR_HTTP_HEADERS_SENT" | "ERR_INVALID_ARG_TYPE" | "ERR_INVALID_URL" | "ERR_STREAM_DESTROYED" | "ERR_STREAM_PREMATURE_CLOSE" | "UND_ERR_CONNECT_TIMEOUT" | "UND_ERR_HEADERS_TIMEOUT" | "UND_ERR_SOCKET" | "UND_ERR_INFO" | "UND_ERR_ABORTED" | "ABORT_ERR" | "UND_ERR_REQUEST_TIMEOUT" | "UNQ_UNKOWN_ERROR" | "UNQ_FILE_PERMISSION_ERROR" | "UNQ_MISSING_REDIRECT_LOCATION" | "UNQ_DECOMPRESSION_ERROR" | "UNQ_DOWNLOAD_FAILED" | "UNQ_HTTP_ERROR" | "UNQ_REDIRECT_DENIED" | "UNQ_PROXY_INVALID_PROTOCOL" | "UNQ_PROXY_INVALID_HOSTPORT" | "UNQ_SOCKS_CONNECTION_FAILED" | "UNQ_SOCKS_AUTHENTICATION_FAILED" | "UNQ_SOCKS_TARGET_CONNECTION_FAILED" | "UNQ_SOCKS_PROTOCOL_ERROR" | "UNQ_PROXY_ERROR"; cause: string | null; finalUrl: string; statusText: string; urls: string[]; }; } declare abstract class Base { protected queue: PQueue | null; protected isQueueEnabled: boolean; jar: CookieJar; protected innerFetchOption: string[]; protected environment: "node" | "edge"; protected defaultUserAgent: string; protected baseURL: string | null; protected defaultHeaders: OutgoingHttpHeaders | null; protected defaultDebug?: boolean; protected mimicBrowser?: boolean; protected debug?: boolean; protected timeout?: number; protected retry?: { maxRetries?: number; retryDelay?: number; incrementDelay?: boolean; }; protected queueOptions: { enable: boolean; options?: queueOptions; } | null; protected isCurl: { status: true; } | { status: false; message: string; }; protected tempPath?: string; protected useCurl?: boolean; private RETRYABLE_STATUS_CODES; protected rejectUnauthorized?: boolean; protected useSecureContext?: boolean; protected httpAgent?: httpAgent; protected httpsAgent?: httpsAgent; protected enableCookieJar: boolean; protected constructor(init?: { queueOptions?: { enable: boolean; options?: queueOptions; }; }); private shouldRetry; /** * queueEnabled = true to enable PQueue instance for further http request, otherwise pass false to turn it off */ set queueEnabled(value: boolean); setQueueOptions(queueOptions: { enable: boolean; options?: queueOptions; }): void; /** * get the state of pQueue if its running or not. */ get queueEnabled(): boolean; /** * Checks if the provided error is an instance of UniqhttError. * * @param error - The error object to check. * @returns A boolean indicating whether the error is an instance of UniqhttError. */ isUniqhttError(error: any): boolean; setCookies(stringCookies: string): void; setCookies(stringCookies: string, url: string, startNew?: boolean): void; setCookies(serializedStringCookiesCookies: string, url: string | undefined, startNew: boolean): void; setCookies(serializedCookies: SerializedCookie[]): void; setCookies(serializedCookies: SerializedCookie[], url: string, startNew?: boolean): void; setCookies(serializedCookies: SerializedCookie[], url: string | undefined, startNew: boolean): void; setCookies(cookies: Cookie[]): void; setCookies(cookies: Cookie[], url: string, startNew?: boolean): void; setCookies(cookies: Cookie[], url: string | undefined, startNew: boolean): void; setCookies(setCookieArray: string[]): void; setCookies(setCookieArray: string[], url: string, startNew?: boolean): void; setCookies(setCookieArray: string[], url: string | undefined, startNew: boolean): void; getCookies(): Cookies; protected abstract request(input: string | URL, method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS", data?: any, config?: (HttpConfig | DownloadOptions) & { isFormData?: boolean; isJson?: boolean; withoutBodyOnRedirect?: boolean; }): Promise<any>; get<T = any>(input: string | URL, config: DownloadOptions): Promise<DownloadResponse<null>>; get<T = any>(input: string | URL, config: HttpConfig): Promise<UniqhttResponse<T>>; get<T = any>(input: string | URL, config: HttpConfig & { returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; get<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; clearCookies(): void; post<T = any>(input: string | URL, data: any, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; post<T = any>(input: string | URL, data: any, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; post<T = any>(input: string | URL, data: any, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; post<T = any>(input: string | URL, data: any): Promise<UniqhttResponse<T>>; post<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; postForm<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; postForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any>): Promise<UniqhttResponse<T>>; postForm<T = any>(input: string | URL, stringBody: string): Promise<UniqhttResponse<T>>; postForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; postForm<T = any>(input: string | URL, stringBody: string, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; postForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; postForm<T = any>(input: string | URL, stringBody: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; postForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; postForm<T = any>(input: string | URL, stringBody: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; postForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: HttpConfig & { returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; postForm<T = any>(input: string | URL, stringBody: string, config: HttpConfig & { returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; postForm<T = any>(input: string | URL, data: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; postForm<T = any>(input: string | URL, data: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; postForm<T = any>(input: string | URL, data: undefined | null, config?: HttpConfig & { returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; postForm<T = any>(input: string | URL, data: undefined | null, config?: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; postJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; postJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; postJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: Buffer; }): Promise<UniqhttResponse<Buffer>>; postJson<T = any>(input: string | URL, jsonString: string, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; postJson<T = any>(input: string | URL, jsonString: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; postJson<T = any>(input: string | URL, jsonString: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: Buffer; }): Promise<UniqhttResponse<Buffer>>; postJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>): Promise<UniqhttResponse<T>>; postJson<T = any>(input: string | URL, jsonOjsonStringbject: string): Promise<UniqhttResponse<T>>; postJson<T = any>(input: string | URL, nullData: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; postJson<T = any>(input: string | URL, nullData: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: Buffer; }): Promise<UniqhttResponse<Buffer>>; postJson<T = any>(input: string | URL, nullData: undefined | null, config?: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; postJson<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; put<T = any>(input: string | URL, data: any, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; put<T = any>(input: string | URL, data: any, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; put<T = any>(input: string | URL, data: any, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; put<T = any>(input: string | URL, data: any): Promise<UniqhttResponse<T>>; put<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; putForm<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; putForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any>): Promise<UniqhttResponse<T>>; putForm<T = any>(input: string | URL, stringBody: string): Promise<UniqhttResponse<T>>; putForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; putForm<T = any>(input: string | URL, stringBody: string, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; putForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; putForm<T = any>(input: string | URL, stringBody: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; putForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; putForm<T = any>(input: string | URL, stringBody: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; putForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: HttpConfig & { returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; putForm<T = any>(input: string | URL, stringBody: string, config: HttpConfig & { returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; putForm<T = any>(input: string | URL, data: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; putForm<T = any>(input: string | URL, data: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; putForm<T = any>(input: string | URL, data: undefined | null, config?: HttpConfig & { returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; putForm<T = any>(input: string | URL, data: undefined | null, config?: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; putJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; putJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; putJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: Buffer; }): Promise<UniqhttResponse<Buffer>>; putJson<T = any>(input: string | URL, jsonString: string, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; putJson<T = any>(input: string | URL, jsonString: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; putJson<T = any>(input: string | URL, jsonString: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: Buffer; }): Promise<UniqhttResponse<Buffer>>; putJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>): Promise<UniqhttResponse<T>>; putJson<T = any>(input: string | URL, jsonOjsonStringbject: string): Promise<UniqhttResponse<T>>; putJson<T = any>(input: string | URL, nullData: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; putJson<T = any>(input: string | URL, nullData: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: Buffer; }): Promise<UniqhttResponse<Buffer>>; putJson<T = any>(input: string | URL, nullData: undefined | null, config?: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; putJson<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; patch<T = any>(input: string | URL, data: any, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; patch<T = any>(input: string | URL, data: any, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; patch<T = any>(input: string | URL, data: any, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; patch<T = any>(input: string | URL, data: any): Promise<UniqhttResponse<T>>; patch<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; patchForm<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; patchForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any>): Promise<UniqhttResponse<T>>; patchForm<T = any>(input: string | URL, stringBody: string): Promise<UniqhttResponse<T>>; patchForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; patchForm<T = any>(input: string | URL, stringBody: string, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; patchForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; patchForm<T = any>(input: string | URL, stringBody: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; patchForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; patchForm<T = any>(input: string | URL, stringBody: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; patchForm<T = any>(input: string | URL, data: URLSearchParams | FormData | Record<string, any> | string, config: HttpConfig & { returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; patchForm<T = any>(input: string | URL, stringBody: string, config: HttpConfig & { returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; patchForm<T = any>(input: string | URL, data: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; patchForm<T = any>(input: string | URL, data: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; patchForm<T = any>(input: string | URL, data: undefined | null, config?: HttpConfig & { returnBuffer: true; }): Promise<UniqhttResponse<Buffer>>; patchForm<T = any>(input: string | URL, data: undefined | null, config?: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; patchJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; patchJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; patchJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: Buffer; }): Promise<UniqhttResponse<Buffer>>; patchJson<T = any>(input: string | URL, jsonString: string, config: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; patchJson<T = any>(input: string | URL, jsonString: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; patchJson<T = any>(input: string | URL, jsonString: string, config: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: Buffer; }): Promise<UniqhttResponse<Buffer>>; patchJson<T = any>(input: string | URL, jsonObject: Record<string | number, any>): Promise<UniqhttResponse<T>>; patchJson<T = any>(input: string | URL, jsonOjsonStringbject: string): Promise<UniqhttResponse<T>>; patchJson<T = any>(input: string | URL, nullData: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; }): Promise<UniqhttResponse<T>>; patchJson<T = any>(input: string | URL, nullData: undefined | null, config?: HttpConfig & { withoutBodyOnRedirect?: boolean; returnBuffer: Buffer; }): Promise<UniqhttResponse<Buffer>>; patchJson<T = any>(input: string | URL, nullData: undefined | null, config?: DownloadOptions & { withoutBodyOnRedirect?: boolean; }): Promise<DownloadResponse<null>>; patchJson<T = any>(input: string | URL): Promise<UniqhttResponse<T>>; delete<T = any>(input: string | URL, config?: HttpConfig): Promise<UniqhttResponse<T>>; head<T = any>(input: string | URL, config?: HttpConfig): Promise<UniqhttResponse<T>>; options<T = any>(input: string | URL, config?: HttpConfig): Promise<UniqhttResponse<T>>; private parseJson; protected Error(response: Response | IResponse | { status: number; statusText: string; url: string; }, message: string, config: UniqhttConfig | null, urls: string[], code: ErrorCodeKey): Promise<UniqhttError$1>; protected formatResponse<T = any>(response: Response | IResponse, finalUrl: string, isBuffer: boolean, config: UniqhttConfig, downloadConfig: { fileName: string; totalTime: string; downloadSpeed: string; size: string; } | undef