crudSaga

crudSaga(options, extend) β‡’ function

This helper implements an entire REST sagas flow based on minimal configuration. As long as your endpoint follows standard REST, this helper can establish a basic starting point for CRUD functionality. It is extensible, and its defaults can be overwritten. The only requirements are a name and a sagaApi.
Returns: function - Function that accepts redux actions object
Param
Type
Required
Description
options
object
Yes
Options to pass to saga helper
options.name
string
Yes
REST resource name
options.sagaApi
string
Yes
A sagaApi instance
extend
function
No
A function to pass actions and add extra sagas

Simple Example:

1
const { createModule } from 'saga-slice';
2
const { crudInitialState, crudReducers } from 'saga-slice-helpers';
3
const { sagaApi } from './myApiFile';
4
​
5
const name = 'todos'; // should be rest api endpoint name
6
const initialState = crudInitialState();
7
const reducers = crudReducers();
8
const sagas = crudSaga({ name, sagaApi });
9
​
10
const { actions } = createModule({ name, initialState, reducers, sagas });
Copied!

Extended Example:

1
const { takeLatest } from 'redux-sagas/effects';
2
const { createModule } from 'saga-slice';
3
const { crudInitialState, crudReducers, lifecycleReducers } from 'saga-slice-helpers';
4
const { sagaApi } from './myApiFile';
5
const { history } from './utils';
6
​
7
const name = 'todos'; // should be rest api endpoint name
8
const initialState = crudInitialState();
9
const reducers = crudReducers({
10
...lifecycleReducers('associateUser')
11
});
12
const sagas = crudSaga({
13
name,
14
sagaApi
15
}, (A) => ({
16
[A.deleteDone]: function* () {
17
yield history.push('/todos');
18
},
19
[A.associateUser]: function* ({ payload }) {
20
const { userId, todoId } = payload;
21
yield sagaApi.put(
22
`/todos/${todoId}/user/${userId}`,
23
A.associateUserSuccess,
24
A.associateUserFail
25
)
26
}
27
}));
28
​
29
const { actions } = createModule({ name, initialState, reducers, sagas });
Copied!

REST implementation

crudSaga will implement the following based on name: 'todos':
Action Type
Method
Path
Success
Fail
Done
readAll
GET
/todos
readAllSuccess
readAllFail
readAllDone
readOne
GET
/todos/{id}
readOneSuccess
readOneFail
readOneDone
create
POST
/todos
createSuccess
createFail
createDone
update
PUT
/todos/{id}
updateSuccess
updateFail
updateDone
patch
PATCH
/todos/{id}
patchSuccess
patchFail
patchDone
delete
DELETE
/todos/{id}
deleteSuccess
deleteFail
deleteDone
Last modified 1yr ago