From 9351084a98f15f1f412c2a84d193bf27a2a35244 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Mon, 12 Dec 2016 12:54:53 -0800 Subject: [PATCH] Update README for ES2015 --- README.md | 110 +++++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index ecb5b81..02cf186 100644 --- a/README.md +++ b/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) { - console.log(json); - }); + .then(res => res.json()) + .then(json => console.log(json)); -// node 0.12+, yield with co +// node 7+ with async function -var co = require('co'); -co(function *() { - var res = yield fetch('https://api.github.com/users/github'); - var json = yield res.json(); - console.log(res); -}); +(async function () { + const res = await fetch('https://api.github.com/users/github'); + const json = await res.json(); + console.log(json); +})(); ``` See [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples.