Skip to content

Commit

Permalink
Merge pull request #31 from lexich/add_postfetch_option
Browse files Browse the repository at this point in the history
Add postfetch option
  • Loading branch information
lexich committed Dec 17, 2015
2 parents 0ac75b6 + a179300 commit d04b678
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 25 deletions.
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["es2015", "stage-0"],
"plugins": ["add-module-exports"]
}
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"no-empty": 0,
"guard-for-in": 0,
"comma-dangle": 0,
"space-before-function-paren": 0
"space-before-function-paren": 0,
"arrow-spacing": [2, { "before": false, "after": true }]
}
}
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ function (state, action) {
}
```

####postfetch
- @description: you can organize chain of calling events after the current endpoint will be successful executed
- @type: Array<Function>
- @default: null
- @example:
```js
{
user: "/user/info",
logout: {
url: "/user/logout",
postfetch: [
function({data, dispatch, getState}) {
dispatch(actions.user.reset());
}
]
}
}
```

####validation (data, callback)
- @param **data** - response data
> type: Object
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-api",
"version": "0.7.1",
"version": "0.7.2",
"main": "dist/redux-api.min.js",
"dependencies": {}
}
11 changes: 10 additions & 1 deletion dist/redux-api.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/redux-api.js.map

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions dist/redux-api.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/redux-api.min.js.map

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-api",
"version": "0.7.1",
"version": "0.7.2",
"author": {
"name": "Efremov Alex",
"email": "[email protected]",
Expand Down Expand Up @@ -31,16 +31,17 @@
},
"devDependencies": {
"babel": "^6.3.13",
"babel-cli": "^6.3.15",
"babel-core": "^6.3.15",
"babel-cli": "^6.3.17",
"babel-core": "^6.3.17",
"babel-eslint": "^4.1.6",
"babel-loader": "^6.2.0",
"babel-plugin-add-module-exports": "^0.1.1",
"babel-preset-es2015": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"chai": "^3.4.1",
"coveralls": "^2.11.4",
"coveralls": "^2.11.6",
"eslint": "^1.10.3",
"eslint-config-airbnb": "^2.0.0",
"eslint-config-airbnb": "^2.1.1",
"eslint-plugin-react": "^3.11.3",
"husky": "^0.10.2",
"isparta": "^4.0.0",
Expand Down
3 changes: 3 additions & 0 deletions src/actionFn.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
.then((data)=> {
dispatch({ type: actionSuccess, syncing: false, data });
each(meta.broadcast, (btype)=> dispatch({ type: btype, data }));
each(meta.postfetch, (postfetch)=> {
isFunction(postfetch) && postfetch({ data, getState, dispatch });
});
pubsub.resolve(getState()[name]);
})
.catch((error)=> {
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default function reduxApi(config) {

const {
url, options, transformer, broadcast,
reducerName, prefetch, validation, helpers
reducerName, prefetch, postfetch, validation, helpers
} = opts;

const ACTIONS = {
Expand All @@ -113,7 +113,7 @@ export default function reduxApi(config) {
broadcast,
virtual: !!opts.virtual,
actions: memo.actions,
prefetch, validation, helpers,
prefetch, postfetch, validation, helpers
};

memo.actions[key] = actionFn(url, key, options, ACTIONS, meta);
Expand Down
47 changes: 45 additions & 2 deletions test/actionFn_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe("actionFn", function() {
callOptions++;
return { ...params, test: 1 };
}, ACTIONS, {
fetch: function(url, opts) {
fetch(url, opts) {
checkOptions = opts;
return fetchSuccess();
}
Expand Down Expand Up @@ -336,6 +336,49 @@ describe("actionFn", function() {
expect(expData).to.eql({ msg: "hello" });
});
});
it("check postfetch option", function() {
let expectedOpts;
const meta = {
fetch: fetchSuccess,
postfetch: [
function(opts) {
expectedOpts = opts;
opts.dispatch({ type: "One", data: opts.data });
},
function(opts) {
opts.dispatch({ type: "Two", data: opts.data });
}
]
};
const api = actionFn("/test/:id", "test", null, ACTIONS, meta);
const expectedEvent = [{
type: ACTIONS.actionFetch,
syncing: false
}, {
type: ACTIONS.actionSuccess,
data: { msg: "hello" },
syncing: false
}, {
type: "One",
data: { msg: "hello" }
}, {
type: "Two",
data: { msg: "hello" }
}];
function dispatch(msg) {
expect(expectedEvent).to.have.length.above(0);
const exp = expectedEvent.shift();
expect(msg).to.eql(exp);
}
return new Promise((resolve)=> {
api(resolve)(dispatch, getState);
}).then(()=> {
expect(expectedOpts).to.exist;
expect(expectedOpts).to.include.keys("data", "getState", "dispatch");
expect(expectedOpts.getState).to.eql(getState);
expect(expectedOpts.dispatch).to.eql(dispatch);
});
});
it("check prefetch option", function() {
const checkPrefetch = [];
const meta = {
Expand All @@ -348,7 +391,7 @@ describe("actionFn", function() {
function(opts, cb) {
checkPrefetch.push(["two", opts]);
cb();
},
}
]
};
const api = actionFn("/test/:id", "test", null, ACTIONS, meta);
Expand Down

0 comments on commit d04b678

Please sign in to comment.