Skip to content

Commit 9fe2c27

Browse files
authored
Response fix (#119)
* v0.1.87 * v0.1.88 * v0.1.89 * v0.1.90 * v0.1.91 * fixed the issue where `response === {}` after making a request inside a `handleClick` function. aka the `response` object was stale * added todo to add tests for this * array destructuring + documentation fixes * fix
1 parent 3f711cd commit 9fe2c27

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

‎README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,15 @@ const App = () => (
193193
<details open><summary><b>Destructured <code>useFetch</code></b></summary>
194194
195195
```js
196+
// the `response` is everything you would expect to be in a normal response from an http request with the `data` field added.
197+
// ⚠️ The `response` object cannot be destructured! (at least not currently) ️️⚠️
196198
var [request, response, loading, error] = useFetch('https://example.com')
197199

198200
// want to use object destructuring? You can do that too
199201
var {
200202
request,
203+
// the `response` is everything you would expect to be in a normal response from an http request with the `data` field added.
204+
// ⚠️ The `response` object cannot be destructured! (at least not currently) ️️⚠️
201205
response,
202206
loading,
203207
error,
@@ -227,14 +231,8 @@ var {
227231
query, // GraphQL
228232
abort
229233
} = request
230-
231-
var {
232-
data,
233-
ok,
234-
headers,
235-
...restOfHttpResponse // everything you would get in a response from an http request
236-
} = response
237234
```
235+
238236
</details>
239237
240238
@@ -579,6 +577,7 @@ Todos
579577
- [ ] tests for SSR
580578
- [ ] tests for FormData (can also do it for react-native at same time. [see here](https://stackoverflow.com/questions/45842088/react-native-mocking-formdata-in-unit-tests))
581579
- [ ] tests for GraphQL hooks `useMutation` + `useQuery`
580+
- [ ] tests for stale `response` see this [PR](https://github.com/alex-cory/use-http/pull/119/files)
582581
- [ ] make this a github package
583582
- [ ] Make work with React Suspense [current example WIP](https://codesandbox.io/s/7ww5950no0)
584583
- [ ] get it all working on a SSR codesandbox, this way we can have api to call locally

‎docs/README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,15 @@ const App = () => (
176176
Destructured
177177
-------------
178178
```js
179+
// the `response` is everything you would expect to be in a normal response from an http request with the `data` field added.
180+
// ⚠️ The `response` object cannot be destructured! (at least not currently) ️️⚠️
179181
var [request, response, loading, error] = useFetch('https://example.com')
180182

181183
// want to use object destructuring? You can do that too
182184
var {
183185
request,
186+
// the `response` is everything you would expect to be in a normal response from an http request with the `data` field added.
187+
// ⚠️ The `response` object cannot be destructured! (at least not currently) ️️⚠️
184188
response,
185189
loading,
186190
error,
@@ -210,13 +214,6 @@ var {
210214
query, // GraphQL
211215
abort
212216
} = request
213-
214-
var {
215-
data,
216-
ok,
217-
headers,
218-
...restOfHttpResponse // everything you would get in a response from an http request
219-
} = response
220217
```
221218
222219
Relative routes

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-http",
3-
"version": "0.1.90",
3+
"version": "0.1.91",
44
"homepage": "http://use-http.com",
55
"main": "dist/index.js",
66
"license": "MIT",

‎src/useFetch.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import useSSR from 'use-ssr'
1515
import makeRouteAndOptions from './makeRouteAndOptions'
1616
import { isEmpty, invariant } from './utils'
1717

18+
const makeResponseProxy = (res = {}) => new Proxy(res, {
19+
get: (httpResponse: any, key) => (httpResponse.current || {})[key]
20+
})
1821

1922
function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
2023
const { customOptions, requestInit, defaults } = useFetchArgs(...args)
@@ -155,8 +158,8 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
155158
}, [onMount, executeRequest])
156159

157160
return Object.assign<UseFetchArrayReturn<TData>, UseFetchObjectReturn<TData>>(
158-
[request, res.current, loading as boolean, error],
159-
{ request, response: res.current, ...request },
161+
[request, makeResponseProxy(res), loading as boolean, error],
162+
{ request, response: makeResponseProxy(res), ...request },
160163
)
161164
}
162165

‎src/useFetchArgs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ export default function useFetchArgs(
116116
const requestInit = pullOutRequestInit(requestInitOptions)
117117

118118
return {
119+
...useFetchArgsDefaults.requestInit,
119120
...contextRequestInit,
120121
...requestInit,
121122
headers: {
122-
...useFetchArgsDefaults.requestInit.headers,
123+
...defaults.headers,
123124
...contextRequestInit.headers,
124125
...requestInit.headers,
125126
},

0 commit comments

Comments
 (0)