Skip to content
This repository was archived by the owner on Aug 3, 2023. It is now read-only.

Commit aab1dfa

Browse files
authored
Merge pull request #183 from shanecav/dofetch-promise
Update doFetch to return a promise that resolves with same value as afterFetch
2 parents f9c109c + d2658fa commit aab1dfa

File tree

7 files changed

+341
-244
lines changed

7 files changed

+341
-244
lines changed

CHANGELOG.md

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
# Changelog
22

3+
### v3.1.0 (2018/7/10)
4+
5+
**New Features**
6+
7+
- `doFetch` now returns a Promise that _always_ resolves. The value that it resolves to is
8+
the same object that is passed to `afterFetch`. Note that `afterFetch` is only called when a
9+
network request has actually been performed, whereas `doFetch` resolves even when the cache is hit.
10+
311
### v3.0.1 (2018/4/24)
412

513
**Bug Fixes**
614

7-
* Fixes a bug where `isLazy` would sometimes be computed using previous
15+
- Fixes a bug where `isLazy` would sometimes be computed using previous
816
props rather than the current props.
917

1018
### v3.0.0 (2018/4/24)
@@ -14,85 +22,85 @@
1422
1523
**Breaking Changes**
1624

17-
* When a request fails, the `data` from a request will no longer be set to `null`. This
25+
- When a request fails, the `data` from a request will no longer be set to `null`. This
1826
allows you to control whether or not your UI continues to display the existing data.
1927

20-
* The `responseType` prop is now more forgiving. If the body cannot be parsed with
28+
- The `responseType` prop is now more forgiving. If the body cannot be parsed with
2129
the `responseType` that you set, then `data` will be set to `null`.
2230

2331
### v2.0.4 (2018/4/20)
2432

2533
**Bug Fixes**
2634

27-
* Fixes a bug where there could be a cache mismatch when re-rendering the same component
35+
- Fixes a bug where there could be a cache mismatch when re-rendering the same component
2836
that has a fetch policy configured.
2937

3038
### v2.0.3 (2018/3/2)
3139

3240
**Bug Fixes**
3341

34-
* Fixes a bug where the `lazy` prop was not always respected. Anytime that a new request key was generated,
42+
- Fixes a bug where the `lazy` prop was not always respected. Anytime that a new request key was generated,
3543
a request would be made.
3644

3745
### v2.0.2 (2018/2/21)
3846

3947
**Bug Fixes**
4048

41-
* Fixes a bug where an Uncaught ReferenceError could be thrown
49+
- Fixes a bug where an Uncaught ReferenceError could be thrown
4250

4351
### v2.0.1 (2018/2/17)
4452

4553
**Bug Fixes**
4654

47-
* This fixes a problem where the default `fetchPolicy` would be `"cache-first"` for "write" requests.
55+
- This fixes a problem where the default `fetchPolicy` would be `"cache-first"` for "write" requests.
4856

4957
### v2.0.0 (2018/2/17)
5058

5159
**Breaking**
5260

53-
* `transformResponse` has been renamed to be `transformData`
54-
* `fetchPolicy` is now determined by the method that you pass in. This change was made to support using
61+
- `transformResponse` has been renamed to be `transformData`
62+
- `fetchPolicy` is now determined by the method that you pass in. This change was made to support using
5563
POST methods for read requests, and is unlikely to break your code.
56-
* A new prop, `cacheResponse`, is used to determine if a response is added to the cache or
64+
- A new prop, `cacheResponse`, is used to determine if a response is added to the cache or
5765
not. This is to support using POST methods for read requests, and is unlikely to break your code.
5866

5967
**New Features**
6068

61-
* A new `failed` property is passed to you in the render prop callback. This allows you to
69+
- A new `failed` property is passed to you in the render prop callback. This allows you to
6270
quickly determine if a request failed for any reason (be it network errors or "error" status
6371
codes).
6472

6573
### v1.1.0 (2018/2/7)
6674

6775
**New Features**
6876

69-
* `responseType` can now be specified as a function. It receives the `response`
77+
- `responseType` can now be specified as a function. It receives the `response`
7078
as the first argument.
71-
* Adds a `requestKey` prop.
72-
* When the request is "faux-aborted," the error will have a `name` equal to `AbortError`.
79+
- Adds a `requestKey` prop.
80+
- When the request is "faux-aborted," the error will have a `name` equal to `AbortError`.
7381
This matches the name of the native error, allowing you to write future-proof code that
7482
handles aborted requests.
7583

