Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-easy-async",
"version": "0.0.12",
"version": "0.0.13",
"description": "Redux Easy Async makes handling asynchronous actions, such as API requests, simple, reliable, and powerful",
"main": "dist/index.js",
"scripts": {
Expand Down
13 changes: 7 additions & 6 deletions src/async-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@ const decorateActionCreator = (actionCreator, asyncConstants) => {
* // - `resp`: the raw api response. Because of the nature of the promises errors that
* // cause the makeRequest promise to be rejected will also get caught here as `resp`
* // and cause a failed request action.
* meta = {},
* meta: {},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

*
* // function that takes your redux state and returns true or false whether to proceed with
* // the request. For example: checking if there is already a similar request in progress or
* // the requested data is already cached. *OPTIONAL*
* shouldMakeRequest = (state) => true,
* shouldMakeRequest: (state) => true,
*
* // `parseStart`, `parseSuccess`, and `parseSuccess` can be useful if you want to modify
* // `parseStart`, `parseSuccess`, and `parseFail` can be useful if you want to modify
* // raw API responses, errors, etc. before passing them to your reducer. The return value
* // of each becomes the payload for start, success, and fail actions. By default response
* // will not be modified.
* //
* // the return value of `parseStart` becomes the payload for the start action. *OPTIONAL*
* parseStart = () => null,
* parseStart: () => null,
* // the return value of `parseSuccess` becomes the payload for the success action. *OPTIONAL*
* parseSuccess = resp => resp,
* parseSuccess: resp => resp,
* // the return value of `parseFail` becomes the payload for the fail action. *OPTIONAL*
* parseFail = resp => resp,
* parseFail: resp => resp,
* }
*
* })
Expand All @@ -93,6 +93,7 @@ export const createAsyncAction = (type, fn, options = {}) => {
return {
type: namespace,
...action,
actionCreatorArgs: args,
actionName: actionCreator.actionName,
startActionCreator: actionCreator.start,
successActionCreator: actionCreator.success,
Expand Down
5 changes: 5 additions & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export const createAsyncMiddleware = (options = {}) => {
makeRequest,
// additional meta that will be passed to the action if any - must be an object
meta = {},
// arguments passed to the action when it was invoked.
actionCreatorArgs = {},
// function that returns boolean for whether to proceed with the request.
shouldMakeRequest = () => true,
// on start the result of parse() is passed as the payload of the start action
Expand Down Expand Up @@ -77,6 +79,7 @@ export const createAsyncMiddleware = (options = {}) => {
meta: {
...meta,
actionName,
actionCreatorArgs,
asyncType: 'start',
asyncID,
requestStartTime,
Expand All @@ -90,6 +93,7 @@ export const createAsyncMiddleware = (options = {}) => {
meta: {
...meta,
actionName,
actionCreatorArgs,
asyncType: 'success',
asyncID,
requestStartTime,
Expand All @@ -103,6 +107,7 @@ export const createAsyncMiddleware = (options = {}) => {
meta: {
...meta,
actionName,
actionCreatorArgs,
asyncType: 'fail',
asyncID,
requestStartTime,
Expand Down
7 changes: 4 additions & 3 deletions test/async-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ describe('Async Actions', () => {
assert.equal(fn.success().type, 'SUCCESS_TEST');
assert.equal(fn.fail().type, 'FAIL_TEST');
});
it('action creator returns and action with all correct attributes', () => {
it('action creator returns an action with all correct attributes', () => {
const makeRequest = () => {};
const meta = {};
const actionCreatorArgs = [{ test: 'test' }, 'args'];
const shouldMakeRequest = () => true;
const parseStart = () => null;
const parseSuccess = resp => resp;
Expand All @@ -80,7 +81,7 @@ describe('Async Actions', () => {
parseFail,
}));

const action = fn();
const action = fn(...actionCreatorArgs);

// auto generated attributes
assert.propertyVal(action, 'type', 'REDUX_EASY_ASYNC_NAMESPACE');
Expand All @@ -96,6 +97,7 @@ describe('Async Actions', () => {
assert.propertyVal(action, 'parseStart', parseStart);
assert.propertyVal(action, 'parseSuccess', parseSuccess);
assert.propertyVal(action, 'parseFail', parseFail);
assert.deepEqual(action.actionCreatorArgs, actionCreatorArgs);
});
it('throws error if action creator does not return an object with a makeRequest function', () => {
let fn = createAsyncAction('TEST', () => {});
Expand All @@ -105,4 +107,3 @@ describe('Async Actions', () => {
});
});
});