UNPKG

1.68 kBMarkdownView Raw
1# r2
2
3<p>
4 <a href="https://www.patreon.com/bePatron?u=880479">
5 <img src="https://c5.patreon.com/external/logo/become_a_patron_button.png" height="40px" />
6 </a>
7</p>
8
9[![Build Status](https://travis-ci.org/mikeal/r2.svg?branch=master)](https://travis-ci.org/mikeal/r2) [![Coverage Status](https://coveralls.io/repos/github/mikeal/r2/badge.svg?branch=master)](https://coveralls.io/github/mikeal/r2?branch=master) [![Greenkeeper badge](https://badges.greenkeeper.io/mikeal/r2.svg)](https://greenkeeper.io/)
10
11Early in Node.js I wrote an HTTP client library called `request`. It evolved
12along with Node.js and eventually became very widely depended upon.
13
14A lot has changed since 2010 and I've decided to re-think what a simple
15HTTP client library should look like.
16
17This new library, `r2`, is a completely new approach from `request`.
18
19* Rather than being built on top of the Node.js Core HTTP library and
20 shimmed for the browser, `r2` is built on top of the browser's
21 Fetch API and shimmed for Node.js.
22* APIs are meant to be used with async/await, which means they are
23 based on promises.
24
25```javascript
26const r2 = require('r2')
27
28let html = await r2('https://www.google.com').text
29```
30
31Simple JSON support.
32
33```javascript
34let obj = {ok: true}
35
36let resp = await r2.put('http://localhost/test.json', {json: obj}).json
37```
38
39Simple headers support.
40
41```javascript
42let headers = {'x-test': 'ok'}
43
44let res = await r2('http://localhost/test', {headers}).response
45```
46
47Being written to the Fetch API is a huge benefit for browser users.
48
49When running through browserify `request` is ~2M uncompressed and ~500K compressed. `r2` is only 66K uncompressed and 16K compressed.