axios-hooks
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [1.6.0](https://github.com/simoneb/axios-hooks/compare/v1.5.0...v1.6.0) (2019-10-23) | ||
### Features | ||
* async refetch ([f018d0a](https://github.com/simoneb/axios-hooks/commit/f018d0a31685003244c2be677845c6195646f2bd)), closes [#51](https://github.com/simoneb/axios-hooks/issues/51) | ||
## [1.5.0](https://github.com/simoneb/axios-hooks/compare/v1.5.0-0...v1.5.0) (2019-10-23) | ||
@@ -7,0 +14,0 @@ |
@@ -162,7 +162,6 @@ import _regeneratorRuntime from "@babel/runtime/regenerator"; | ||
}); | ||
_context3.next = 11; | ||
break; | ||
return _context3.abrupt("return", response); | ||
case 8: | ||
_context3.prev = 8; | ||
case 9: | ||
_context3.prev = 9; | ||
_context3.t0 = _context3["catch"](0); | ||
@@ -174,4 +173,5 @@ dispatch({ | ||
}); | ||
throw _context3.t0; | ||
case 11: | ||
case 13: | ||
case "end": | ||
@@ -181,3 +181,3 @@ return _context3.stop(); | ||
} | ||
}, _callee3, null, [[0, 8]]); | ||
}, _callee3, null, [[0, 9]]); | ||
})); | ||
@@ -188,3 +188,3 @@ return _request.apply(this, arguments); | ||
function executeRequestWithCache(config, dispatch) { | ||
request(_extends({}, config, { | ||
return request(_extends({}, config, { | ||
adapter: cacheAdapter | ||
@@ -231,3 +231,3 @@ }), dispatch); | ||
if (!options.manual) { | ||
executeRequest(config, options, dispatch); | ||
executeRequest(config, options, dispatch)["catch"](function () {}); | ||
} // eslint-disable-next-line react-hooks/exhaustive-deps | ||
@@ -234,0 +234,0 @@ |
@@ -37,3 +37,3 @@ import { | ||
ResponseValues<T>, | ||
(config?: AxiosRequestConfig, options?: RefetchOptions) => void | ||
(config?: AxiosRequestConfig, options?: RefetchOptions) => AxiosPromise | ||
] | ||
@@ -40,0 +40,0 @@ |
@@ -186,7 +186,6 @@ "use strict"; | ||
}); | ||
_context3.next = 11; | ||
break; | ||
return _context3.abrupt("return", response); | ||
case 8: | ||
_context3.prev = 8; | ||
case 9: | ||
_context3.prev = 9; | ||
_context3.t0 = _context3["catch"](0); | ||
@@ -198,4 +197,5 @@ dispatch({ | ||
}); | ||
throw _context3.t0; | ||
case 11: | ||
case 13: | ||
case "end": | ||
@@ -205,3 +205,3 @@ return _context3.stop(); | ||
} | ||
}, _callee3, null, [[0, 8]]); | ||
}, _callee3, null, [[0, 9]]); | ||
})); | ||
@@ -212,3 +212,3 @@ return _request.apply(this, arguments); | ||
function executeRequestWithCache(config, dispatch) { | ||
request((0, _extends3["default"])({}, config, { | ||
return request((0, _extends3["default"])({}, config, { | ||
adapter: cacheAdapter | ||
@@ -255,3 +255,3 @@ }), dispatch); | ||
if (!options.manual) { | ||
executeRequest(config, options, dispatch); | ||
executeRequest(config, options, dispatch)["catch"](function () {}); | ||
} // eslint-disable-next-line react-hooks/exhaustive-deps | ||
@@ -258,0 +258,0 @@ |
{ | ||
"name": "axios-hooks", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "axios-hooks", | ||
@@ -80,3 +80,4 @@ "keywords": [ | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
"pre-commit": "lint-staged", | ||
"post-merge": "npm i" | ||
} | ||
@@ -83,0 +84,0 @@ }, |
@@ -37,3 +37,3 @@ import { | ||
ResponseValues<T>, | ||
(config?: AxiosRequestConfig, options?: RefetchOptions) => void | ||
(config?: AxiosRequestConfig, options?: RefetchOptions) => AxiosPromise | ||
] | ||
@@ -40,0 +40,0 @@ |
@@ -98,4 +98,6 @@ import React from 'react' | ||
dispatch({ type: actions.REQUEST_END, payload: response }) | ||
return response | ||
} catch (err) { | ||
dispatch({ type: actions.REQUEST_END, payload: err, error: true }) | ||
throw err | ||
} | ||
@@ -105,3 +107,3 @@ } | ||
function executeRequestWithCache(config, dispatch) { | ||
request({ ...config, adapter: cacheAdapter }, dispatch) | ||
return request({ ...config, adapter: cacheAdapter }, dispatch) | ||
} | ||
@@ -143,3 +145,3 @@ | ||
if (!options.manual) { | ||
executeRequest(config, options, dispatch) | ||
executeRequest(config, options, dispatch).catch(() => {}) | ||
} | ||
@@ -146,0 +148,0 @@ // eslint-disable-next-line react-hooks/exhaustive-deps |
@@ -39,15 +39,14 @@ import { renderHook, act } from '@testing-library/react-hooks' | ||
it('should reset error when request completes and returns data', async () => { | ||
axios.mockResolvedValueOnce({ data: 'whatever' }) | ||
const error = new Error('boom') | ||
axios.mockRejectedValue(error) | ||
const { result, waitForNextUpdate } = renderHook(() => useAxios('')) | ||
result.current[0].error = { | ||
isAxiosError: true, | ||
config: {}, | ||
name: '', | ||
message: '' | ||
} | ||
await waitForNextUpdate() | ||
expect(result.current[0].error).toBe(error) | ||
axios.mockResolvedValue({ data: 'whatever' }) | ||
// Refetch | ||
@@ -58,2 +57,4 @@ act(() => { | ||
await waitForNextUpdate() | ||
expect(result.current[0].error).toBe(null) | ||
@@ -90,2 +91,72 @@ }) | ||
describe('refetch', () => { | ||
describe('when axios resolves', () => { | ||
it('should resolve to the response by default', () => { | ||
const response = { data: 'whatever' } | ||
axios.mockResolvedValue(response) | ||
const { | ||
result: { | ||
current: [, refetch] | ||
} | ||
} = renderHook(() => useAxios('')) | ||
act(() => { | ||
expect(refetch()).resolves.toEqual(response) | ||
}) | ||
}) | ||
it('should resolve to the response when using cache', () => { | ||
const response = { data: 'whatever' } | ||
axios.mockResolvedValue(response) | ||
const { | ||
result: { | ||
current: [, refetch] | ||
} | ||
} = renderHook(() => useAxios('')) | ||
act(() => { | ||
expect(refetch({}, { useCache: true })).resolves.toEqual(response) | ||
}) | ||
}) | ||
}) | ||
describe('when axios rejects', () => { | ||
it('should reject with the error by default', () => { | ||
const error = new Error('boom') | ||
axios.mockRejectedValue(error) | ||
const { | ||
result: { | ||
current: [, refetch] | ||
} | ||
} = renderHook(() => useAxios('')) | ||
act(() => { | ||
expect(refetch()).rejects.toEqual(error) | ||
}) | ||
}) | ||
it('should reject with the error by when using cache', () => { | ||
const error = new Error('boom') | ||
axios.mockRejectedValue(error) | ||
const { | ||
result: { | ||
current: [, refetch] | ||
} | ||
} = renderHook(() => useAxios('')) | ||
act(() => { | ||
expect(refetch({}, { useCache: true })).rejects.toEqual(error) | ||
}) | ||
}) | ||
}) | ||
}) | ||
it('should return the same reference to the fetch function', async () => { | ||
@@ -92,0 +163,0 @@ axios.mockResolvedValue({ data: 'whatever' }) |
@@ -39,15 +39,14 @@ import { renderHook, act } from '@testing-library/react-hooks' | ||
it('should reset error when request completes and returns data', async () => { | ||
axios.mockResolvedValueOnce({ data: 'whatever' }) | ||
const error = new Error('boom') | ||
axios.mockRejectedValue(error) | ||
const { result, waitForNextUpdate } = renderHook(() => useAxios('')) | ||
result.current[0].error = { | ||
isAxiosError: true, | ||
config: {}, | ||
name: '', | ||
message: '' | ||
} | ||
await waitForNextUpdate() | ||
expect(result.current[0].error).toBe(error) | ||
axios.mockResolvedValue({ data: 'whatever' }) | ||
// Refetch | ||
@@ -58,2 +57,4 @@ act(() => { | ||
await waitForNextUpdate() | ||
expect(result.current[0].error).toBe(null) | ||
@@ -90,2 +91,84 @@ }) | ||
describe('refetch', () => { | ||
describe('when axios resolves', () => { | ||
it('should resolve to the response by default', () => { | ||
const response = { data: 'whatever' } | ||
axios.mockResolvedValue(response) | ||
const { | ||
result: { | ||
current: [, refetch] | ||
} | ||
} = renderHook(() => useAxios('')) | ||
act(() => { | ||
expect(refetch()).resolves.toEqual(response) | ||
}) | ||
}) | ||
it('should resolve to the response when using cache', () => { | ||
const response = { data: 'whatever' } | ||
axios.mockResolvedValue(response) | ||
const { | ||
result: { | ||
current: [, refetch] | ||
} | ||
} = renderHook(() => useAxios('')) | ||
act(() => { | ||
expect(refetch({}, { useCache: true })).resolves.toEqual(response) | ||
}) | ||
}) | ||
}) | ||
describe('when axios rejects', () => { | ||
it('should reject with the error by default', () => { | ||
const error = new Error('boom') | ||
axios.mockRejectedValue(error) | ||
const { | ||
result: { | ||
current: [, refetch] | ||
} | ||
} = renderHook(() => useAxios('')) | ||
act(() => { | ||
expect(refetch()).rejects.toEqual(error) | ||
}) | ||
}) | ||
it('should reject with the error by when using cache', () => { | ||
const error = new Error('boom') | ||
axios.mockRejectedValue(error) | ||
const { | ||
result: { | ||
current: [, refetch] | ||
} | ||
} = renderHook(() => useAxios('')) | ||
act(() => { | ||
expect(refetch({}, { useCache: true })).rejects.toEqual(error) | ||
}) | ||
}) | ||
}) | ||
}) | ||
it('should return the same reference to the fetch function', async () => { | ||
axios.mockResolvedValue({ data: 'whatever' }) | ||
const { result, rerender } = renderHook(() => useAxios('')) | ||
const firstRefetch = result.current[1] | ||
rerender() | ||
expect(result.current[1]).toBe(firstRefetch) | ||
}) | ||
describe('manual option', () => { | ||
@@ -92,0 +175,0 @@ it('should set loading to false', async () => { |
47770
1017