GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
19 KiB

2 months ago
  1. node-fetch
  2. ==========
  3. [![npm version][npm-image]][npm-url]
  4. [![build status][travis-image]][travis-url]
  5. [![coverage status][codecov-image]][codecov-url]
  6. [![install size][install-size-image]][install-size-url]
  7. [![Discord][discord-image]][discord-url]
  8. A light-weight module that brings `window.fetch` to Node.js
  9. (We are looking for [v2 maintainers and collaborators](https://github.com/bitinn/node-fetch/issues/567))
  10. [![Backers][opencollective-image]][opencollective-url]
  11. <!-- TOC -->
  12. - [Motivation](#motivation)
  13. - [Features](#features)
  14. - [Difference from client-side fetch](#difference-from-client-side-fetch)
  15. - [Installation](#installation)
  16. - [Loading and configuring the module](#loading-and-configuring-the-module)
  17. - [Common Usage](#common-usage)
  18. - [Plain text or HTML](#plain-text-or-html)
  19. - [JSON](#json)
  20. - [Simple Post](#simple-post)
  21. - [Post with JSON](#post-with-json)
  22. - [Post with form parameters](#post-with-form-parameters)
  23. - [Handling exceptions](#handling-exceptions)
  24. - [Handling client and server errors](#handling-client-and-server-errors)
  25. - [Advanced Usage](#advanced-usage)
  26. - [Streams](#streams)
  27. - [Buffer](#buffer)
  28. - [Accessing Headers and other Meta data](#accessing-headers-and-other-meta-data)
  29. - [Extract Set-Cookie Header](#extract-set-cookie-header)
  30. - [Post data using a file stream](#post-data-using-a-file-stream)
  31. - [Post with form-data (detect multipart)](#post-with-form-data-detect-multipart)
  32. - [Request cancellation with AbortSignal](#request-cancellation-with-abortsignal)
  33. - [API](#api)
  34. - [fetch(url[, options])](#fetchurl-options)
  35. - [Options](#options)
  36. - [Class: Request](#class-request)
  37. - [Class: Response](#class-response)
  38. - [Class: Headers](#class-headers)
  39. - [Interface: Body](#interface-body)
  40. - [Class: FetchError](#class-fetcherror)
  41. - [License](#license)
  42. - [Acknowledgement](#acknowledgement)
  43. <!-- /TOC -->
  44. ## Motivation
  45. Instead of implementing `XMLHttpRequest` in Node.js to run browser-specific [Fetch polyfill](https://github.com/github/fetch), why not go from native `http` to `fetch` API directly? Hence, `node-fetch`, minimal code for a `window.fetch` compatible API on Node.js runtime.
  46. See Matt Andrews' [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch) or Leonardo Quixada's [cross-fetch](https://github.com/lquixada/cross-fetch) for isomorphic usage (exports `node-fetch` for server-side, `whatwg-fetch` for client-side).
  47. ## Features
  48. - Stay consistent with `window.fetch` API.
  49. - Make conscious trade-off when following [WHATWG fetch spec][whatwg-fetch] and [stream spec](https://streams.spec.whatwg.org/) implementation details, document known differences.
  50. - Use native promise but allow substituting it with [insert your favorite promise library].
  51. - Use native Node streams for body on both request and response.
  52. - Decode content encoding (gzip/deflate) properly and convert string output (such as `res.text()` and `res.json()`) to UTF-8 automatically.
  53. - Useful extensions such as timeout, redirect limit, response size limit, [explicit errors](ERROR-HANDLING.md) for troubleshooting.
  54. ## Difference from client-side fetch
  55. - See [Known Differences](LIMITS.md) for details.
  56. - If you happen to use a missing feature that `window.fetch` offers, feel free to open an issue.
  57. - Pull requests are welcomed too!
  58. ## Installation
  59. Current stable release (`2.x`)
  60. ```sh
  61. $ npm install node-fetch
  62. ```
  63. ## Loading and configuring the module
  64. We suggest you load the module via `require` until the stabilization of ES modules in node:
  65. ```js
  66. const fetch = require('node-fetch');
  67. ```
  68. If you are using a Promise library other than native, set it through `fetch.Promise`:
  69. ```js
  70. const Bluebird = require('bluebird');
  71. fetch.Promise = Bluebird;
  72. ```
  73. ## Common Usage
  74. NOTE: The documentation below is up-to-date with `2.x` releases; see the [`1.x` readme](https://github.com/bitinn/node-fetch/blob/1.x/README.md), [changelog](https://github.com/bitinn/node-fetch/blob/1.x/CHANGELOG.md) and [2.x upgrade guide](UPGRADE-GUIDE.md) for the differences.
  75. #### Plain text or HTML
  76. ```js
  77. fetch('https://github.com/')
  78. .then(res => res.text())
  79. .then(body => console.log(body));
  80. ```
  81. #### JSON
  82. ```js
  83. fetch('https://api.github.com/users/github')
  84. .then(res => res.json())
  85. .then(json => console.log(json));
  86. ```
  87. #### Simple Post
  88. ```js
  89. fetch('https://httpbin.org/post', { method: 'POST', body: 'a=1' })
  90. .then(res => res.json()) // expecting a json response
  91. .then(json => console.log(json));
  92. ```
  93. #### Post with JSON
  94. ```js
  95. const body = { a: 1 };
  96. fetch('https://httpbin.org/post', {
  97. method: 'post',
  98. body: JSON.stringify(body),
  99. headers: { 'Content-Type': 'application/json' },
  100. })
  101. .then(res => res.json())
  102. .then(json => console.log(json));
  103. ```
  104. #### Post with form parameters
  105. `URLSearchParams` is available in Node.js as of v7.5.0. See [official documentation](https://nodejs.org/api/url.html#url_class_urlsearchparams) for more usage methods.
  106. NOTE: The `Content-Type` header is only set automatically to `x-www-form-urlencoded` when an instance of `URLSearchParams` is given as such:
  107. ```js
  108. const { URLSearchParams } = require('url');
  109. const params = new URLSearchParams();
  110. params.append('a', 1);
  111. fetch('https://httpbin.org/post', { method: 'POST', body: params })
  112. .then(res => res.json())
  113. .then(json => console.log(json));
  114. ```
  115. #### Handling exceptions
  116. NOTE: 3xx-5xx responses are *NOT* exceptions and should be handled in `then()`; see the next section for more information.
  117. Adding a catch to the fetch promise chain will catch *all* exceptions, such as errors originating from node core libraries, network errors and operational errors, which are instances of FetchError. See the [error handling document](ERROR-HANDLING.md) for more details.
  118. ```js
  119. fetch('https://domain.invalid/')
  120. .catch(err => console.error(err));
  121. ```
  122. #### Handling client and server errors
  123. It is common to create a helper function to check that the response contains no client (4xx) or server (5xx) error responses:
  124. ```js
  125. function checkStatus(res) {
  126. if (res.ok) { // res.status >= 200 && res.status < 300
  127. return res;
  128. } else {
  129. throw MyCustomError(res.statusText);
  130. }
  131. }
  132. fetch('https://httpbin.org/status/400')
  133. .then(checkStatus)
  134. .then(res => console.log('will not get here...'))
  135. ```
  136. ## Advanced Usage
  137. #### Streams
  138. The "Node.js way" is to use streams when possible:
  139. ```js
  140. fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
  141. .then(res => {
  142. const dest = fs.createWriteStream('./octocat.png');
  143. res.body.pipe(dest);
  144. });
  145. ```
  146. #### Buffer
  147. If you prefer to cache binary data in full, use buffer(). (NOTE: `buffer()` is a `node-fetch`-only API)
  148. ```js
  149. const fileType = require('file-type');
  150. fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
  151. .then(res => res.buffer())
  152. .then(buffer => fileType(buffer))
  153. .then(type => { /* ... */ });
  154. ```
  155. #### Accessing Headers and other Meta data
  156. ```js
  157. fetch('https://github.com/')
  158. .then(res => {
  159. console.log(res.ok);
  160. console.log(res.status);
  161. console.log(res.statusText);
  162. console.log(res.headers.raw());
  163. console.log(res.headers.get('content-type'));
  164. });
  165. ```
  166. #### Extract Set-Cookie Header
  167. Unlike browsers, you can access raw `Set-Cookie` headers manually using `Headers.raw()`. This is a `node-fetch` only API.
  168. ```js
  169. fetch(url).then(res => {
  170. // returns an array of values, instead of a string of comma-separated values
  171. console.log(res.headers.raw()['set-cookie']);
  172. });
  173. ```
  174. #### Post data using a file stream
  175. ```js
  176. const { createReadStream } = require('fs');
  177. const stream = createReadStream('input.txt');
  178. fetch('https://httpbin.org/post', { method: 'POST', body: stream })
  179. .then(res => res.json())
  180. .then(json => console.log(json));
  181. ```
  182. #### Post with form-data (detect multipart)
  183. ```js
  184. const FormData = require('form-data');
  185. const form = new FormData();
  186. form.append('a', 1);
  187. fetch('https://httpbin.org/post', { method: 'POST', body: form })
  188. .then(res => res.json())
  189. .then(json => console.log(json));
  190. // OR, using custom headers
  191. // NOTE: getHeaders() is non-standard API
  192. const form = new FormData();
  193. form.append('a', 1);
  194. const options = {
  195. method: 'POST',
  196. body: form,
  197. headers: form.getHeaders()
  198. }
  199. fetch('https://httpbin.org/post', options)
  200. .then(res => res.json())
  201. .then(json => console.log(json));
  202. ```
  203. #### Request cancellation with AbortSignal
  204. > NOTE: You may cancel streamed requests only on Node >= v8.0.0
  205. You may cancel requests with `AbortController`. A suggested implementation is [`abort-controller`](https://www.npmjs.com/package/abort-controller).
  206. An example of timing out a request after 150ms could be achieved as the following:
  207. ```js
  208. import AbortController from 'abort-controller';
  209. const controller = new AbortController();
  210. const timeout = setTimeout(
  211. () => { controller.abort(); },
  212. 150,
  213. );
  214. fetch(url, { signal: controller.signal })
  215. .then(res => res.json())
  216. .then(
  217. data => {
  218. useData(data)
  219. },
  220. err => {
  221. if (err.name === 'AbortError') {
  222. // request was aborted
  223. }
  224. },
  225. )
  226. .finally(() => {
  227. clearTimeout(timeout);
  228. });
  229. ```
  230. See [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples.
  231. ## API
  232. ### fetch(url[, options])
  233. - `url` A string representing the URL for fetching
  234. - `options` [Options](#fetch-options) for the HTTP(S) request
  235. - Returns: <code>Promise&lt;[Response](#class-response)&gt;</code>
  236. Perform an HTTP(S) fetch.
  237. `url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
  238. <a id="fetch-options"></a>
  239. ### Options
  240. The default values are shown after each option key.
  241. ```js
  242. {
  243. // These properties are part of the Fetch Standard
  244. method: 'GET',
  245. headers: {}, // request headers. format is the identical to that accepted by the Headers constructor (see below)
  246. body: null, // request body. can be null, a string, a Buffer, a Blob, or a Node.js Readable stream
  247. redirect: 'follow', // set to `manual` to extract redirect headers, `error` to reject redirect
  248. signal: null, // pass an instance of AbortSignal to optionally abort requests
  249. // The following properties are node-fetch extensions
  250. follow: 20, // maximum redirect count. 0 to not follow redirect
  251. timeout: 0, // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies). Signal is recommended instead.
  252. compress: true, // support gzip/deflate content encoding. false to disable
  253. size: 0, // maximum response body size in bytes. 0 to disable
  254. agent: null // http(s).Agent instance or function that returns an instance (see below)
  255. }
  256. ```
  257. ##### Default Headers
  258. If no values are set, the following request headers will be sent automatically:
  259. Header | Value
  260. ------------------- | --------------------------------------------------------
  261. `Accept-Encoding` | `gzip,deflate` _(when `options.compress === true`)_
  262. `Accept` | `*/*`
  263. `Connection` | `close` _(when no `options.agent` is present)_
  264. `Content-Length` | _(automatically calculated, if possible)_
  265. `Transfer-Encoding` | `chunked` _(when `req.body` is a stream)_
  266. `User-Agent` | `node-fetch/1.0 (+https://github.com/bitinn/node-fetch)`
  267. Note: when `body` is a `Stream`, `Content-Length` is not set automatically.
  268. ##### Custom Agent
  269. The `agent` option allows you to specify networking related options which are out of the scope of Fetch, including and not limited to the following:
  270. - Support self-signed certificate
  271. - Use only IPv4 or IPv6
  272. - Custom DNS Lookup
  273. See [`http.Agent`](https://nodejs.org/api/http.html#http_new_agent_options) for more information.
  274. In addition, the `agent` option accepts a function that returns `http`(s)`.Agent` instance given current [URL](https://nodejs.org/api/url.html), this is useful during a redirection chain across HTTP and HTTPS protocol.
  275. ```js
  276. const httpAgent = new http.Agent({
  277. keepAlive: true
  278. });
  279. const httpsAgent = new https.Agent({
  280. keepAlive: true
  281. });
  282. const options = {
  283. agent: function (_parsedURL) {
  284. if (_parsedURL.protocol == 'http:') {
  285. return httpAgent;
  286. } else {
  287. return httpsAgent;
  288. }
  289. }
  290. }
  291. ```
  292. <a id="class-request"></a>
  293. ### Class: Request
  294. An HTTP(S) request containing information about URL, method, headers, and the body. This class implements the [Body](#iface-body) interface.
  295. Due to the nature of Node.js, the following properties are not implemented at this moment:
  296. - `type`
  297. - `destination`
  298. - `referrer`
  299. - `referrerPolicy`
  300. - `mode`
  301. - `credentials`
  302. - `cache`
  303. - `integrity`
  304. - `keepalive`
  305. The following node-fetch extension properties are provided:
  306. - `follow`
  307. - `compress`
  308. - `counter`
  309. - `agent`
  310. See [options](#fetch-options) for exact meaning of these extensions.
  311. #### new Request(input[, options])
  312. <small>*(spec-compliant)*</small>
  313. - `input` A string representing a URL, or another `Request` (which will be cloned)
  314. - `options` [Options][#fetch-options] for the HTTP(S) request
  315. Constructs a new `Request` object. The constructor is identical to that in the [browser](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request).
  316. In most cases, directly `fetch(url, options)` is simpler than creating a `Request` object.
  317. <a id="class-response"></a>
  318. ### Class: Response
  319. An HTTP(S) response. This class implements the [Body](#iface-body) interface.
  320. The following properties are not implemented in node-fetch at this moment:
  321. - `Response.error()`
  322. - `Response.redirect()`
  323. - `type`
  324. - `trailer`
  325. #### new Response([body[, options]])
  326. <small>*(spec-compliant)*</small>
  327. - `body` A `String` or [`Readable` stream][node-readable]
  328. - `options` A [`ResponseInit`][response-init] options dictionary
  329. Constructs a new `Response` object. The constructor is identical to that in the [browser](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response).
  330. Because Node.js does not implement service workers (for which this class was designed), one rarely has to construct a `Response` directly.
  331. #### response.ok
  332. <small>*(spec-compliant)*</small>
  333. Convenience property representing if the request ended normally. Will evaluate to true if the response status was greater than or equal to 200 but smaller than 300.
  334. #### response.redirected
  335. <small>*(spec-compliant)*</small>
  336. Convenience property representing if the request has been redirected at least once. Will evaluate to true if the internal redirect counter is greater than 0.
  337. <a id="class-headers"></a>
  338. ### Class: Headers
  339. This class allows manipulating and iterating over a set of HTTP headers. All methods specified in the [Fetch Standard][whatwg-fetch] are implemented.
  340. #### new Headers([init])
  341. <small>*(spec-compliant)*</small>
  342. - `init` Optional argument to pre-fill the `Headers` object
  343. Construct a new `Headers` object. `init` can be either `null`, a `Headers` object, an key-value map object or any iterable object.
  344. ```js
  345. // Example adapted from https://fetch.spec.whatwg.org/#example-headers-class
  346. const meta = {
  347. 'Content-Type': 'text/xml',
  348. 'Breaking-Bad': '<3'
  349. };
  350. const headers = new Headers(meta);
  351. // The above is equivalent to
  352. const meta = [
  353. [ 'Content-Type', 'text/xml' ],
  354. [ 'Breaking-Bad', '<3' ]
  355. ];
  356. const headers = new Headers(meta);
  357. // You can in fact use any iterable objects, like a Map or even another Headers
  358. const meta = new Map();
  359. meta.set('Content-Type', 'text/xml');
  360. meta.set('Breaking-Bad', '<3');
  361. const headers = new Headers(meta);
  362. const copyOfHeaders = new Headers(headers);
  363. ```
  364. <a id="iface-body"></a>
  365. ### Interface: Body
  366. `Body` is an abstract interface with methods that are applicable to both `Request` and `Response` classes.
  367. The following methods are not yet implemented in node-fetch at this moment:
  368. - `formData()`
  369. #### body.body
  370. <small>*(deviation from spec)*</small>
  371. * Node.js [`Readable` stream][node-readable]
  372. Data are encapsulated in the `Body` object. Note that while the [Fetch Standard][whatwg-fetch] requires the property to always be a WHATWG `ReadableStream`, in node-fetch it is a Node.js [`Readable` stream][node-readable].
  373. #### body.bodyUsed
  374. <small>*(spec-compliant)*</small>
  375. * `Boolean`
  376. A boolean property for if this body has been consumed. Per the specs, a consumed body cannot be used again.
  377. #### body.arrayBuffer()
  378. #### body.blob()
  379. #### body.json()
  380. #### body.text()
  381. <small>*(spec-compliant)*</small>
  382. * Returns: <code>Promise</code>
  383. Consume the body and return a promise that will resolve to one of these formats.
  384. #### body.buffer()
  385. <small>*(node-fetch extension)*</small>
  386. * Returns: <code>Promise&lt;Buffer&gt;</code>
  387. Consume the body and return a promise that will resolve to a Buffer.
  388. #### body.textConverted()
  389. <small>*(node-fetch extension)*</small>
  390. * Returns: <code>Promise&lt;String&gt;</code>
  391. Identical to `body.text()`, except instead of always converting to UTF-8, encoding sniffing will be performed and text converted to UTF-8 if possible.
  392. (This API requires an optional dependency of the npm package [encoding](https://www.npmjs.com/package/encoding), which you need to install manually. `webpack` users may see [a warning message](https://github.com/bitinn/node-fetch/issues/412#issuecomment-379007792) due to this optional dependency.)
  393. <a id="class-fetcherror"></a>
  394. ### Class: FetchError
  395. <small>*(node-fetch extension)*</small>
  396. An operational error in the fetching process. See [ERROR-HANDLING.md][] for more info.
  397. <a id="class-aborterror"></a>
  398. ### Class: AbortError
  399. <small>*(node-fetch extension)*</small>
  400. An Error thrown when the request is aborted in response to an `AbortSignal`'s `abort` event. It has a `name` property of `AbortError`. See [ERROR-HANDLING.MD][] for more info.
  401. ## Acknowledgement
  402. Thanks to [github/fetch](https://github.com/github/fetch) for providing a solid implementation reference.
  403. `node-fetch` v1 was maintained by [@bitinn](https://github.com/bitinn); v2 was maintained by [@TimothyGu](https://github.com/timothygu), [@bitinn](https://github.com/bitinn) and [@jimmywarting](https://github.com/jimmywarting); v2 readme is written by [@jkantr](https://github.com/jkantr).
  404. ## License
  405. MIT
  406. [npm-image]: https://flat.badgen.net/npm/v/node-fetch
  407. [npm-url]: https://www.npmjs.com/package/node-fetch
  408. [travis-image]: https://flat.badgen.net/travis/bitinn/node-fetch
  409. [travis-url]: https://travis-ci.org/bitinn/node-fetch
  410. [codecov-image]: https://flat.badgen.net/codecov/c/github/bitinn/node-fetch/master
  411. [codecov-url]: https://codecov.io/gh/bitinn/node-fetch
  412. [install-size-image]: https://flat.badgen.net/packagephobia/install/node-fetch
  413. [install-size-url]: https://packagephobia.now.sh/result?p=node-fetch
  414. [discord-image]: https://img.shields.io/discord/619915844268326952?color=%237289DA&label=Discord&style=flat-square
  415. [discord-url]: https://discord.gg/Zxbndcm
  416. [opencollective-image]: https://opencollective.com/node-fetch/backers.svg
  417. [opencollective-url]: https://opencollective.com/node-fetch
  418. [whatwg-fetch]: https://fetch.spec.whatwg.org/
  419. [response-init]: https://fetch.spec.whatwg.org/#responseinit
  420. [node-readable]: https://nodejs.org/api/stream.html#stream_readable_streams
  421. [mdn-headers]: https://developer.mozilla.org/en-US/docs/Web/API/Headers
  422. [LIMITS.md]: https://github.com/bitinn/node-fetch/blob/master/LIMITS.md
  423. [ERROR-HANDLING.md]: https://github.com/bitinn/node-fetch/blob/master/ERROR-HANDLING.md
  424. [UPGRADE-GUIDE.md]: https://github.com/bitinn/node-fetch/blob/master/UPGRADE-GUIDE.md