index.d.ts 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /// <reference types="node" />
  2. import { Duplex, Readable } from 'stream';
  3. import { URL, URLSearchParams } from 'url';
  4. import { Socket } from 'net';
  5. import { SecureContextOptions, DetailedPeerCertificate } from 'tls';
  6. import http = require('http');
  7. import { ClientRequest, RequestOptions, ServerResponse, request as httpRequest } from 'http';
  8. import https = require('https');
  9. import { Timings, IncomingMessageWithTimings } from '@szmarczak/http-timer';
  10. import CacheableLookup from 'cacheable-lookup';
  11. import CacheableRequest = require('cacheable-request');
  12. import ResponseLike = require('responselike');
  13. import { Delays, TimeoutError as TimedOutTimeoutError } from './utils/timed-out';
  14. import { URLOptions } from './utils/options-to-url';
  15. import { DnsLookupIpVersion } from './utils/dns-ip-version';
  16. import { PromiseOnly } from '../as-promise/types';
  17. declare type HttpRequestFunction = typeof httpRequest;
  18. declare type Error = NodeJS.ErrnoException;
  19. declare const kRequest: unique symbol;
  20. declare const kResponse: unique symbol;
  21. declare const kResponseSize: unique symbol;
  22. declare const kDownloadedSize: unique symbol;
  23. declare const kBodySize: unique symbol;
  24. declare const kUploadedSize: unique symbol;
  25. declare const kServerResponsesPiped: unique symbol;
  26. declare const kUnproxyEvents: unique symbol;
  27. declare const kIsFromCache: unique symbol;
  28. declare const kCancelTimeouts: unique symbol;
  29. declare const kStartedReading: unique symbol;
  30. declare const kStopReading: unique symbol;
  31. declare const kTriggerRead: unique symbol;
  32. declare const kBody: unique symbol;
  33. declare const kJobs: unique symbol;
  34. declare const kOriginalResponse: unique symbol;
  35. declare const kRetryTimeout: unique symbol;
  36. export declare const kIsNormalizedAlready: unique symbol;
  37. export interface Agents {
  38. http?: http.Agent;
  39. https?: https.Agent;
  40. http2?: unknown;
  41. }
  42. export declare const withoutBody: ReadonlySet<string>;
  43. export interface ToughCookieJar {
  44. getCookieString: ((currentUrl: string, options: Record<string, unknown>, cb: (err: Error | null, cookies: string) => void) => void) & ((url: string, callback: (error: Error | null, cookieHeader: string) => void) => void);
  45. setCookie: ((cookieOrString: unknown, currentUrl: string, options: Record<string, unknown>, cb: (err: Error | null, cookie: unknown) => void) => void) & ((rawCookie: string, url: string, callback: (error: Error | null, result: unknown) => void) => void);
  46. }
  47. export interface PromiseCookieJar {
  48. getCookieString: (url: string) => Promise<string>;
  49. setCookie: (rawCookie: string, url: string) => Promise<unknown>;
  50. }
  51. /**
  52. All available HTTP request methods provided by Got.
  53. */
  54. export declare type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'HEAD' | 'DELETE' | 'OPTIONS' | 'TRACE' | 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'trace';
  55. declare type Promisable<T> = T | Promise<T>;
  56. export declare type InitHook = (options: Options) => void;
  57. export declare type BeforeRequestHook = (options: NormalizedOptions) => Promisable<void | Response | ResponseLike>;
  58. export declare type BeforeRedirectHook = (options: NormalizedOptions, response: Response) => Promisable<void>;
  59. export declare type BeforeErrorHook = (error: RequestError) => Promisable<RequestError>;
  60. export declare type BeforeRetryHook = (options: NormalizedOptions, error?: RequestError, retryCount?: number) => void | Promise<void>;
  61. interface PlainHooks {
  62. /**
  63. Called with plain request options, right before their normalization.
  64. This is especially useful in conjunction with `got.extend()` when the input needs custom handling.
  65. __Note #1__: This hook must be synchronous!
  66. __Note #2__: Errors in this hook will be converted into an instances of `RequestError`.
  67. __Note #3__: The options object may not have a `url` property.
  68. To modify it, use a `beforeRequest` hook instead.
  69. @default []
  70. */
  71. init?: InitHook[];
  72. /**
  73. Called with normalized request options.
  74. Got will make no further changes to the request before it is sent.
  75. This is especially useful in conjunction with `got.extend()` when you want to create an API client that, for example, uses HMAC-signing.
  76. @default []
  77. */
  78. beforeRequest?: BeforeRequestHook[];
  79. /**
  80. Called with normalized request options and the redirect response.
  81. Got will make no further changes to the request.
  82. This is especially useful when you want to avoid dead sites.
  83. @default []
  84. @example
  85. ```
  86. const got = require('got');
  87. got('https://example.com', {
  88. hooks: {
  89. beforeRedirect: [
  90. (options, response) => {
  91. if (options.hostname === 'deadSite') {
  92. options.hostname = 'fallbackSite';
  93. }
  94. }
  95. ]
  96. }
  97. });
  98. ```
  99. */
  100. beforeRedirect?: BeforeRedirectHook[];
  101. /**
  102. Called with an `Error` instance.
  103. The error is passed to the hook right before it's thrown.
  104. This is especially useful when you want to have more detailed errors.
  105. __Note__: Errors thrown while normalizing input options are thrown directly and not part of this hook.
  106. @default []
  107. @example
  108. ```
  109. const got = require('got');
  110. got('https://api.github.com/some-endpoint', {
  111. hooks: {
  112. beforeError: [
  113. error => {
  114. const {response} = error;
  115. if (response && response.body) {
  116. error.name = 'GitHubError';
  117. error.message = `${response.body.message} (${response.statusCode})`;
  118. }
  119. return error;
  120. }
  121. ]
  122. }
  123. });
  124. ```
  125. */
  126. beforeError?: BeforeErrorHook[];
  127. /**
  128. Called with normalized request options, the error and the retry count.
  129. Got will make no further changes to the request.
  130. This is especially useful when some extra work is required before the next try.
  131. __Note__: When using streams, this hook is ignored.
  132. __Note__: When retrying in a `afterResponse` hook, all remaining `beforeRetry` hooks will be called without the `error` and `retryCount` arguments.
  133. @default []
  134. @example
  135. ```
  136. const got = require('got');
  137. got.post('https://example.com', {
  138. hooks: {
  139. beforeRetry: [
  140. (options, error, retryCount) => {
  141. if (error.response.statusCode === 413) { // Payload too large
  142. options.body = getNewBody();
  143. }
  144. }
  145. ]
  146. }
  147. });
  148. ```
  149. */
  150. beforeRetry?: BeforeRetryHook[];
  151. }
  152. /**
  153. All available hook of Got.
  154. */
  155. export interface Hooks extends PromiseOnly.Hooks, PlainHooks {
  156. }
  157. declare type PlainHookEvent = 'init' | 'beforeRequest' | 'beforeRedirect' | 'beforeError' | 'beforeRetry';
  158. /**
  159. All hook events acceptable by Got.
  160. */
  161. export declare type HookEvent = PromiseOnly.HookEvent | PlainHookEvent;
  162. export declare const knownHookEvents: HookEvent[];
  163. declare type AcceptableResponse = IncomingMessageWithTimings | ResponseLike;
  164. declare type AcceptableRequestResult = AcceptableResponse | ClientRequest | Promise<AcceptableResponse | ClientRequest> | undefined;
  165. export declare type RequestFunction = (url: URL, options: RequestOptions, callback?: (response: AcceptableResponse) => void) => AcceptableRequestResult;
  166. export declare type Headers = Record<string, string | string[] | undefined>;
  167. declare type CheckServerIdentityFunction = (hostname: string, certificate: DetailedPeerCertificate) => Error | void;
  168. export declare type ParseJsonFunction = (text: string) => unknown;
  169. export declare type StringifyJsonFunction = (object: unknown) => string;
  170. export interface RetryObject {
  171. attemptCount: number;
  172. retryOptions: RequiredRetryOptions;
  173. error: TimeoutError | RequestError;
  174. computedValue: number;
  175. retryAfter?: number;
  176. }
  177. export declare type RetryFunction = (retryObject: RetryObject) => number | Promise<number>;
  178. /**
  179. An object representing `limit`, `calculateDelay`, `methods`, `statusCodes`, `maxRetryAfter` and `errorCodes` fields for maximum retry count, retry handler, allowed methods, allowed status codes, maximum [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) time and allowed error codes.
  180. Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 1).
  181. The `calculateDelay` property is a `function` that receives an object with `attemptCount`, `retryOptions`, `error` and `computedValue` properties for current retry count, the retry options, error and default computed value.
  182. The function must return a delay in milliseconds (or a Promise resolving with it) (`0` return value cancels retry).
  183. By default, it retries *only* on the specified methods, status codes, and on these network errors:
  184. - `ETIMEDOUT`: One of the [timeout](#timeout) limits were reached.
  185. - `ECONNRESET`: Connection was forcibly closed by a peer.
  186. - `EADDRINUSE`: Could not bind to any free port.
  187. - `ECONNREFUSED`: Connection was refused by the server.
  188. - `EPIPE`: The remote side of the stream being written has been closed.
  189. - `ENOTFOUND`: Couldn't resolve the hostname to an IP address.
  190. - `ENETUNREACH`: No internet connection.
  191. - `EAI_AGAIN`: DNS lookup timed out.
  192. __Note__: If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`.
  193. __Note__: If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will cancel the request.
  194. */
  195. export interface RequiredRetryOptions {
  196. limit: number;
  197. methods: Method[];
  198. statusCodes: number[];
  199. errorCodes: string[];
  200. calculateDelay: RetryFunction;
  201. maxRetryAfter?: number;
  202. }
  203. export interface CacheOptions {
  204. shared?: boolean;
  205. cacheHeuristic?: number;
  206. immutableMinTimeToLive?: number;
  207. ignoreCargoCult?: boolean;
  208. }
  209. interface PlainOptions extends URLOptions {
  210. /**
  211. Custom request function.
  212. The main purpose of this is to [support HTTP2 using a wrapper](https://github.com/szmarczak/http2-wrapper).
  213. @default http.request | https.request
  214. */
  215. request?: RequestFunction;
  216. /**
  217. An object representing `http`, `https` and `http2` keys for [`http.Agent`](https://nodejs.org/api/http.html#http_class_http_agent), [`https.Agent`](https://nodejs.org/api/https.html#https_class_https_agent) and [`http2wrapper.Agent`](https://github.com/szmarczak/http2-wrapper#new-http2agentoptions) instance.
  218. This is necessary because a request to one protocol might redirect to another.
  219. In such a scenario, Got will switch over to the right protocol agent for you.
  220. If a key is not present, it will default to a global agent.
  221. @example
  222. ```
  223. const got = require('got');
  224. const HttpAgent = require('agentkeepalive');
  225. const {HttpsAgent} = HttpAgent;
  226. got('https://sindresorhus.com', {
  227. agent: {
  228. http: new HttpAgent(),
  229. https: new HttpsAgent()
  230. }
  231. });
  232. ```
  233. */
  234. agent?: Agents | false;
  235. /**
  236. Decompress the response automatically.
  237. This will set the `accept-encoding` header to `gzip, deflate, br` on Node.js 11.7.0+ or `gzip, deflate` for older Node.js versions, unless you set it yourself.
  238. Brotli (`br`) support requires Node.js 11.7.0 or later.
  239. If this is disabled, a compressed response is returned as a `Buffer`.
  240. This may be useful if you want to handle decompression yourself or stream the raw compressed data.
  241. @default true
  242. */
  243. decompress?: boolean;
  244. /**
  245. Milliseconds to wait for the server to end the response before aborting the request with `got.TimeoutError` error (a.k.a. `request` property).
  246. By default, there's no timeout.
  247. This also accepts an `object` with the following fields to constrain the duration of each phase of the request lifecycle:
  248. - `lookup` starts when a socket is assigned and ends when the hostname has been resolved.
  249. Does not apply when using a Unix domain socket.
  250. - `connect` starts when `lookup` completes (or when the socket is assigned if lookup does not apply to the request) and ends when the socket is connected.
  251. - `secureConnect` starts when `connect` completes and ends when the handshaking process completes (HTTPS only).
  252. - `socket` starts when the socket is connected. See [request.setTimeout](https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback).
  253. - `response` starts when the request has been written to the socket and ends when the response headers are received.
  254. - `send` starts when the socket is connected and ends with the request has been written to the socket.
  255. - `request` starts when the request is initiated and ends when the response's end event fires.
  256. */
  257. timeout?: Delays | number;
  258. /**
  259. When specified, `prefixUrl` will be prepended to `url`.
  260. The prefix can be any valid URL, either relative or absolute.
  261. A trailing slash `/` is optional - one will be added automatically.
  262. __Note__: `prefixUrl` will be ignored if the `url` argument is a URL instance.
  263. __Note__: Leading slashes in `input` are disallowed when using this option to enforce consistency and avoid confusion.
  264. For example, when the prefix URL is `https://example.com/foo` and the input is `/bar`, there's ambiguity whether the resulting URL would become `https://example.com/foo/bar` or `https://example.com/bar`.
  265. The latter is used by browsers.
  266. __Tip__: Useful when used with `got.extend()` to create niche-specific Got instances.
  267. __Tip__: You can change `prefixUrl` using hooks as long as the URL still includes the `prefixUrl`.
  268. If the URL doesn't include it anymore, it will throw.
  269. @example
  270. ```
  271. const got = require('got');
  272. (async () => {
  273. await got('unicorn', {prefixUrl: 'https://cats.com'});
  274. //=> 'https://cats.com/unicorn'
  275. const instance = got.extend({
  276. prefixUrl: 'https://google.com'
  277. });
  278. await instance('unicorn', {
  279. hooks: {
  280. beforeRequest: [
  281. options => {
  282. options.prefixUrl = 'https://cats.com';
  283. }
  284. ]
  285. }
  286. });
  287. //=> 'https://cats.com/unicorn'
  288. })();
  289. ```
  290. */
  291. prefixUrl?: string | URL;
  292. /**
  293. __Note #1__: The `body` option cannot be used with the `json` or `form` option.
  294. __Note #2__: If you provide this option, `got.stream()` will be read-only.
  295. __Note #3__: If you provide a payload with the `GET` or `HEAD` method, it will throw a `TypeError` unless the method is `GET` and the `allowGetBody` option is set to `true`.
  296. __Note #4__: This option is not enumerable and will not be merged with the instance defaults.
  297. The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / `fs.createReadStream` instance / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.
  298. */
  299. body?: string | Buffer | Readable;
  300. /**
  301. The form body is converted to a query string using [`(new URLSearchParams(object)).toString()`](https://nodejs.org/api/url.html#url_constructor_new_urlsearchparams_obj).
  302. If the `Content-Type` header is not present, it will be set to `application/x-www-form-urlencoded`.
  303. __Note #1__: If you provide this option, `got.stream()` will be read-only.
  304. __Note #2__: This option is not enumerable and will not be merged with the instance defaults.
  305. */
  306. form?: Record<string, any>;
  307. /**
  308. JSON body. If the `Content-Type` header is not set, it will be set to `application/json`.
  309. __Note #1__: If you provide this option, `got.stream()` will be read-only.
  310. __Note #2__: This option is not enumerable and will not be merged with the instance defaults.
  311. */
  312. json?: Record<string, any>;
  313. /**
  314. The URL to request, as a string, a [`https.request` options object](https://nodejs.org/api/https.html#https_https_request_options_callback), or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).
  315. Properties from `options` will override properties in the parsed `url`.
  316. If no protocol is specified, it will throw a `TypeError`.
  317. __Note__: The query string is **not** parsed as search params.
  318. @example
  319. ```
  320. got('https://example.com/?query=a b'); //=> https://example.com/?query=a%20b
  321. got('https://example.com/', {searchParams: {query: 'a b'}}); //=> https://example.com/?query=a+b
  322. // The query string is overridden by `searchParams`
  323. got('https://example.com/?query=a b', {searchParams: {query: 'a b'}}); //=> https://example.com/?query=a+b
  324. ```
  325. */
  326. url?: string | URL;
  327. /**
  328. Cookie support. You don't have to care about parsing or how to store them.
  329. __Note__: If you provide this option, `options.headers.cookie` will be overridden.
  330. */
  331. cookieJar?: PromiseCookieJar | ToughCookieJar;
  332. /**
  333. Ignore invalid cookies instead of throwing an error.
  334. Only useful when the `cookieJar` option has been set. Not recommended.
  335. @default false
  336. */
  337. ignoreInvalidCookies?: boolean;
  338. /**
  339. Query string that will be added to the request URL.
  340. This will override the query string in `url`.
  341. If you need to pass in an array, you can do it using a `URLSearchParams` instance.
  342. @example
  343. ```
  344. const got = require('got');
  345. const searchParams = new URLSearchParams([['key', 'a'], ['key', 'b']]);
  346. got('https://example.com', {searchParams});
  347. console.log(searchParams.toString());
  348. //=> 'key=a&key=b'
  349. ```
  350. */
  351. searchParams?: string | Record<string, string | number | boolean | null | undefined> | URLSearchParams;
  352. /**
  353. An instance of [`CacheableLookup`](https://github.com/szmarczak/cacheable-lookup) used for making DNS lookups.
  354. Useful when making lots of requests to different *public* hostnames.
  355. `CacheableLookup` uses `dns.resolver4(..)` and `dns.resolver6(...)` under the hood and fall backs to `dns.lookup(...)` when the first two fail, which may lead to additional delay.
  356. __Note__: This should stay disabled when making requests to internal hostnames such as `localhost`, `database.local` etc.
  357. @default false
  358. */
  359. dnsCache?: CacheableLookup | boolean;
  360. /**
  361. User data. In contrast to other options, `context` is not enumerable.
  362. __Note__: The object is never merged, it's just passed through.
  363. Got will not modify the object in any way.
  364. @example
  365. ```
  366. const got = require('got');
  367. const instance = got.extend({
  368. hooks: {
  369. beforeRequest: [
  370. options => {
  371. if (!options.context || !options.context.token) {
  372. throw new Error('Token required');
  373. }
  374. options.headers.token = options.context.token;
  375. }
  376. ]
  377. }
  378. });
  379. (async () => {
  380. const context = {
  381. token: 'secret'
  382. };
  383. const response = await instance('https://httpbin.org/headers', {context});
  384. // Let's see the headers
  385. console.log(response.body);
  386. })();
  387. ```
  388. */
  389. context?: Record<string, unknown>;
  390. /**
  391. Hooks allow modifications during the request lifecycle.
  392. Hook functions may be async and are run serially.
  393. */
  394. hooks?: Hooks;
  395. /**
  396. Defines if redirect responses should be followed automatically.
  397. Note that if a `303` is sent by the server in response to any request type (`POST`, `DELETE`, etc.), Got will automatically request the resource pointed to in the location header via `GET`.
  398. This is in accordance with [the spec](https://tools.ietf.org/html/rfc7231#section-6.4.4).
  399. @default true
  400. */
  401. followRedirect?: boolean;
  402. /**
  403. If exceeded, the request will be aborted and a `MaxRedirectsError` will be thrown.
  404. @default 10
  405. */
  406. maxRedirects?: number;
  407. /**
  408. A cache adapter instance for storing cached response data.
  409. @default false
  410. */
  411. cache?: string | CacheableRequest.StorageAdapter | false;
  412. /**
  413. Determines if a `got.HTTPError` is thrown for unsuccessful responses.
  414. If this is disabled, requests that encounter an error status code will be resolved with the `response` instead of throwing.
  415. This may be useful if you are checking for resource availability and are expecting error responses.
  416. @default true
  417. */
  418. throwHttpErrors?: boolean;
  419. username?: string;
  420. password?: string;
  421. /**
  422. If set to `true`, Got will additionally accept HTTP2 requests.
  423. It will choose either HTTP/1.1 or HTTP/2 depending on the ALPN protocol.
  424. __Note__: Overriding `options.request` will disable HTTP2 support.
  425. __Note__: This option will default to `true` in the next upcoming major release.
  426. @default false
  427. @example
  428. ```
  429. const got = require('got');
  430. (async () => {
  431. const {headers} = await got('https://nghttp2.org/httpbin/anything', {http2: true});
  432. console.log(headers.via);
  433. //=> '2 nghttpx'
  434. })();
  435. ```
  436. */
  437. http2?: boolean;
  438. /**
  439. Set this to `true` to allow sending body for the `GET` method.
  440. However, the [HTTP/2 specification](https://tools.ietf.org/html/rfc7540#section-8.1.3) says that `An HTTP GET request includes request header fields and no payload body`, therefore when using the HTTP/2 protocol this option will have no effect.
  441. This option is only meant to interact with non-compliant servers when you have no other choice.
  442. __Note__: The [RFC 7321](https://tools.ietf.org/html/rfc7231#section-4.3.1) doesn't specify any particular behavior for the GET method having a payload, therefore __it's considered an [anti-pattern](https://en.wikipedia.org/wiki/Anti-pattern)__.
  443. @default false
  444. */
  445. allowGetBody?: boolean;
  446. lookup?: CacheableLookup['lookup'];
  447. /**
  448. Request headers.
  449. Existing headers will be overwritten. Headers set to `undefined` will be omitted.
  450. @default {}
  451. */
  452. headers?: Headers;
  453. /**
  454. By default, redirects will use [method rewriting](https://tools.ietf.org/html/rfc7231#section-6.4).
  455. For example, when sending a POST request and receiving a `302`, it will resend the body to the new location using the same HTTP method (`POST` in this case).
  456. @default true
  457. */
  458. methodRewriting?: boolean;
  459. /**
  460. Indicates which DNS record family to use.
  461. Values:
  462. - `auto`: IPv4 (if present) or IPv6
  463. - `ipv4`: Only IPv4
  464. - `ipv6`: Only IPv6
  465. __Note__: If you are using the undocumented option `family`, `dnsLookupIpVersion` will override it.
  466. @default 'auto'
  467. */
  468. dnsLookupIpVersion?: DnsLookupIpVersion;
  469. /**
  470. A function used to parse JSON responses.
  471. @example
  472. ```
  473. const got = require('got');
  474. const Bourne = require('@hapi/bourne');
  475. (async () => {
  476. const parsed = await got('https://example.com', {
  477. parseJson: text => Bourne.parse(text)
  478. }).json();
  479. console.log(parsed);
  480. })();
  481. ```
  482. */
  483. parseJson?: ParseJsonFunction;
  484. /**
  485. A function used to stringify the body of JSON requests.
  486. @example
  487. ```
  488. const got = require('got');
  489. (async () => {
  490. await got.post('https://example.com', {
  491. stringifyJson: object => JSON.stringify(object, (key, value) => {
  492. if (key.startsWith('_')) {
  493. return;
  494. }
  495. return value;
  496. }),
  497. json: {
  498. some: 'payload',
  499. _ignoreMe: 1234
  500. }
  501. });
  502. })();
  503. ```
  504. @example
  505. ```
  506. const got = require('got');
  507. (async () => {
  508. await got.post('https://example.com', {
  509. stringifyJson: object => JSON.stringify(object, (key, value) => {
  510. if (typeof value === 'number') {
  511. return value.toString();
  512. }
  513. return value;
  514. }),
  515. json: {
  516. some: 'payload',
  517. number: 1
  518. }
  519. });
  520. })();
  521. ```
  522. */
  523. stringifyJson?: StringifyJsonFunction;
  524. /**
  525. An object representing `limit`, `calculateDelay`, `methods`, `statusCodes`, `maxRetryAfter` and `errorCodes` fields for maximum retry count, retry handler, allowed methods, allowed status codes, maximum [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) time and allowed error codes.
  526. Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 1).
  527. The `calculateDelay` property is a `function` that receives an object with `attemptCount`, `retryOptions`, `error` and `computedValue` properties for current retry count, the retry options, error and default computed value.
  528. The function must return a delay in milliseconds (or a Promise resolving with it) (`0` return value cancels retry).
  529. By default, it retries *only* on the specified methods, status codes, and on these network errors:
  530. - `ETIMEDOUT`: One of the [timeout](#timeout) limits were reached.
  531. - `ECONNRESET`: Connection was forcibly closed by a peer.
  532. - `EADDRINUSE`: Could not bind to any free port.
  533. - `ECONNREFUSED`: Connection was refused by the server.
  534. - `EPIPE`: The remote side of the stream being written has been closed.
  535. - `ENOTFOUND`: Couldn't resolve the hostname to an IP address.
  536. - `ENETUNREACH`: No internet connection.
  537. - `EAI_AGAIN`: DNS lookup timed out.
  538. __Note__: If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`.
  539. __Note__: If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will cancel the request.
  540. */
  541. retry?: Partial<RequiredRetryOptions> | number;
  542. /**
  543. The IP address used to send the request from.
  544. */
  545. localAddress?: string;
  546. socketPath?: string;
  547. /**
  548. The HTTP method used to make the request.
  549. @default 'GET'
  550. */
  551. method?: Method;
  552. createConnection?: (options: http.RequestOptions, oncreate: (error: Error, socket: Socket) => void) => Socket;
  553. cacheOptions?: CacheOptions;
  554. /**
  555. If set to `false`, all invalid SSL certificates will be ignored and no error will be thrown.
  556. If set to `true`, it will throw an error whenever an invalid SSL certificate is detected.
  557. We strongly recommend to have this set to `true` for security reasons.
  558. @default true
  559. @example
  560. ```
  561. const got = require('got');
  562. (async () => {
  563. // Correct:
  564. await got('https://example.com', {rejectUnauthorized: true});
  565. // You can disable it when developing an HTTPS app:
  566. await got('https://localhost', {rejectUnauthorized: false});
  567. // Never do this:
  568. await got('https://example.com', {rejectUnauthorized: false});
  569. })();
  570. ```
  571. */
  572. rejectUnauthorized?: boolean;
  573. /**
  574. Options for the advanced HTTPS API.
  575. */
  576. https?: HTTPSOptions;
  577. }
  578. export interface Options extends PromiseOnly.Options, PlainOptions {
  579. }
  580. export interface HTTPSOptions {
  581. rejectUnauthorized?: https.RequestOptions['rejectUnauthorized'];
  582. checkServerIdentity?: CheckServerIdentityFunction;
  583. /**
  584. Override the default Certificate Authorities ([from Mozilla](https://ccadb-public.secure.force.com/mozilla/IncludedCACertificateReport)).
  585. @example
  586. ```
  587. // Single Certificate Authority
  588. got('https://example.com', {
  589. https: {
  590. certificateAuthority: fs.readFileSync('./my_ca.pem')
  591. }
  592. });
  593. ```
  594. */
  595. certificateAuthority?: SecureContextOptions['ca'];
  596. /**
  597. Private keys in [PEM](https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail) format.
  598. [PEM](https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail) allows the option of private keys being encrypted.
  599. Encrypted keys will be decrypted with `options.https.passphrase`.
  600. Multiple keys with different passphrases can be provided as an array of `{pem: <string | Buffer>, passphrase: <string>}`
  601. */
  602. key?: SecureContextOptions['key'];
  603. /**
  604. [Certificate chains](https://en.wikipedia.org/wiki/X.509#Certificate_chains_and_cross-certification) in [PEM](https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail) format.
  605. One cert chain should be provided per private key (`options.https.key`).
  606. When providing multiple cert chains, they do not have to be in the same order as their private keys in `options.https.key`.
  607. If the intermediate certificates are not provided, the peer will not be able to validate the certificate, and the handshake will fail.
  608. */
  609. certificate?: SecureContextOptions['cert'];
  610. /**
  611. The passphrase to decrypt the `options.https.key` (if different keys have different passphrases refer to `options.https.key` documentation).
  612. */
  613. passphrase?: SecureContextOptions['passphrase'];
  614. pfx?: SecureContextOptions['pfx'];
  615. }
  616. interface NormalizedPlainOptions extends PlainOptions {
  617. method: Method;
  618. url: URL;
  619. timeout: Delays;
  620. prefixUrl: string;
  621. ignoreInvalidCookies: boolean;
  622. decompress: boolean;
  623. searchParams?: URLSearchParams;
  624. cookieJar?: PromiseCookieJar;
  625. headers: Headers;
  626. context: Record<string, unknown>;
  627. hooks: Required<Hooks>;
  628. followRedirect: boolean;
  629. maxRedirects: number;
  630. cache?: string | CacheableRequest.StorageAdapter;
  631. throwHttpErrors: boolean;
  632. dnsCache?: CacheableLookup;
  633. http2: boolean;
  634. allowGetBody: boolean;
  635. rejectUnauthorized: boolean;
  636. lookup?: CacheableLookup['lookup'];
  637. methodRewriting: boolean;
  638. username: string;
  639. password: string;
  640. parseJson: ParseJsonFunction;
  641. stringifyJson: StringifyJsonFunction;
  642. retry: RequiredRetryOptions;
  643. cacheOptions: CacheOptions;
  644. [kRequest]: HttpRequestFunction;
  645. [kIsNormalizedAlready]?: boolean;
  646. }
  647. export interface NormalizedOptions extends PromiseOnly.NormalizedOptions, NormalizedPlainOptions {
  648. }
  649. interface PlainDefaults {
  650. timeout: Delays;
  651. prefixUrl: string;
  652. method: Method;
  653. ignoreInvalidCookies: boolean;
  654. decompress: boolean;
  655. context: Record<string, unknown>;
  656. cookieJar?: PromiseCookieJar | ToughCookieJar;
  657. dnsCache?: CacheableLookup;
  658. headers: Headers;
  659. hooks: Required<Hooks>;
  660. followRedirect: boolean;
  661. maxRedirects: number;
  662. cache?: string | CacheableRequest.StorageAdapter;
  663. throwHttpErrors: boolean;
  664. http2: boolean;
  665. allowGetBody: boolean;
  666. https?: HTTPSOptions;
  667. methodRewriting: boolean;
  668. parseJson: ParseJsonFunction;
  669. stringifyJson: StringifyJsonFunction;
  670. retry: RequiredRetryOptions;
  671. agent?: Agents | false;
  672. request?: RequestFunction;
  673. searchParams?: URLSearchParams;
  674. lookup?: CacheableLookup['lookup'];
  675. localAddress?: string;
  676. createConnection?: Options['createConnection'];
  677. cacheOptions: CacheOptions;
  678. }
  679. export interface Defaults extends PromiseOnly.Defaults, PlainDefaults {
  680. }
  681. export interface Progress {
  682. percent: number;
  683. transferred: number;
  684. total?: number;
  685. }
  686. export interface PlainResponse extends IncomingMessageWithTimings {
  687. /**
  688. The original request URL.
  689. */
  690. requestUrl: string;
  691. /**
  692. The redirect URLs.
  693. */
  694. redirectUrls: string[];
  695. /**
  696. - `options` - The Got options that were set on this request.
  697. __Note__: This is not a [http.ClientRequest](https://nodejs.org/api/http.html#http_class_http_clientrequest).
  698. */
  699. request: Request;
  700. /**
  701. The remote IP address.
  702. This is hopefully a temporary limitation, see [lukechilds/cacheable-request#86](https://github.com/lukechilds/cacheable-request/issues/86).
  703. __Note__: Not available when the response is cached.
  704. */
  705. ip?: string;
  706. /**
  707. Whether the response was retrieved from the cache.
  708. */
  709. isFromCache: boolean;
  710. /**
  711. The status code of the response.
  712. */
  713. statusCode: number;
  714. /**
  715. The request URL or the final URL after redirects.
  716. */
  717. url: string;
  718. /**
  719. The object contains the following properties:
  720. - `start` - Time when the request started.
  721. - `socket` - Time when a socket was assigned to the request.
  722. - `lookup` - Time when the DNS lookup finished.
  723. - `connect` - Time when the socket successfully connected.
  724. - `secureConnect` - Time when the socket securely connected.
  725. - `upload` - Time when the request finished uploading.
  726. - `response` - Time when the request fired `response` event.
  727. - `end` - Time when the response fired `end` event.
  728. - `error` - Time when the request fired `error` event.
  729. - `abort` - Time when the request fired `abort` event.
  730. - `phases`
  731. - `wait` - `timings.socket - timings.start`
  732. - `dns` - `timings.lookup - timings.socket`
  733. - `tcp` - `timings.connect - timings.lookup`
  734. - `tls` - `timings.secureConnect - timings.connect`
  735. - `request` - `timings.upload - (timings.secureConnect || timings.connect)`
  736. - `firstByte` - `timings.response - timings.upload`
  737. - `download` - `timings.end - timings.response`
  738. - `total` - `(timings.end || timings.error || timings.abort) - timings.start`
  739. If something has not been measured yet, it will be `undefined`.
  740. __Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
  741. */
  742. timings: Timings;
  743. /**
  744. The number of times the request was retried.
  745. */
  746. retryCount: number;
  747. /**
  748. The raw result of the request.
  749. */
  750. rawBody?: Buffer;
  751. /**
  752. The result of the request.
  753. */
  754. body?: unknown;
  755. }
  756. export interface Response<T = unknown> extends PlainResponse {
  757. /**
  758. The result of the request.
  759. */
  760. body: T;
  761. /**
  762. The raw result of the request.
  763. */
  764. rawBody: Buffer;
  765. }
  766. export interface RequestEvents<T> {
  767. /**
  768. `request` event to get the request object of the request.
  769. __Tip__: You can use `request` event to abort requests.
  770. @example
  771. ```
  772. got.stream('https://github.com')
  773. .on('request', request => setTimeout(() => request.destroy(), 50));
  774. ```
  775. */
  776. on: ((name: 'request', listener: (request: http.ClientRequest) => void) => T)
  777. /**
  778. The `response` event to get the response object of the final request.
  779. */
  780. & (<R extends Response>(name: 'response', listener: (response: R) => void) => T)
  781. /**
  782. The `redirect` event to get the response object of a redirect. The second argument is options for the next request to the redirect location.
  783. */
  784. & (<R extends Response, N extends NormalizedOptions>(name: 'redirect', listener: (response: R, nextOptions: N) => void) => T)
  785. /**
  786. Progress events for uploading (sending a request) and downloading (receiving a response).
  787. The `progress` argument is an object like:
  788. ```js
  789. {
  790. percent: 0.1,
  791. transferred: 1024,
  792. total: 10240
  793. }
  794. ```
  795. If the `content-length` header is missing, `total` will be `undefined`.
  796. @example
  797. ```js
  798. (async () => {
  799. const response = await got('https://sindresorhus.com')
  800. .on('downloadProgress', progress => {
  801. // Report download progress
  802. })
  803. .on('uploadProgress', progress => {
  804. // Report upload progress
  805. });
  806. console.log(response);
  807. })();
  808. ```
  809. */
  810. & ((name: 'uploadProgress' | 'downloadProgress', listener: (progress: Progress) => void) => T)
  811. /**
  812. To enable retrying on a Got stream, it is required to have a `retry` handler attached.
  813. When this event is emitted, you should reset the stream you were writing to and prepare the body again.
  814. See `got.options.retry` for more information.
  815. */
  816. & ((name: 'retry', listener: (retryCount: number, error: RequestError) => void) => T);
  817. }
  818. export declare const setNonEnumerableProperties: (sources: Array<Options | Defaults | undefined>, to: Options) => void;
  819. /**
  820. An error to be thrown when a request fails.
  821. Contains a `code` property with error class code, like `ECONNREFUSED`.
  822. */
  823. export declare class RequestError extends Error {
  824. code: string;
  825. stack: string;
  826. readonly options: NormalizedOptions;
  827. readonly response?: Response;
  828. readonly request?: Request;
  829. readonly timings?: Timings;
  830. constructor(message: string, error: Partial<Error & {
  831. code?: string;
  832. }>, self: Request | NormalizedOptions);
  833. }
  834. /**
  835. An error to be thrown when the server redirects you more than ten times.
  836. Includes a `response` property.
  837. */
  838. export declare class MaxRedirectsError extends RequestError {
  839. readonly response: Response;
  840. readonly request: Request;
  841. readonly timings: Timings;
  842. constructor(request: Request);
  843. }
  844. /**
  845. An error to be thrown when the server response code is not 2xx nor 3xx if `options.followRedirect` is `true`, but always except for 304.
  846. Includes a `response` property.
  847. */
  848. export declare class HTTPError extends RequestError {
  849. readonly response: Response;
  850. readonly request: Request;
  851. readonly timings: Timings;
  852. constructor(response: Response);
  853. }
  854. /**
  855. An error to be thrown when a cache method fails.
  856. For example, if the database goes down or there's a filesystem error.
  857. */
  858. export declare class CacheError extends RequestError {
  859. readonly request: Request;
  860. constructor(error: Error, request: Request);
  861. }
  862. /**
  863. An error to be thrown when the request body is a stream and an error occurs while reading from that stream.
  864. */
  865. export declare class UploadError extends RequestError {
  866. readonly request: Request;
  867. constructor(error: Error, request: Request);
  868. }
  869. /**
  870. An error to be thrown when the request is aborted due to a timeout.
  871. Includes an `event` and `timings` property.
  872. */
  873. export declare class TimeoutError extends RequestError {
  874. readonly request: Request;
  875. readonly timings: Timings;
  876. readonly event: string;
  877. constructor(error: TimedOutTimeoutError, timings: Timings, request: Request);
  878. }
  879. /**
  880. An error to be thrown when reading from response stream fails.
  881. */
  882. export declare class ReadError extends RequestError {
  883. readonly request: Request;
  884. readonly response: Response;
  885. readonly timings: Timings;
  886. constructor(error: Error, request: Request);
  887. }
  888. /**
  889. An error to be thrown when given an unsupported protocol.
  890. */
  891. export declare class UnsupportedProtocolError extends RequestError {
  892. constructor(options: NormalizedOptions);
  893. }
  894. export default class Request extends Duplex implements RequestEvents<Request> {
  895. ['constructor']: typeof Request;
  896. [kUnproxyEvents]: () => void;
  897. _cannotHaveBody: boolean;
  898. [kDownloadedSize]: number;
  899. [kUploadedSize]: number;
  900. [kStopReading]: boolean;
  901. [kTriggerRead]: boolean;
  902. [kBody]: Options['body'];
  903. [kJobs]: Array<() => void>;
  904. [kRetryTimeout]?: NodeJS.Timeout;
  905. [kBodySize]?: number;
  906. [kServerResponsesPiped]: Set<ServerResponse>;
  907. [kIsFromCache]?: boolean;
  908. [kStartedReading]?: boolean;
  909. [kCancelTimeouts]?: () => void;
  910. [kResponseSize]?: number;
  911. [kResponse]?: IncomingMessageWithTimings;
  912. [kOriginalResponse]?: IncomingMessageWithTimings;
  913. [kRequest]?: ClientRequest;
  914. _noPipe?: boolean;
  915. _progressCallbacks: Array<() => void>;
  916. options: NormalizedOptions;
  917. requestUrl: string;
  918. requestInitialized: boolean;
  919. redirects: string[];
  920. retryCount: number;
  921. constructor(url: string | URL | undefined, options?: Options, defaults?: Defaults);
  922. static normalizeArguments(url?: string | URL, options?: Options, defaults?: Defaults): NormalizedOptions;
  923. _lockWrite(): void;
  924. _unlockWrite(): void;
  925. _finalizeBody(): Promise<void>;
  926. _onResponseBase(response: IncomingMessageWithTimings): Promise<void>;
  927. _onResponse(response: IncomingMessageWithTimings): Promise<void>;
  928. _onRequest(request: ClientRequest): void;
  929. _createCacheableRequest(url: URL, options: RequestOptions): Promise<ClientRequest | ResponseLike>;
  930. _makeRequest(): Promise<void>;
  931. _error(error: RequestError): Promise<void>;
  932. _beforeError(error: Error): void;
  933. _read(): void;
  934. _write(chunk: any, encoding: string | undefined, callback: (error?: Error | null) => void): void;
  935. _writeRequest(chunk: any, encoding: BufferEncoding | undefined, callback: (error?: Error | null) => void): void;
  936. _final(callback: (error?: Error | null) => void): void;
  937. _destroy(error: Error | null, callback: (error: Error | null) => void): void;
  938. get _isAboutToError(): boolean;
  939. /**
  940. The remote IP address.
  941. */
  942. get ip(): string | undefined;
  943. /**
  944. Indicates whether the request has been aborted or not.
  945. */
  946. get aborted(): boolean;
  947. get socket(): Socket | undefined;
  948. /**
  949. Progress event for downloading (receiving a response).
  950. */
  951. get downloadProgress(): Progress;
  952. /**
  953. Progress event for uploading (sending a request).
  954. */
  955. get uploadProgress(): Progress;
  956. /**
  957. The object contains the following properties:
  958. - `start` - Time when the request started.
  959. - `socket` - Time when a socket was assigned to the request.
  960. - `lookup` - Time when the DNS lookup finished.
  961. - `connect` - Time when the socket successfully connected.
  962. - `secureConnect` - Time when the socket securely connected.
  963. - `upload` - Time when the request finished uploading.
  964. - `response` - Time when the request fired `response` event.
  965. - `end` - Time when the response fired `end` event.
  966. - `error` - Time when the request fired `error` event.
  967. - `abort` - Time when the request fired `abort` event.
  968. - `phases`
  969. - `wait` - `timings.socket - timings.start`
  970. - `dns` - `timings.lookup - timings.socket`
  971. - `tcp` - `timings.connect - timings.lookup`
  972. - `tls` - `timings.secureConnect - timings.connect`
  973. - `request` - `timings.upload - (timings.secureConnect || timings.connect)`
  974. - `firstByte` - `timings.response - timings.upload`
  975. - `download` - `timings.end - timings.response`
  976. - `total` - `(timings.end || timings.error || timings.abort) - timings.start`
  977. If something has not been measured yet, it will be `undefined`.
  978. __Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
  979. */
  980. get timings(): Timings | undefined;
  981. /**
  982. Whether the response was retrieved from the cache.
  983. */
  984. get isFromCache(): boolean | undefined;
  985. pipe<T extends NodeJS.WritableStream>(destination: T, options?: {
  986. end?: boolean;
  987. }): T;
  988. unpipe<T extends NodeJS.WritableStream>(destination: T): this;
  989. }
  990. export {};