Async action creator (async-action-creator
)
![Coverage Status](https://coveralls.io/repos/github/goncy/async-action-creator/badge.svg?branch=master)
Async actions with steroids for Redux or whatever you want.
What
Action creator is a library that helps with handling async actions on redux or similar implementations, giving a couple of methods for dispatching async actions and handling actions status, errors, and responses in a separated reducers so you don't need to hesitate creating a reducer for that.
How
import {createAction} from 'async-action-creator'
const myAction = createAction('MY_ACTION')
import {REDUCER_NAME, reducer} from 'async-action-creator'
const rootReducer = combineReducers({
[REDUCER_NAME]: reducer
})
export default {
[myAction.TYPE]: {
action: myAction,
uri: "https://api.chucknorris.io/jokes/random",
method: "GET",
onResolve: response => response.value,
onReject: error => error.message,
options: options => ({
...options,
'content-type': 'text'
})
},
};
import {middleware} from 'async-action-creator'
...
createStore(
rootReducer,
applyMiddleware(middleware(services))
)
...
dispatch(myAction.run())
{type: 'MY_ACTION'}
{type: 'MY_ACTION_STARTED'}
{type: 'MY_ACTION_RESOLVED', payload: "Jean-Claude Van Damme once attempted to throw a Chuck Norris Roundhouse Kick. He was immediately arrested for fraud."}
const mapDispatchToProps = {
run: myAction.run,
start: myAction.start,
fetch: myAction.fetch,
update: myAction.update,
create: myAction.create,
remove: myAction.remove,
resolve: myAction.resolve,
reject: myAction.reject
}
const mapStateToProps = state => ({
status: myAction.getStatus(state),
error: myAction.getError(state),
response: myAction.getResponse(state)
)}
switch (type) {
case myAction.TYPE:
return {
...state,
hello: 'me'
}
case myAction.STARTED:
return {
...state,
hello: 'cat'
}
case myAction.FETCH:
return {
...state,
hello: 'rat'
}
case myAction.UPDATE:
return {
...state,
hello: 'rabbit'
}
case myAction.CREATE:
return {
...state,
hello: 'owl'
}
case myAction.REMOVE:
return {
...state,
hello: 'elephant'
}
case myAction.RESOLVED:
return {
...state,
hello: 'dog'
}
case myAction.REJECTED:
return {
...state,
hello: 'dodo'
}
default:
return state
}
Why
Sometimes you need an action status and you had to create a new reducer just for that, also, this reduces the boilerplate of creating a lot of actions for all your async actions.
Installation
yarn add async-action-creator
// or
npm install --save async-action-creator
Tests
// jest tests
yarn test
// jest coverage
yarn cover
Contributors
Simply create a pull request :)
- Code style: Standard
- FlowType used
Thanks
This project is based on the idea of make-action-creator by @ajchambeaud, and the getStatus
and getError
implementations to the same library from @pablen.
License
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.