async-action-creator
)
Async action creator (
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
// Creating an actionconst myAction = // Mounting the reducerconst rootReducer = // Creating the services myActionTYPE: action: myAction uri: "https://api.chucknorris.io/jokes/random" method: "GET" responsevalue errormessage ...options 'content-type': 'text' ; // Adding the middleware ...... // Middleware in action // Actions automatically dispatched: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." // Dispatching async actions manuallyconst mapDispatchToProps = run: myActionrun // run({foo: 'bar'}) -> {type: MY_ACTION, payload: {foo: 'bar'}} start: myActionstart // start({foo: 'bar'}) -> {type: MY_ACTION_STARTED, payload: {foo: 'bar'}} fetch: myActionfetch // fetch({foo: 'bar'}) -> {type: MY_ACTION_FETCH, payload: {foo: 'bar'}} update: myActionupdate // update({foo: 'bar'}) -> {type: MY_ACTION_UPDATE, payload: {foo: 'bar'}} create: myActioncreate // create({foo: 'bar'}) -> {type: MY_ACTION_CREATE, payload: {foo: 'bar'}} remove: myActionremove // remove({foo: 'bar'}) -> {type: MY_ACTION_REMOVE, payload: {foo: 'bar'}} resolve: myActionresolve // resolve({foo: 'bar'}) -> {type: MY_ACTION_RESOLVED, payload: {foo: 'bar'}} reject: myActionreject // reject({foo: 'bar'}) -> {type: MY_ACTION_REJECTED, payload: {foo: 'bar'}} // Getting action status, error and response// Using this with this `myAction.resolve({foo: 'bar'})` as the last action dispatchedconst mapStateToProps = status: myAction // => 'resolved' error: myAction // => undefined response: myAction // => {foo: 'bar'}) // Using actions in reducers
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// ornpm install --save async-action-creator
Tests
// jest testsyarn test// jest coverageyarn 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.