1 | # JSON Fetch
|
2 |
|
3 | [![codecov badge](https://codecov.io/gh/goodeggs/json-fetch/branch/master/graph/badge.svg)](https://codecov.io/gh/goodeggs/json-fetch)
|
4 |
|
5 | A wrapper around ES6 fetch to simplify interacting with JSON APIs.
|
6 |
|
7 | - automatically JSON stringify request body
|
8 | - set JSON request headers
|
9 | - resolve with json for any response with the `Content-Type`: `application/json` header
|
10 | - include request credentials by default
|
11 | - configurable retry option for requests
|
12 |
|
13 | [![build status][travis-badge]][travis-link]
|
14 | [![MIT license][license-badge]][license-link]
|
15 |
|
16 | ## Usage
|
17 |
|
18 | ```
|
19 | yarn add json-fetch
|
20 | # or...
|
21 | npm install json-fetch
|
22 | ```
|
23 |
|
24 | ```js
|
25 | import jsonFetch from 'json-fetch';
|
26 |
|
27 | jsonFetch('http://www.test.com/products/1234', {
|
28 | body: {name: 'apple'}, // content to be JSON stringified
|
29 | credentials: 'omit', // "include" by default
|
30 | expectedStatuses: [201], // rejects "FetchUnexpectedStatusError" on unexpected status (optional)
|
31 | // supports all normal json-fetch options:
|
32 | method: 'POST',
|
33 | })
|
34 | .then((response) => {
|
35 | // handle response with expected status:
|
36 | console.log(response.body); // json response here
|
37 | console.log(response.status);
|
38 | console.log(response.statusText);
|
39 | console.log(response.headers);
|
40 | })
|
41 | .catch((err) => {
|
42 | // handle response with unexpected status:
|
43 | console.log(err.name);
|
44 | console.log(err.message);
|
45 | console.log(err.response.status);
|
46 | console.log(err.response.statusText);
|
47 | console.log(err.response.body);
|
48 | console.log(err.response.text);
|
49 | console.log(err.response.headers);
|
50 | });
|
51 | ```
|
52 |
|
53 | ### Retry Behavior
|
54 |
|
55 | By default, jsonFetch doesn't retry requests. However, you may opt in to jsonFetch's very flexible retry behavior, provided by the excellent [`promise-retry`](https://github.com/IndigoUnited/node-promise-retry) library. Here's a quick example:
|
56 |
|
57 | ```js
|
58 | import jsonFetch, {retriers} from 'json-fetch'
|
59 |
|
60 | jsonFetch('http://www.test.com/products/1234', {
|
61 | method: 'POST',
|
62 | body: {name: 'apple'},
|
63 | shouldRetry: retries.isNetworkError // after every request, retry if a network error is thrown
|
64 | retry: {
|
65 | // Retry 5 times, in addition to the original request
|
66 | retries: 5,
|
67 | }
|
68 | }).then(response => {
|
69 | // handle responses
|
70 | });
|
71 | ```
|
72 |
|
73 | Any option that `promise-retry` accepts will be passed through from `options.retry`. See [the promise-retry documentation](https://github.com/IndigoUnited/node-promise-retry#promiseretryfn-options) for all options.
|
74 |
|
75 | ### Custom Retry Logic
|
76 |
|
77 | We've provided two default "retrier" functions that decide to retry 503/504 status code responses and network errors (`jsonFetch.retriers.is5xx` and `jsonFetch.retriers.isNetworkError` respectively). You can easily provide your own custom retrier function to `options.shouldRetry`.
|
78 |
|
79 | The contract for a retrier function is:
|
80 |
|
81 | ```js
|
82 | shouldRetry([Error || FetchResponse]) returns bool
|
83 | ```
|
84 |
|
85 | You can use any attribute of the [FetchResponse](https://developer.mozilla.org/en-US/docs/Web/API/Response) or Error to determine whether to retry or not. Your function _must_ handle both errors (such as network errors) and FetchResponse objects without blowing up. We recommend stateless, side-effect free functions. You do not need to worry about the maximum number of retries -- promise-retry will stop retrying after the maximum you specify. See the tests and `src/retriers.js` file for examples.
|
86 |
|
87 | ## Contributing
|
88 |
|
89 | Please follow our [Code of Conduct](CODE_OF_CONDUCT.md) when contributing to this project.
|
90 |
|
91 | ```
|
92 | yarn install
|
93 | yarn test
|
94 | ```
|
95 |
|
96 | ## Deploying a new version
|
97 |
|
98 | This module is automatically deployed when a version tag bump is detected by travis.
|
99 | Remember to update the [changelog](CHANGELOG.md)!
|
100 |
|
101 | ```
|
102 | yarn version
|
103 | ```
|
104 |
|
105 | ## License
|
106 |
|
107 | [MIT](License.md)
|
108 |
|
109 | _Module scaffold generated by [generator-goodeggs-npm](https://github.com/goodeggs/generator-goodeggs-npm)._
|
110 |
|
111 | [travis-badge]: http://img.shields.io/travis/goodeggs/json-fetch.svg?style=flat-square
|
112 | [travis-link]: https://travis-ci.org/goodeggs/json-fetch
|
113 | [npm-badge]: http://img.shields.io/npm/v/json-fetch.svg?style=flat-square
|
114 | [npm-link]: https://www.npmjs.org/package/json-fetch
|
115 | [license-badge]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square
|
116 | [license-link]: LICENSE.md
|