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
|
||||
|
||||
```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.
|
||||
// fetch.Promise = require('bluebird');
|
||||
// if you are using your own Promise library, set it through fetch.Promise. Eg.
|
||||
|
||||
// import Bluebird from 'bluebird';
|
||||
// fetch.Promise = Bluebird;
|
||||
|
||||
// plain text or html
|
||||
|
||||
fetch('https://github.com/')
|
||||
.then(function(res) {
|
||||
return res.text();
|
||||
}).then(function(body) {
|
||||
console.log(body);
|
||||
});
|
||||
.then(res => res.text())
|
||||
.then(body => console.log(body));
|
||||
|
||||
// json
|
||||
|
||||
fetch('https://api.github.com/users/github')
|
||||
.then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(json) {
|
||||
console.log(json);
|
||||
});
|
||||
.then(res => res.json())
|
||||
.then(json => console.log(json));
|
||||
|
||||
// catching network error
|
||||
// 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
|
||||
|
||||
fetch('http://domain.invalid/')
|
||||
.catch(function(err) {
|
||||
console.log(err);
|
||||
});
|
||||
.catch(err => console.error(err));
|
||||
|
||||
// stream
|
||||
// the node.js way is to use stream when possible
|
||||
|
||||
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
|
||||
.then(function(res) {
|
||||
var dest = fs.createWriteStream('./octocat.png');
|
||||
.then(res => {
|
||||
const dest = fs.createWriteStream('./octocat.png');
|
||||
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()
|
||||
// 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')
|
||||
.then(function(res) {
|
||||
return res.buffer();
|
||||
}).then(function(buffer) {
|
||||
fileType(buffer);
|
||||
});
|
||||
.then(res => res.buffer())
|
||||
.then(buffer => fileType(buffer))
|
||||
.then(type => { /* ... */ });
|
||||
|
||||
// meta
|
||||
|
||||
fetch('https://github.com/')
|
||||
.then(function(res) {
|
||||
.then(res => {
|
||||
console.log(res.ok);
|
||||
console.log(res.status);
|
||||
console.log(res.statusText);
|
||||
|
@ -108,22 +103,17 @@ fetch('https://github.com/')
|
|||
// post
|
||||
|
||||
fetch('http://httpbin.org/post', { method: 'POST', body: 'a=1' })
|
||||
.then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(json) {
|
||||
console.log(json);
|
||||
});
|
||||
.then(res => res.json())
|
||||
.then(json => console.log(json));
|
||||
|
||||
// post with stream from resumer
|
||||
// post with stream from file
|
||||
|
||||
var resumer = require('resumer');
|
||||
var stream = resumer().queue('a=1').end();
|
||||
import { createReadStream } from 'fs';
|
||||
|
||||
const stream = createReadStream('input.txt');
|
||||
fetch('http://httpbin.org/post', { method: 'POST', body: stream })
|
||||
.then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(json) {
|
||||
console.log(json);
|
||||
});
|
||||
.then(res => res.json())
|
||||
.then(json => console.log(json));
|
||||
|
||||
// post with JSON
|
||||
|
||||
|
@ -133,45 +123,37 @@ fetch('http://httpbin.org/post', {
|
|||
body: JSON.stringify(body),
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
.then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(json) {
|
||||
console.log(json);
|
||||
});
|
||||
.then(res => res.json())
|
||||
.then(json => console.log(json));
|
||||
|
||||
// post with form-data (detect multipart)
|
||||
|
||||
var FormData = require('form-data');
|
||||
var form = new FormData();
|
||||
import FormData from 'form-data';
|
||||
|
||||
const form = new FormData();
|
||||
form.append('a', 1);
|
||||
fetch('http://httpbin.org/post', { method: 'POST', body: form })
|
||||
.then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(json) {
|
||||
console.log(json);
|
||||
});
|
||||
.then(res => res.json())
|
||||
.then(json => console.log(json));
|
||||
|
||||
// post with form-data (custom headers)
|
||||
// note that getHeaders() is non-standard API
|
||||
|
||||
var FormData = require('form-data');
|
||||
var form = new FormData();
|
||||
import FormData from 'form-data';
|
||||
|
||||
const form = new FormData();
|
||||
form.append('a', 1);
|
||||
fetch('http://httpbin.org/post', { method: 'POST', body: form, headers: form.getHeaders() })
|
||||
.then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(json) {
|
||||
.then(res => res.json())
|
||||
.then(json => console.log(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);
|
||||
});
|
||||
|
||||
// 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.
|
||||
|
|
Loading…
Reference in New Issue