More documentation
This commit is contained in:
parent
25e43a8f3e
commit
a345c398b3
40
CHANGELOG.md
40
CHANGELOG.md
|
@ -9,27 +9,43 @@ Changelog
|
|||
|
||||
This is a major release. Check [our upgrade guide](https://github.com/bitinn/node-fetch/blob/master/UPGRADE-GUIDE.md) for an overview on some key differences between v1 and v2.
|
||||
|
||||
### General changes
|
||||
|
||||
- Major: Node.js 0.10.x and 0.12.x support is dropped
|
||||
- Major: overwrite user's `Content-Length` if we can be sure our information is correct (per spec)
|
||||
- Major: `response.text()` no longer attempts to detect encoding, instead always opting for UTF-8 (per spec); use `response.textConverted()` for the v1 behavior
|
||||
- Major: make `response.json()` throw error instead of returning an empty object on 204 no-content respose (per spec; reverts behavior changed in v1.6.2)
|
||||
- Major: remove `headers.getAll()`; make `get()` return all headers delimited by commas (per spec)
|
||||
- Major: internal methods are no longer exposed
|
||||
- Major: throw error when a `GET` or `HEAD` Request is constructed with a non-null body (per spec)
|
||||
- Major: `require('node-fetch/lib/response')` etc. is now unsupported; use `require('node-fetch').Response` or ES6 module imports
|
||||
- Enhance: start testing on Node.js 4, 6, 7
|
||||
- Enhance: use Rollup to produce a distributed bundle (less memory overhead and faster startup)
|
||||
- Enhance: make `toString()` on Headers, Requests, and Responses return correct IDL class strings
|
||||
- Enhance: make `Object.prototype.toString()` on Headers, Requests, and Responses return correct class strings
|
||||
- Other: rewrite in ES2015 using Babel
|
||||
- Other: use Codecov for code coverage tracking
|
||||
|
||||
### HTTP requests
|
||||
|
||||
- Major: overwrite user's `Content-Length` if we can be sure our information is correct (per spec)
|
||||
- Fix: support WHATWG URL objects, created by `whatwg-url` package or `require('url').URL` in Node.js 7+
|
||||
|
||||
### Response and Request classes
|
||||
|
||||
- Major: `response.text()` no longer attempts to detect encoding, instead always opting for UTF-8 (per spec); use `response.textConverted()` for the v1 behavior
|
||||
- Major: make `response.json()` throw error instead of returning an empty object on 204 no-content respose (per spec; reverts behavior changed in v1.6.2)
|
||||
- Major: internal methods are no longer exposed
|
||||
- Major: throw error when a `GET` or `HEAD` Request is constructed with a non-null body (per spec)
|
||||
- Enhance: add `response.arrayBuffer()` (also applies to Requests)
|
||||
- Enhance: add experimental `response.blob()` (also applies to Requests)
|
||||
- Fix: fix Request and Response with `null` body
|
||||
|
||||
### Headers class
|
||||
|
||||
- Major: remove `headers.getAll()`; make `get()` return all headers delimited by commas (per spec)
|
||||
- Enhance: make Headers iterable
|
||||
- Enhance: make Headers constructor accept an array of tuples
|
||||
- Enhance: make sure header names and values are valid in HTTP
|
||||
- Enhance: add a list of default headers in README
|
||||
- Fix: coerce Headers prototype function parameters to strings, where applicable
|
||||
- Fix: fix Request and Response with `null` body
|
||||
- Fix: support WHATWG URL objects, created by `whatwg-url` package or `require('url').URL` in Node.js 7+
|
||||
- Other: rewrite in ES2015 using Babel and Rollup
|
||||
- Other: use Codecov for code coverage tracking
|
||||
|
||||
### Documentation
|
||||
|
||||
- Enhance: more comprehensive API docs
|
||||
- Enhance: add a list of default headers in README
|
||||
|
||||
|
||||
# 1.x release
|
||||
|
|
13
LIMITS.md
13
LIMITS.md
|
@ -8,13 +8,15 @@ Known differences
|
|||
|
||||
- URL input must be an absolute URL, using either `http` or `https` as scheme.
|
||||
|
||||
- On the upside, there are no forbidden headers, and `res.url` contains the final url when following redirects.
|
||||
- On the upside, there are no forbidden headers.
|
||||
|
||||
- For convenience, `res.body` is a transform stream, so decoding can be handled independently.
|
||||
- `res.url` contains the final url when following redirects.
|
||||
|
||||
- Similarly, `req.body` can either be a string, a buffer or a readable stream.
|
||||
- For convenience, `res.body` is a Node.js [Readable stream][readable-stream], so decoding can be handled independently.
|
||||
|
||||
- Also, you can handle rejected fetch requests through checking `err.type` and `err.code`.
|
||||
- Similarly, `req.body` can either be `null`, a string, a buffer or a Readable stream.
|
||||
|
||||
- Also, you can handle rejected fetch requests through checking `err.type` and `err.code`. See [ERROR-HANDLING.md][] for more info.
|
||||
|
||||
- Only support `res.text()`, `res.json()`, `res.blob()`, `res.arraybuffer()`, `res.buffer()`
|
||||
|
||||
|
@ -23,3 +25,6 @@ Known differences
|
|||
- Current implementation lacks server-side cookie store, you will need to extract `Set-Cookie` headers manually.
|
||||
|
||||
- If you are using `res.clone()` and writing an isomorphic app, note that stream on Node.js have a smaller internal buffer size (16Kb, aka `highWaterMark`) from client-side browsers (>1Mb, not consistent across browsers).
|
||||
|
||||
[readable-stream]: https://nodejs.org/api/stream.html#stream_readable_streams
|
||||
[ERROR-HANDLING.md]: https://github.com/bitinn/node-fetch/blob/master/ERROR-HANDLING.md
|
||||
|
|
227
README.md
227
README.md
|
@ -9,36 +9,38 @@ node-fetch
|
|||
A light-weight module that brings `window.fetch` to Node.js
|
||||
|
||||
|
||||
# Motivation
|
||||
## Motivation
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
See Matt Andrews' [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch) for isomorphic usage (exports `node-fetch` for server-side, `whatwg-fetch` for client-side).
|
||||
|
||||
|
||||
# Features
|
||||
## Features
|
||||
|
||||
- Stay consistent with `window.fetch` API.
|
||||
- Make conscious trade-off when following [whatwg fetch spec](https://fetch.spec.whatwg.org/) and [stream spec](https://streams.spec.whatwg.org/) implementation details, document known difference.
|
||||
- Make conscious trade-off when following [whatwg fetch spec][whatwg-fetch] and [stream spec](https://streams.spec.whatwg.org/) implementation details, document known difference.
|
||||
- Use native promise, but allow substituting it with [insert your favorite promise library].
|
||||
- Use native stream for body, on both request and response.
|
||||
- Decode content encoding (gzip/deflate) properly, and convert string output (such as `res.text()` and `res.json()`) to UTF-8 automatically.
|
||||
- Useful extensions such as timeout, redirect limit, response size limit, [explicit errors](https://github.com/bitinn/node-fetch/blob/master/ERROR-HANDLING.md) for troubleshooting.
|
||||
- Useful extensions such as timeout, redirect limit, response size limit, [explicit errors][] for troubleshooting.
|
||||
|
||||
|
||||
# Difference from client-side fetch
|
||||
## Difference from client-side fetch
|
||||
|
||||
- See [Known Differences](https://github.com/bitinn/node-fetch/blob/master/LIMITS.md) for details.
|
||||
- If you happen to use a missing feature that `window.fetch` offers, feel free to open an issue.
|
||||
- Pull requests are welcomed too!
|
||||
|
||||
|
||||
# Install
|
||||
## Install
|
||||
|
||||
`npm install node-fetch --save`
|
||||
```sh
|
||||
$ npm install node-fetch --save
|
||||
```
|
||||
|
||||
|
||||
# Usage
|
||||
## Usage
|
||||
|
||||
```javascript
|
||||
import fetch from 'node-fetch';
|
||||
|
@ -159,35 +161,41 @@ fetch('http://httpbin.org/post', { method: 'POST', body: form, headers: form.get
|
|||
See [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples.
|
||||
|
||||
|
||||
# API
|
||||
## API
|
||||
|
||||
## fetch(url, options)
|
||||
### fetch(url[, options])
|
||||
|
||||
Returns a `Promise`
|
||||
- `url` A string representing the URL for fetching
|
||||
- `options` [Options](#fetch-options) for the HTTP(S) request
|
||||
- Returns: <code>Promise<[Response](#class-response)></code>
|
||||
|
||||
### Url
|
||||
Perform an HTTP(S) fetch.
|
||||
|
||||
Should be an absolute url, eg `http://example.com/`
|
||||
`url` should be an absolute url, such as `http://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.
|
||||
|
||||
### Options
|
||||
<a id="fetch-options"></a>
|
||||
#### Options
|
||||
|
||||
Note that only `method`, `headers`, `redirect` and `body` are allowed in `window.fetch`. Other options are node.js extensions. The default values are shown after each option key.
|
||||
The default values are shown after each option key.
|
||||
|
||||
```
|
||||
```js
|
||||
{
|
||||
method: 'GET'
|
||||
, headers: {} // request header. format {a:'1'} or {b:['1','2','3']}
|
||||
, redirect: 'follow' // set to `manual` to extract redirect headers, `error` to reject redirect
|
||||
, follow: 20 // maximum redirect count. 0 to not follow redirect
|
||||
, timeout: 0 // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies)
|
||||
, compress: true // support gzip/deflate content encoding. false to disable
|
||||
, size: 0 // maximum response body size in bytes. 0 to disable
|
||||
, body: empty // request body. can be a string, buffer, readable stream
|
||||
, agent: null // http.Agent instance, allows custom proxy, certificate etc.
|
||||
// These properties are part of the Fetch Standard
|
||||
method: 'GET',
|
||||
headers: {}, // request headers. format is the identical to that accepted by the Headers constructor (see below)
|
||||
body: null, // request body. can be null, a string, a Buffer, a Blob, or a Node.js Readable stream
|
||||
redirect: 'follow', // set to `manual` to extract redirect headers, `error` to reject redirect
|
||||
|
||||
// The following properties are node-fetch extensions
|
||||
follow: 20, // maximum redirect count. 0 to not follow redirect
|
||||
timeout: 0, // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies)
|
||||
compress: true, // support gzip/deflate content encoding. false to disable
|
||||
size: 0, // maximum response body size in bytes. 0 to disable
|
||||
agent: null // http(s).Agent instance, allows custom proxy, certificate etc.
|
||||
}
|
||||
```
|
||||
|
||||
#### Default Headers
|
||||
##### Default Headers
|
||||
|
||||
If no values are set, the following request headers will be sent automatically:
|
||||
|
||||
|
@ -199,13 +207,169 @@ Header | Value
|
|||
`Content-Length` | _(automatically calculated, if possible)_
|
||||
`User-Agent` | `node-fetch/1.0 (+https://github.com/bitinn/node-fetch)`
|
||||
|
||||
<a id="class-request"></a>
|
||||
### Class: Request
|
||||
|
||||
# License
|
||||
An HTTP(S) request containing information about URL, method, headers, and the body. This class implements the [Body](#iface-body) interface.
|
||||
|
||||
Due to the nature of Node.js, the following properties are not implemented at this moment:
|
||||
|
||||
- `type`
|
||||
- `destination`
|
||||
- `referrer`
|
||||
- `referrerPolicy`
|
||||
- `mode`
|
||||
- `credentials`
|
||||
- `cache`
|
||||
- `integrity`
|
||||
- `keepalive`
|
||||
|
||||
The following node-fetch extension properties are provided:
|
||||
|
||||
- `follow`
|
||||
- `compress`
|
||||
- `counter`
|
||||
- `agent`
|
||||
|
||||
See [options](#fetch-options) for exact meaning of these extensions.
|
||||
|
||||
#### new Request(input[, options])
|
||||
|
||||
<small>*(spec-compliant)*</small>
|
||||
|
||||
- `input` A string representing a URL, or another `Request` (which will be cloned)
|
||||
- `options` [Options][#fetch-options] for the HTTP(S) request
|
||||
|
||||
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).
|
||||
|
||||
In most cases, directly `fetch(url, options)` is simpler than creating a `Request` object.
|
||||
|
||||
<a id="class-response"></a>
|
||||
### Class: Response
|
||||
|
||||
An HTTP(S) response. This class implements the [Body](#iface-body) interface.
|
||||
|
||||
The following properties are not implemented in node-fetch at this moment:
|
||||
|
||||
- `Response.error()`
|
||||
- `Response.redirect()`
|
||||
- `type`
|
||||
- `redirected`
|
||||
- `trailer`
|
||||
|
||||
#### new Response([body[, options]])
|
||||
|
||||
<small>*(spec-compliant)*</small>
|
||||
|
||||
- `body` A string or [Readable stream][node-readable]
|
||||
- `options` A [`ResponseInit`][response-init] options dictionary
|
||||
|
||||
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).
|
||||
|
||||
Because Node.js does not implement service workers (for which this class was designed), one rarely has to construct a `Response` directly.
|
||||
|
||||
<a id="class-headers"></a>
|
||||
### Class: Headers
|
||||
|
||||
This class allows manipulating and iterating over a set of HTTP headers. All methods specified in the [Fetch Standard][whatwg-fetch] are implemented.
|
||||
|
||||
#### new Headers([init])
|
||||
|
||||
<small>*(spec-compliant)*</small>
|
||||
|
||||
- `init` Optional argument to pre-fill the `Headers` object
|
||||
|
||||
Construct a new `Headers` object. `init` can be either `null`, a `Headers` object, an key-value map object, or any iterable object.
|
||||
|
||||
```js
|
||||
// Example adapted from https://fetch.spec.whatwg.org/#example-headers-class
|
||||
|
||||
const meta = {
|
||||
'Content-Type': 'text/xml',
|
||||
'Breaking-Bad': '<3'
|
||||
};
|
||||
const headers = new Headers(meta);
|
||||
|
||||
// The above is equivalent to
|
||||
const meta = [
|
||||
[ 'Content-Type', 'text/xml' ],
|
||||
[ 'Breaking-Bad', '<3' ]
|
||||
];
|
||||
const headers = new Headers(meta);
|
||||
|
||||
// You can in fact use any iterable objects, like a Map or even another Headers
|
||||
const meta = new Map();
|
||||
meta.set('Content-Type', 'text/xml');
|
||||
meta.set('Breaking-Bad', '<3');
|
||||
const headers = new Headers(meta);
|
||||
const copyOfHeaders = new Headers(headers);
|
||||
```
|
||||
|
||||
<a id="iface-body"></a>
|
||||
### Interface: Body
|
||||
|
||||
`Body` is an abstract interface with methods that are applicable to both `Request` and `Response` classes.
|
||||
|
||||
The following methods are not yet implemented in node-fetch at this moment:
|
||||
|
||||
- `formData()`
|
||||
|
||||
#### body.body
|
||||
|
||||
<small>*(deviation from spec)*</small>
|
||||
|
||||
* Node.js [`Readable` stream][node-readable]
|
||||
|
||||
The data 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].
|
||||
|
||||
#### body.bodyUsed
|
||||
|
||||
<small>*(spec-compliant)*</small>
|
||||
|
||||
* `Boolean`
|
||||
|
||||
A boolean property for if this body has been consumed. Per spec, a consumed body cannot be used again.
|
||||
|
||||
#### body.arrayBuffer()
|
||||
#### body.blob()
|
||||
#### body.json()
|
||||
#### body.text()
|
||||
|
||||
<small>*(spec-compliant)*</small>
|
||||
|
||||
* Returns: <code>Promise</code>
|
||||
|
||||
Consume the body and return a promise that will resolve to one of these formats.
|
||||
|
||||
#### body.buffer()
|
||||
|
||||
<small>*(node-fetch extension)*</small>
|
||||
|
||||
* Returns: <code>Promise<Buffer></code>
|
||||
|
||||
Consume the body and return a promise that will resolve to a Buffer.
|
||||
|
||||
#### body.textConverted()
|
||||
|
||||
<small>*(node-fetch extension)*</small>
|
||||
|
||||
* Returns: <code>Promise<String></code>
|
||||
|
||||
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.
|
||||
|
||||
<a id="class-fetcherror"></a>
|
||||
### Class: FetchError
|
||||
|
||||
<small>*(node-fetch extension)*</small>
|
||||
|
||||
An operational error in the fetching process. See [ERROR-HANDLING.md][] for more info.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
|
||||
# Acknowledgement
|
||||
## Acknowledgement
|
||||
|
||||
Thanks to [github/fetch](https://github.com/github/fetch) for providing a solid implementation reference.
|
||||
|
||||
|
@ -216,3 +380,8 @@ Thanks to [github/fetch](https://github.com/github/fetch) for providing a solid
|
|||
[travis-url]: https://travis-ci.org/bitinn/node-fetch
|
||||
[codecov-image]: https://img.shields.io/codecov/c/github/bitinn/node-fetch.svg?style=flat-square
|
||||
[codecov-url]: https://codecov.io/gh/bitinn/node-fetch
|
||||
[ERROR-HANDLING.md]: https://github.com/bitinn/node-fetch/blob/master/ERROR-HANDLING.md
|
||||
[whatwg-fetch]: https://fetch.spec.whatwg.org/
|
||||
[response-init]: https://fetch.spec.whatwg.org/#responseinit
|
||||
[node-readable]: https://nodejs.org/api/stream.html#stream_readable_streams
|
||||
[mdn-headers]: https://developer.mozilla.org/en-US/docs/Web/API/Headers
|
||||
|
|
Loading…
Reference in New Issue