1 | <p align="center">
|
2 | <img src="https://i.imgur.com/JaXEFNp.png" width="300" height="300" alt="unfetch">
|
3 | <br>
|
4 | <a href="https://www.npmjs.org/package/unfetch"><img src="https://img.shields.io/npm/v/unfetch.svg?style=flat" alt="npm"></a> <a href="https://travis-ci.org/developit/unfetch"><img src="https://travis-ci.org/developit/unfetch.svg?branch=master" alt="travis"></a>
|
5 | </p>
|
6 |
|
7 | # unfetch
|
8 |
|
9 | > Tiny 500b fetch "barely-polyfill"
|
10 |
|
11 | - **Tiny:** weighs about **500 bytes** gzipped
|
12 | - **Minimal:** just `fetch()` with headers and text/json/xml responses
|
13 | - **Familiar:** a subset of the full API
|
14 | - **Supported:** supports IE8+ (<abbr title="Bring Your Own Promises">BYOP</abbr>)
|
15 | - **Standalone:** one function, no dependencies
|
16 |
|
17 | > 🤔 **What's Missing?**
|
18 | >
|
19 | > - Uses simple Arrays instead of Iterables, since Arrays _are_ iterables
|
20 | > - No streaming, just Promisifies existing XMLHttpRequest response bodies
|
21 | > - Bare-bones `.blob()` implementation - just proxies `xhr.response`
|
22 |
|
23 | * * *
|
24 |
|
25 | ## Table of Contents
|
26 |
|
27 | - [Install](#install)
|
28 | - [Usage](#usage)
|
29 | - [Examples & Demos](#examples--demos)
|
30 | - [API](#api)
|
31 | - [Contribute](#contribute)
|
32 | - [License](#license)
|
33 |
|
34 | * * *
|
35 |
|
36 | ## Install
|
37 |
|
38 | This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.
|
39 |
|
40 | ```sh
|
41 | $ npm install --save unfetch
|
42 | ```
|
43 |
|
44 | Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else:
|
45 |
|
46 | ```javascript
|
47 | // using ES6 modules
|
48 | import fetch from 'unfetch'
|
49 |
|
50 | // using CommonJS modules
|
51 | var fetch = require('unfetch')
|
52 | ```
|
53 |
|
54 | The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):
|
55 |
|
56 | ```html
|
57 | <script src="https://unpkg.com/unfetch/dist/unfetch.umd.js"></script>
|
58 | ```
|
59 |
|
60 | This exposes a `fetch` global if not already present.
|
61 |
|
62 | * * *
|
63 |
|
64 | ## Usage
|
65 |
|
66 | ```js
|
67 | import fetch from 'unfetch'
|
68 |
|
69 | fetch('/foo.json')
|
70 | .then( r => r.json() )
|
71 | .then( data => {
|
72 | console.log(data)
|
73 | })
|
74 | ```
|
75 |
|
76 | ## Examples & Demos
|
77 |
|
78 | ```js
|
79 | // simple GET request:
|
80 | fetch('/foo')
|
81 | .then( r => r.text() )
|
82 | .then( txt => console.log(txt) )
|
83 |
|
84 |
|
85 | // complex POST request with JSON, headers:
|
86 | fetch('/bear', {
|
87 | method: 'POST',
|
88 | headers: {
|
89 | 'Content-Type': 'application/json'
|
90 | },
|
91 | body: JSON.stringify({ hungry: true })
|
92 | }).then( r => {
|
93 | open(r.headers.get('location'));
|
94 | return r.json();
|
95 | })
|
96 | ```
|
97 |
|
98 | * * *
|
99 |
|
100 | ## Contribute
|
101 |
|
102 | First off, thanks for taking the time to contribute!
|
103 | Now, take a moment to be sure your contributions make sense to everyone else.
|
104 |
|
105 | ### Reporting Issues
|
106 |
|
107 | Found a problem? Want a new feature? First of all see if your issue or idea has [already been reported](../../issues).
|
108 | If it hasn't, just open a [new clear and descriptive issue](../../issues/new).
|
109 |
|
110 | ### Submitting pull requests
|
111 |
|
112 | Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.
|
113 |
|
114 | - Fork it!
|
115 | - Clone your fork: `git clone https://github.com/<your-username>/unfetch`
|
116 | - Navigate to the newly cloned directory: `cd unfetch`
|
117 | - Create a new branch for the new feature: `git checkout -b my-new-feature`
|
118 | - Install the tools necessary for development: `npm install`
|
119 | - Make your changes.
|
120 | - Commit your changes: `git commit -am 'Add some feature'`
|
121 | - Push to the branch: `git push origin my-new-feature`
|
122 | - Submit a pull request with full remarks documenting your changes.
|
123 |
|
124 | ## License
|
125 |
|
126 | [MIT License](LICENSE.md) © [Jason Miller](https://jasonformat.com/)
|