From 0e41d4ed65dff2d458b6d4e0d503cba6e850ce15 Mon Sep 17 00:00:00 2001 From: Alex Cory Date: Fri, 4 Oct 2019 17:39:56 +0800 Subject: [PATCH 1/9] v0.1.87 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f2caf30d..9cf444a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "use-http", - "version": "0.1.84", + "version": "0.1.87", "homepage": "http://use-http.com", "main": "dist/index.js", "license": "MIT", From 4a852a14f5d22f9c4d7dea48ec6b6aa057cf0246 Mon Sep 17 00:00:00 2001 From: Alex Cory Date: Mon, 7 Oct 2019 12:44:07 +0800 Subject: [PATCH 2/9] v0.1.88 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9cf444a5..c5fefcae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "use-http", - "version": "0.1.87", + "version": "0.1.88", "homepage": "http://use-http.com", "main": "dist/index.js", "license": "MIT", From 8fef7a528ae511eb39a156820c1c32c0f6631863 Mon Sep 17 00:00:00 2001 From: Alex Cory Date: Mon, 7 Oct 2019 12:46:43 +0800 Subject: [PATCH 3/9] v0.1.89 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c5fefcae..34f8fea1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "use-http", - "version": "0.1.88", + "version": "0.1.89", "homepage": "http://use-http.com", "main": "dist/index.js", "license": "MIT", From 8fc5f438ba9ba8c5f741d670cff6e6af66962cbb Mon Sep 17 00:00:00 2001 From: Alex Cory Date: Tue, 8 Oct 2019 12:57:36 +0800 Subject: [PATCH 4/9] v0.1.90 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 34f8fea1..2cdb664b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "use-http", - "version": "0.1.89", + "version": "0.1.90", "homepage": "http://use-http.com", "main": "dist/index.js", "license": "MIT", From f5bcb9ac119b55f631a037a5a3da7f42eb98a413 Mon Sep 17 00:00:00 2001 From: Alex Cory Date: Wed, 9 Oct 2019 17:58:33 +0800 Subject: [PATCH 5/9] v0.1.91 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", From 0b9e93c86c0d2bb7044854296a28f08def683e6e Mon Sep 17 00:00:00 2001 From: Alex Cory Date: Wed, 16 Oct 2019 17:38:16 +0800 Subject: [PATCH 6/9] fixed the issue where `response === {}` after making a request inside a `handleClick` function. aka the `response` object was stale --- src/useFetch.ts | 5 ++++- src/useFetchArgs.ts | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/useFetch.ts b/src/useFetch.ts index b198327e..60b777b1 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) @@ -156,7 +159,7 @@ function useFetch(...args: UseFetchArgs): UseFetch { return Object.assign, UseFetchObjectReturn>( [request, res.current, loading as boolean, error], - { request, response: res.current, ...request }, + { 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, }, From bce42b9af02d72bbb0006de4210fadc841e3b52a Mon Sep 17 00:00:00 2001 From: Alex Cory Date: Wed, 16 Oct 2019 17:40:50 +0800 Subject: [PATCH 7/9] added todo to add tests for this --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index eea8d808..bdd943af 100644 --- a/README.md +++ b/README.md @@ -576,6 +576,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 From 53b655abef465b77ec12eed6fddf00925827c409 Mon Sep 17 00:00:00 2001 From: Alex Cory Date: Wed, 16 Oct 2019 18:00:02 +0800 Subject: [PATCH 8/9] array destructuring + documentation fixes --- README.md | 12 +++++------- docs/README.md | 11 ++++------- src/useFetch.ts | 4 ++-- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index bdd943af..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 ``` +
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/src/useFetch.ts b/src/useFetch.ts index 60b777b1..568cb26a 100644 --- a/src/useFetch.ts +++ b/src/useFetch.ts @@ -16,7 +16,7 @@ import makeRouteAndOptions from './makeRouteAndOptions' import { isEmpty, invariant } from './utils' const makeResponseProxy = (res = {}) => new Proxy(res, { - get: (httpResponse: any, key) => httpResponse.current[key], + get: (httpResponse: any, key) => (httpResponse.current || {})[key] }) function useFetch(...args: UseFetchArgs): UseFetch { @@ -158,7 +158,7 @@ function useFetch(...args: UseFetchArgs): UseFetch { }, [onMount, executeRequest]) return Object.assign, UseFetchObjectReturn>( - [request, res.current, loading as boolean, error], + [request, makeResponseProxy(res.current), loading as boolean, error], { request, response: makeResponseProxy(res), ...request }, ) } From a6c0d8edc63be6ab866a2f3b0a57c51b9316e281 Mon Sep 17 00:00:00 2001 From: Alex Cory Date: Wed, 16 Oct 2019 18:01:12 +0800 Subject: [PATCH 9/9] fix --- src/useFetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/useFetch.ts b/src/useFetch.ts index 568cb26a..401b8fb0 100644 --- a/src/useFetch.ts +++ b/src/useFetch.ts @@ -158,7 +158,7 @@ function useFetch(...args: UseFetchArgs): UseFetch { }, [onMount, executeRequest]) return Object.assign, UseFetchObjectReturn>( - [request, makeResponseProxy(res.current), loading as boolean, error], + [request, makeResponseProxy(res), loading as boolean, error], { request, response: makeResponseProxy(res), ...request }, ) }