Skip to content

Commit

Permalink
Bug/response interceptor cache (#236)
Browse files Browse the repository at this point in the history
* v0.4.4

* v0.4.5

* v1.0.0

* should fix, need to add tests

* adding test
  • Loading branch information
iamthesiz authored Apr 11, 2020
1 parent c6b4d7c commit 02ab59e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,10 @@ Todos
retryOnError: false,
refreshWhenHidden: false,
})


// potential for causing a rerender after clearing cache if needed
request.cache.clear(true)
```
- [ ] potential option ideas for `GraphQL`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-http",
"version": "0.4.5",
"version": "1.0.0",
"homepage": "http://use-http.com",
"main": "dist/index.js",
"license": "MIT",
Expand Down
Binary file added public/watch-youtube-video.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 24 additions & 1 deletion src/__tests__/useFetch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,29 @@ describe('useFetch - BROWSER - interceptors', (): void => {
await act(result.current.get)
expect((fetch.mock.calls[0][1] as any).data).toEqual('url')
})

it('should still call both interceptors when using cache', async (): Promise<void> => {
let requestCalled = 0
let responseCalled = 0
const { result, waitForNextUpdate } = renderHook(
() => useFetch('url', {
interceptors: {
async request(options) {
requestCalled++
return options
},
async response(response) {
responseCalled++
return response
}
}
}, []),
)
await waitForNextUpdate()
await act(result.current.get)
expect(requestCalled).toBe(2)
expect(responseCalled).toBe(2)
})
})

describe('useFetch - BROWSER - Overwrite Global Options set in Provider', (): void => {
Expand Down Expand Up @@ -813,7 +836,7 @@ describe('useFetch - BROWSER - retryOn & retryDelay', (): void => {

it('should retryOn specific error codes', async (): Promise<void> => {
const { result, waitForNextUpdate } = renderHook(
() => useFetch('url', {
() => useFetch('some-url-1234124', {
retries: 2,
retryOn: [401]
}, [])
Expand Down
8 changes: 5 additions & 3 deletions src/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
if (response.isCached && cachePolicy === CACHE_FIRST) {
try {
res.current = response.cached as Res<TData>
const d = await tryGetData(response.cached, defaults.data)
res.current.data = d
data.current = d
const theData = await tryGetData(response.cached, defaults.data)
res.current.data = theData
res.current = interceptors.response ? await interceptors.response(res.current) : res.current
invariant('data' in res.current, 'You must have `data` field on the Response returned from your `interceptors.response`')
data.current = res.current.data as TData
if (!suspense && mounted.current) forceUpdate()
return data.current
} catch (err) {
Expand Down

0 comments on commit 02ab59e

Please sign in to comment.