|
| 1 | +# `@ackee/antonio-utils` |
| 2 | + |
| 3 | +Custom Saga effects with built-in cancelation of API requests. |
| 4 | + |
| 5 | +## Table of contents |
| 6 | + |
| 7 | +- [Install](#install) |
| 8 | +- [API](#api) |
| 9 | + |
| 10 | + - Effect creators |
| 11 | + |
| 12 | + - [`takeRequest(actionTypes, saga)`](#api-takeRequest) |
| 13 | + - [`takeLatestRequest(params, saga)`](#api-takeLatestRequest) |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## <a name="install"></a>Install |
| 18 | + |
| 19 | +```bash |
| 20 | +yarn add @ackee/antonio-utils -S |
| 21 | +``` |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## <a name="api"></a>API Reference |
| 26 | + |
| 27 | +### <a name="api-takeRequest"></a>`takeRequest(actionTypes: Object, saga: Function)` |
| 28 | + |
| 29 | +#### Parameters |
| 30 | + |
| 31 | +- `actionTypes: Object` |
| 32 | + - `REQUEST: String` - action type that launches the saga |
| 33 | + - `CANCEL: String` - action type that aborts the running saga |
| 34 | +- `saga(requestAction, cancelToken): Function` - the actual API request is made here |
| 35 | + |
| 36 | +#### Example |
| 37 | + |
| 38 | +```js |
| 39 | +import { takeRequest } from '@ackee/antonio-utils'; |
| 40 | + |
| 41 | +export default function* () { |
| 42 | + // Works same as the Redux saga take effect, but on top of that, it cancels the API request. |
| 43 | + yield takeRequest( |
| 44 | + { |
| 45 | + REQUEST: 'FETCH_TODO_ITEM_REQUEST', |
| 46 | + CANCEL: 'FETCH_TODO_ITEM_INVALIDATE', |
| 47 | + }, |
| 48 | + fetchTodoItem, |
| 49 | + ); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +### <a name="api-takeLatestRequest"></a>`takeLatestRequest(params: Object, saga: Function)` |
| 56 | + |
| 57 | +#### Parameters |
| 58 | + |
| 59 | +- `params: Object` |
| 60 | + - `REQUEST: String` - action type that launches the saga |
| 61 | + - `cancelTask: Function` - Redux action that will cancel the |
| 62 | + running saga |
| 63 | + - `requestIdSelector: Function` (optional) - A function that receives request action as 1st arg. and returns unique ID of this action, e.g. user ID. |
| 64 | +- `saga(requestAction, cancelToken): Function` - the actual API request is made here |
| 65 | + |
| 66 | +#### Example |
| 67 | + |
| 68 | +```js |
| 69 | +import { takeLatestRequest } from '@ackee/antonio-utils'; |
| 70 | + |
| 71 | +// The 'cancelToken' must be passed to the request config object: |
| 72 | +function* fetchTodoItem(requestAction, cancelToken) { |
| 73 | + const response = yield api.get(`todos/1`, { |
| 74 | + cancelToken, |
| 75 | + }); |
| 76 | + |
| 77 | + return response.data; |
| 78 | +} |
| 79 | + |
| 80 | +const fetchTodoItemInvalidate = () => ({ |
| 81 | + type: 'FETCH_TODO_ITEM_INVALIDATE', |
| 82 | +}); |
| 83 | + |
| 84 | +export default function* () { |
| 85 | + // Works same as the Redux saga takeLatest effect, but on top of that, it cancels the API request. |
| 86 | + yield takeLatestRequest( |
| 87 | + { |
| 88 | + REQUEST: 'FETCH_TODO_ITEM_REQUEST', |
| 89 | + cancelTask: fetchTodoItemInvalidate, |
| 90 | + }, |
| 91 | + fetchTodoItem, |
| 92 | + ); |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### Example - take latest request for certain user |
| 97 | + |
| 98 | +If `requestIdSelector` function provided, instead of cancelling of all previous requests and taking only the last one for certain action type, take the lastest request for certain user, i.e. **identify the request by action type and by an ID**. |
| 99 | + |
| 100 | +```js |
| 101 | +import { takeLatestRequest } from '@ackee/antonio-utils'; |
| 102 | + |
| 103 | +// The 'cancelToken' must be passed to the request config object: |
| 104 | +function* fetchUser(requestAction, cancelToken) { |
| 105 | + const { userId } = requestAction; |
| 106 | + const response = yield api.get(`users/${userId}`, { |
| 107 | + cancelToken, |
| 108 | + }); |
| 109 | + |
| 110 | + return response.data; |
| 111 | +} |
| 112 | + |
| 113 | +const fetchUserInvalidate = userId => ({ |
| 114 | + type: 'FETCH_USER_INVALIDATE', |
| 115 | + userId, |
| 116 | +}); |
| 117 | + |
| 118 | +export default function* () { |
| 119 | + // Works same as the Redux saga takeLatest effect, but on top of that, it cancels the API request. |
| 120 | + yield takeLatestRequest( |
| 121 | + { |
| 122 | + REQUEST: 'FETCH_USER_REQUEST', |
| 123 | + cancelTask: fetchUserInvalidate, |
| 124 | + requestIdSelector: action => action.userId, |
| 125 | + }, |
| 126 | + fetchUser, |
| 127 | + ); |
| 128 | +} |
| 129 | +``` |
0 commit comments