diff --git a/README.md b/README.md index eea8d808..13f95111 100644 --- a/README.md +++ b/README.md @@ -192,11 +192,15 @@ const App = () => (
Destructured useFetch ```js +// the `response` is everything you would expect to be in a normal response from an http request with the `data` field added. +// ⚠️ The `response` object cannot be destructured! (at least not currently) ️️⚠️ var [request, response, loading, error] = useFetch('https://example.com') // want to use object destructuring? You can do that too var { request, + // the `response` is everything you would expect to be in a normal response from an http request with the `data` field added. + // ⚠️ The `response` object cannot be destructured! (at least not currently) ️️⚠️ response, loading, error, @@ -226,14 +230,8 @@ var { query, // GraphQL abort } = request - -var { - data, - ok, - headers, - ...restOfHttpResponse // everything you would get in a response from an http request -} = response ``` +
@@ -576,6 +574,7 @@ Todos - [ ] tests for SSR - [ ] 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)) - [ ] tests for GraphQL hooks `useMutation` + `useQuery` + - [ ] tests for stale `response` see this [PR](https://github.com/alex-cory/use-http/pull/119/files) - [ ] make this a github package - [ ] Make work with React Suspense [current example WIP](https://codesandbox.io/s/7ww5950no0) - [ ] get it all working on a SSR codesandbox, this way we can have api to call locally diff --git a/docs/README.md b/docs/README.md index 8c97c5bc..7f5b58fe 100644 --- a/docs/README.md +++ b/docs/README.md @@ -175,11 +175,15 @@ const App = () => ( Destructured ------------- ```js +// the `response` is everything you would expect to be in a normal response from an http request with the `data` field added. +// ⚠️ The `response` object cannot be destructured! (at least not currently) ️️⚠️ var [request, response, loading, error] = useFetch('https://example.com') // want to use object destructuring? You can do that too var { request, + // the `response` is everything you would expect to be in a normal response from an http request with the `data` field added. + // ⚠️ The `response` object cannot be destructured! (at least not currently) ️️⚠️ response, loading, error, @@ -209,13 +213,6 @@ var { query, // GraphQL abort } = request - -var { - data, - ok, - headers, - ...restOfHttpResponse // everything you would get in a response from an http request -} = response ``` Relative routes diff --git a/package.json b/package.json index 4f8c32ca..322194e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "use-http", - "version": "0.1.90", + "version": "0.1.91", "homepage": "http://use-http.com", "main": "dist/index.js", "license": "MIT", diff --git a/src/useFetch.ts b/src/useFetch.ts index b198327e..401b8fb0 100644 --- a/src/useFetch.ts +++ b/src/useFetch.ts @@ -15,6 +15,9 @@ import useSSR from 'use-ssr' import makeRouteAndOptions from './makeRouteAndOptions' import { isEmpty, invariant } from './utils' +const makeResponseProxy = (res = {}) => new Proxy(res, { + get: (httpResponse: any, key) => (httpResponse.current || {})[key] +}) function useFetch(...args: UseFetchArgs): UseFetch { const { customOptions, requestInit, defaults } = useFetchArgs(...args) @@ -155,8 +158,8 @@ function useFetch(...args: UseFetchArgs): UseFetch { }, [onMount, executeRequest]) return Object.assign, UseFetchObjectReturn>( - [request, res.current, loading as boolean, error], - { request, response: res.current, ...request }, + [request, makeResponseProxy(res), loading as boolean, error], + { request, response: makeResponseProxy(res), ...request }, ) } diff --git a/src/useFetchArgs.ts b/src/useFetchArgs.ts index cb538904..7f90a12a 100644 --- a/src/useFetchArgs.ts +++ b/src/useFetchArgs.ts @@ -116,10 +116,11 @@ export default function useFetchArgs( const requestInit = pullOutRequestInit(requestInitOptions) return { + ...useFetchArgsDefaults.requestInit, ...contextRequestInit, ...requestInit, headers: { - ...useFetchArgsDefaults.requestInit.headers, + ...defaults.headers, ...contextRequestInit.headers, ...requestInit.headers, },