Update README for ES2015
This commit is contained in:
parent
030bf27943
commit
9351084a98
112
README.md
112
README.md
|
@ -41,44 +41,40 @@ See Matt Andrews' [isomorphic-fetch](https://github.com/matthew-andrews/isomorph
|
||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var fetch = require('node-fetch');
|
import fetch from 'node-fetch';
|
||||||
|
// or
|
||||||
|
// const fetch = require('node-fetch');
|
||||||
|
|
||||||
// if you are on node v0.10, set a Promise library first, eg.
|
// if you are using your own Promise library, set it through fetch.Promise. Eg.
|
||||||
// fetch.Promise = require('bluebird');
|
|
||||||
|
// import Bluebird from 'bluebird';
|
||||||
|
// fetch.Promise = Bluebird;
|
||||||
|
|
||||||
// plain text or html
|
// plain text or html
|
||||||
|
|
||||||
fetch('https://github.com/')
|
fetch('https://github.com/')
|
||||||
.then(function(res) {
|
.then(res => res.text())
|
||||||
return res.text();
|
.then(body => console.log(body));
|
||||||
}).then(function(body) {
|
|
||||||
console.log(body);
|
|
||||||
});
|
|
||||||
|
|
||||||
// json
|
// json
|
||||||
|
|
||||||
fetch('https://api.github.com/users/github')
|
fetch('https://api.github.com/users/github')
|
||||||
.then(function(res) {
|
.then(res => res.json())
|
||||||
return res.json();
|
.then(json => console.log(json));
|
||||||
}).then(function(json) {
|
|
||||||
console.log(json);
|
|
||||||
});
|
|
||||||
|
|
||||||
// catching network error
|
// catching network error
|
||||||
// 3xx-5xx responses are NOT network errors, and should be handled in then()
|
// 3xx-5xx responses are NOT network errors, and should be handled in then()
|
||||||
// you only need one catch() at the end of your promise chain
|
// you only need one catch() at the end of your promise chain
|
||||||
|
|
||||||
fetch('http://domain.invalid/')
|
fetch('http://domain.invalid/')
|
||||||
.catch(function(err) {
|
.catch(err => console.error(err));
|
||||||
console.log(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
// stream
|
// stream
|
||||||
// the node.js way is to use stream when possible
|
// the node.js way is to use stream when possible
|
||||||
|
|
||||||
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
|
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
|
||||||
.then(function(res) {
|
.then(res => {
|
||||||
var dest = fs.createWriteStream('./octocat.png');
|
const dest = fs.createWriteStream('./octocat.png');
|
||||||
res.body.pipe(dest);
|
res.body.pipe(dest);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -86,18 +82,17 @@ fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
|
||||||
// if you prefer to cache binary data in full, use buffer()
|
// if you prefer to cache binary data in full, use buffer()
|
||||||
// note that buffer() is a node-fetch only API
|
// note that buffer() is a node-fetch only API
|
||||||
|
|
||||||
var fileType = require('file-type');
|
import fileType from 'file-type';
|
||||||
|
|
||||||
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
|
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
|
||||||
.then(function(res) {
|
.then(res => res.buffer())
|
||||||
return res.buffer();
|
.then(buffer => fileType(buffer))
|
||||||
}).then(function(buffer) {
|
.then(type => { /* ... */ });
|
||||||
fileType(buffer);
|
|
||||||
});
|
|
||||||
|
|
||||||
// meta
|
// meta
|
||||||
|
|
||||||
fetch('https://github.com/')
|
fetch('https://github.com/')
|
||||||
.then(function(res) {
|
.then(res => {
|
||||||
console.log(res.ok);
|
console.log(res.ok);
|
||||||
console.log(res.status);
|
console.log(res.status);
|
||||||
console.log(res.statusText);
|
console.log(res.statusText);
|
||||||
|
@ -108,22 +103,17 @@ fetch('https://github.com/')
|
||||||
// post
|
// post
|
||||||
|
|
||||||
fetch('http://httpbin.org/post', { method: 'POST', body: 'a=1' })
|
fetch('http://httpbin.org/post', { method: 'POST', body: 'a=1' })
|
||||||
.then(function(res) {
|
.then(res => res.json())
|
||||||
return res.json();
|
.then(json => console.log(json));
|
||||||
}).then(function(json) {
|
|
||||||
console.log(json);
|
|
||||||
});
|
|
||||||
|
|
||||||
// post with stream from resumer
|
// post with stream from file
|
||||||
|
|
||||||
var resumer = require('resumer');
|
import { createReadStream } from 'fs';
|
||||||
var stream = resumer().queue('a=1').end();
|
|
||||||
|
const stream = createReadStream('input.txt');
|
||||||
fetch('http://httpbin.org/post', { method: 'POST', body: stream })
|
fetch('http://httpbin.org/post', { method: 'POST', body: stream })
|
||||||
.then(function(res) {
|
.then(res => res.json())
|
||||||
return res.json();
|
.then(json => console.log(json));
|
||||||
}).then(function(json) {
|
|
||||||
console.log(json);
|
|
||||||
});
|
|
||||||
|
|
||||||
// post with JSON
|
// post with JSON
|
||||||
|
|
||||||
|
@ -133,45 +123,37 @@ fetch('http://httpbin.org/post', {
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
})
|
})
|
||||||
.then(function(res) {
|
.then(res => res.json())
|
||||||
return res.json();
|
.then(json => console.log(json));
|
||||||
}).then(function(json) {
|
|
||||||
console.log(json);
|
|
||||||
});
|
|
||||||
|
|
||||||
// post with form-data (detect multipart)
|
// post with form-data (detect multipart)
|
||||||
|
|
||||||
var FormData = require('form-data');
|
import FormData from 'form-data';
|
||||||
var form = new FormData();
|
|
||||||
|
const form = new FormData();
|
||||||
form.append('a', 1);
|
form.append('a', 1);
|
||||||
fetch('http://httpbin.org/post', { method: 'POST', body: form })
|
fetch('http://httpbin.org/post', { method: 'POST', body: form })
|
||||||
.then(function(res) {
|
.then(res => res.json())
|
||||||
return res.json();
|
.then(json => console.log(json));
|
||||||
}).then(function(json) {
|
|
||||||
console.log(json);
|
|
||||||
});
|
|
||||||
|
|
||||||
// post with form-data (custom headers)
|
// post with form-data (custom headers)
|
||||||
// note that getHeaders() is non-standard API
|
// note that getHeaders() is non-standard API
|
||||||
|
|
||||||
var FormData = require('form-data');
|
import FormData from 'form-data';
|
||||||
var form = new FormData();
|
|
||||||
|
const form = new FormData();
|
||||||
form.append('a', 1);
|
form.append('a', 1);
|
||||||
fetch('http://httpbin.org/post', { method: 'POST', body: form, headers: form.getHeaders() })
|
fetch('http://httpbin.org/post', { method: 'POST', body: form, headers: form.getHeaders() })
|
||||||
.then(function(res) {
|
.then(res => res.json())
|
||||||
return res.json();
|
.then(json => console.log(json));
|
||||||
}).then(function(json) {
|
|
||||||
|
// node 7+ with async function
|
||||||
|
|
||||||
|
(async function () {
|
||||||
|
const res = await fetch('https://api.github.com/users/github');
|
||||||
|
const json = await res.json();
|
||||||
console.log(json);
|
console.log(json);
|
||||||
});
|
})();
|
||||||
|
|
||||||
// node 0.12+, yield with co
|
|
||||||
|
|
||||||
var co = require('co');
|
|
||||||
co(function *() {
|
|
||||||
var res = yield fetch('https://api.github.com/users/github');
|
|
||||||
var json = yield res.json();
|
|
||||||
console.log(res);
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
See [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples.
|
See [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples.
|
||||||
|
|
Loading…
Reference in New Issue