7684
### v1.0.0 (2018/2/4)
7785

7886
**Breaking**
7987

80-
* The `responseType` will now be set to `"text"` anytime a response returns
88+
- The `responseType` will now be set to `"text"` anytime a response returns
8189
with a 204 status code.
82-
* The `responseType` is no longer used when creating the request key.
90+
- The `responseType` is no longer used when creating the request key.
8391

8492
### v0.3.0 (2018/2/4)
8593

8694
**Changes**
8795

88-
* `fetch-dedupe` has been abstracted into a separate library. This
96+
- `fetch-dedupe` has been abstracted into a separate library. This
8997
does not change the public API of this library.
9098

9199
### v0.2.0 (2018/2/1)
92100

93101
**New Features**
94102

95-
* The render prop will now be passed the `requestKey`.
103+
- The render prop will now be passed the `requestKey`.
96104

97105
### v0.1.0 (2018/2/1)
98106

@@ -101,4 +109,4 @@ named `render`. Accordingly, this library has been updated to use `children` as
101109

102110
**Breaking**
103111

104-
* `<Fetch/>` now uses `children` as the render prop, rather than `render`.
112+
- `<Fetch/>` now uses `children` as the render prop, rather than `render`.

README.md

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,28 @@ yarn add react-request
4646

4747
### Documentation
4848

49-
* [Getting Started](#getting-started)
50-
* [API](#api)
51-
* [\<Fetch/\>](#fetch-)
52-
* [fetchDedupe()](#fetchdedupe-input--init--dedupeoptions-)
53-
* [getRequestKey()](#getrequestkey-url-method-body-responsetype-)
54-
* [isRequestInFlight()](#isrequestinflight-requestkey-)
55-
* [clearRequestCache()](#clearrequestcache)
56-
* [clearResponseCache()](#clearresponsecache)
57-
* [Guides ⇗](./docs/guides/INDEX.md)
58-
* [Response Caching ⇗](./docs/guides/response-caching.md)
59-
* [Request Deduplication ⇗](./docs/guides/request-deduplication.md)
60-
* [Request Keys ⇗](./docs/guides/request-keys.md)
61-
* [Best Practices ⇗](./docs/guides/best-practices.md)
62-
* [Using the `lazy` Prop ⇗](./docs/guides/using-the-lazy-prop.md)
63-
* [Aborting ⇗](./docs/guides/aborting.md)
64-
* [Differences with `fetch()`](./docs/guides/differences-with-fetch.md)
65-
* [Differences with React Apollo ⇗](./docs/guides/differences-with-apollo.md)
66-
* [Integration with Other Technologies ⇗](./docs/guides/integration-with-other-technologies.md)
67-
* [Examples ⇗](./docs/examples.md)
68-
* [FAQ ⇗](./docs/FAQ.md)
69-
* [Roadmap ⇗](./ROADMAP.md)
70-
* [Acknowledgements](#acknowledgements)
49+
- [Getting Started](#getting-started)
50+
- [API](#api)
51+
- [\<Fetch/\>](#fetch-)
52+
- [fetchDedupe()](#fetchdedupe-input--init--dedupeoptions-)
53+
- [getRequestKey()](#getrequestkey-url-method-body-responsetype-)
54+
- [isRequestInFlight()](#isrequestinflight-requestkey-)
55+
- [clearRequestCache()](#clearrequestcache)
56+
- [clearResponseCache()](#clearresponsecache)
57+
- [Guides ⇗](./docs/guides/INDEX.md)
58+
- [Response Caching ⇗](./docs/guides/response-caching.md)
59+
- [Request Deduplication ⇗](./docs/guides/request-deduplication.md)
60+
- [Request Keys ⇗](./docs/guides/request-keys.md)
61+
- [Best Practices ⇗](./docs/guides/best-practices.md)
62+
- [Using the `lazy` Prop ⇗](./docs/guides/using-the-lazy-prop.md)
63+
- [Aborting ⇗](./docs/guides/aborting.md)
64+
- [Differences with `fetch()`](./docs/guides/differences-with-fetch.md)
65+
- [Differences with React Apollo ⇗](./docs/guides/differences-with-apollo.md)
66+
- [Integration with Other Technologies ⇗](./docs/guides/integration-with-other-technologies.md)
67+
- [Examples ⇗](./docs/examples.md)
68+
- [FAQ ⇗](./docs/FAQ.md)
69+
- [Roadmap ⇗](./ROADMAP.md)
70+
- [Acknowledgements](#acknowledgements)
7171

7272
### Getting Started
7373

@@ -124,7 +124,7 @@ class App extends Component {
124124
<Fetch
125125
url="https://jsonplaceholder.typicode.com/posts/1"
126126
method="DELETE"
127-
/>
127+
/>,
128128
]}>
129129
{([readPost, deletePost]) => {
130130
return (
@@ -159,19 +159,19 @@ API as a prop, in addition to a few other things.
159159

160160
The props that come from the `fetch()` method are:
161161

162-
* `url`
163-
* `method`: defaults to `"GET"`
164-
* `body`
165-
* `credentials`
166-
* `headers`
167-
* `mode`
168-
* `cache`
169-
* `redirect`
170-
* `referrer`: defaults to `"about:client"`
171-
* `referrerPolicy`: defaults to `""`
172-
* `integrity`: defaults to `""`
173-
* `keepalive`
174-
* `signal`
162+
- `url`
163+
- `method`: defaults to `"GET"`
164+
- `body`
165+
- `credentials`
166+
- `headers`
167+
- `mode`
168+
- `cache`
169+
- `redirect`
170+
- `referrer`: defaults to `"about:client"`
171+
- `referrerPolicy`: defaults to `""`
172+
- `integrity`: defaults to `""`
173+
- `keepalive`
174+
- `signal`
175175

176176
To learn more about the valid options for these props, refer to the
177177
[`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch)
@@ -185,7 +185,7 @@ The following example demonstrates some of the most commonly-used props that com
185185
method="patch"
186186
credentials="same-origin"
187187
headers={{
188-
'csrf-token': myCsrfToken
188+
'csrf-token': myCsrfToken,
189189
}}
190190
body={JSON.stringify({ title: 'New post' })}>
191191
{({ doFetch }) => {
@@ -201,34 +201,37 @@ In addition to the `fetch()` props, there are a number of other useful props.
201201
The [render prop](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce) of this component.
202202
It is called with one argument, `result`, an object with the following keys:
203203

204-
* `fetching`: A Boolean representing whether or not a request is currently in flight for this component
205-
* `failed`: A Boolean representing whether or not the request failed for any reason. This includes network
204+
- `fetching`: A Boolean representing whether or not a request is currently in flight for this component
205+
- `failed`: A Boolean representing whether or not the request failed for any reason. This includes network
206206
errors and status codes that are greater than or equal to`400`.
207-
* `error`: An error object representing a network error occurred. Note that HTTP "error" status codes do not
207+
- `error`: An error object representing a network error occurred. Note that HTTP "error" status codes do not
208208
cause errors; only failed or aborted network requests do. For more, see the
209209
["Using Fetch" MDN guide](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful).
210-
* `response`: An instance of [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response). The
210+
- `response`: An instance of [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response). The
211211
[`body`](https://developer.mozilla.org/en-US/docs/Web/API/Body) will already be read, and made
212212
available to you as `response.data`.
213-
* `data`: The data returned in `response`. This will be different from `response.data` if a
213+
- `data`: The data returned in `response`. This will be different from `response.data` if a
214214
`transformData` prop was passed to `<Fetch/>`.
215-
* `doFetch`: A function that makes the HTTP request. See notes below.
216-
* `url`: The URL that was passed into `<Fetch />`.
217-
* `requestName`: The name of the request (see `requestName` below)
218-
* `requestKey`: The computed [request key](./docs/guides/request-keys.md)
215+
- `doFetch`: A function that makes the HTTP request. See notes below.
216+
- `url`: The URL that was passed into `<Fetch />`.
217+
- `requestName`: The name of the request (see `requestName` below)
218+
- `requestKey`: The computed [request key](./docs/guides/request-keys.md)
219219

220220
There are three common use cases for the `doFetch` prop:
221221

222-
* You can use it to "refresh" the data by making a follow-up request for read requests
223-
* You can use it to retry the request if there is any sort of error
224-
* You must manually call this method to actually make the request anytime that the `lazy` prop
222+
- You can use it to "refresh" the data by making a follow-up request for read requests
223+
- You can use it to retry the request if there is any sort of error
224+
- You must manually call this method to actually make the request anytime that the `lazy` prop
225225
is passed as `true`.
226226

227227
`doFetch` accepts one argument: `options`. Any of the `fetch()` options, such as `url`, `method`, and
228228
`body` are valid `options`. You may also specify a new `requestKey` if you are manually generating your
229229
own keys. This method allows you to customize the request from within the component based on the
230230
component's state.
231231

232+
`doFetch` returns a Promise that **always** resolves. It resolves to the same argument that the
233+
[`afterFetch`](#afterFetch) prop receives.
234+
232235
##### `lazy`
233236

234237
Whether or not the request will be called when the component mounts. The default value
@@ -252,10 +255,10 @@ is based on the request method that you use.
252255
A function that is called just before a network request is initiated. It is called
253256
with one argument, an object with the following keys:
254257

255-
* `url`: The URL of the request
256-
* `init`: The second argument passed to `global.fetch()`, which specifies things
258+
- `url`: The URL of the request
259+
- `init`: The second argument passed to `global.fetch()`, which specifies things
257260
such as the body, method, and so on
258-
* `requestKey`: Either the computed request key, or the value of the
261+
- `requestKey`: Either the computed request key, or the value of the
259262
`requestKey` prop
260263

261264
This feature is useful for analytics, or syncing response data with a data store such
@@ -268,16 +271,16 @@ as [Redux](https://github.com/reactjs/redux/).
268271
A function that is called anytime that a network response is received. It is called
269272
with one argument, an object with the following keys:
270273

271-
* `url`: The URL of the request
272-
* `init`: The second argument passed to `global.fetch()`, which specifies things
274+
- `url`: The URL of the request
275+
- `init`: The second argument passed to `global.fetch()`, which specifies things
273276
such as the body, method, and so on
274-
* `requestKey`: Either the computed request key, or the value of the
277+
- `requestKey`: Either the computed request key, or the value of the
275278
`requestKey` prop
276-
* `response`: The response that was received from the HTTP request
277-
* `data`: The transformed data from the response. This will be different from
279+
- `response`: The response that was received from the HTTP request
280+
- `data`: The transformed data from the response. This will be different from
278281
`response.data` if a `transformData` function was passed as a prop to `<Fetch/>`.
279-
* `error`: An error returned from the HTTP request
280-
* `didUnmount`: A Boolean representing whether or not the component has unmounted
282+
- `error`: An error returned from the HTTP request
283+
- `didUnmount`: A Boolean representing whether or not the component has unmounted
281284

282285
This can be used for analytics or syncing response data with a data store such
283286
as [Redux](https://github.com/reactjs/redux/).
@@ -351,7 +354,7 @@ about the response, such as its status code.
351354
responseType="text"
352355
transformData={countryName => {
353356
return {
354-
countryName
357+
countryName,
355358
};
356359
}}>
357360
{({ data }) => {
@@ -383,10 +386,10 @@ everywhere, we tend to give them names to help humans read and debug the code.
383386
384387
This determines how the request interacts with the cache. Valid options are:
385388
386-
* `"cache-first"`
387-
* `"cache-and-network"`
388-
* `"network-only"`
389-
* `"cache-only"`
389+
- `"cache-first"`
390+
- `"cache-and-network"`
391+
- `"network-only"`
392+
- `"cache-only"`
390393
391394
For documentation on what each of these values do, refer to the [response caching guide](./docs/guides/response-caching.md).
392395

examples/updating-a-resource/src/App.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ class App extends Component {
1010
<button
1111
onClick={() =>
1212
doFetch({
13-
body: this.getUpdatedPost()
13+
body: this.getUpdatedPost(),
14+
}).then(afterFetchInfo => {
15+
// eslint-disable-next-line no-console
16+
console.log(
17+
'The call to doFetch resolved. Received result:',
18+
afterFetchInfo
19+
);
1420
})
1521
}
1622
disabled={fetching}>
@@ -26,7 +32,7 @@ class App extends Component {
2632

2733
getUpdatedPost() {
2834
return JSON.stringify({
29-
title: 'hello'
35+
title: 'hello',
3036
});
3137
}
3238
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-request",
3-
"version": "3.0.1",
3+
"version": "3.1.0",
44
"description": "Declarative HTTP requests with React.",
55
"main": "lib/index.js",
66
"module": "es/index.js",

0 commit comments

Comments
 (0)