Skip to content

Commit d04b678

Browse files
committed
Merge pull request #31 from lexich/add_postfetch_option
Add postfetch option
2 parents 0ac75b6 + a179300 commit d04b678

12 files changed

+102
-25
lines changed

.babelrc

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
22
"presets": ["es2015", "stage-0"],
3+
"plugins": ["add-module-exports"]
34
}

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"no-empty": 0,
4646
"guard-for-in": 0,
4747
"comma-dangle": 0,
48-
"space-before-function-paren": 0
48+
"space-before-function-paren": 0,
49+
"arrow-spacing": [2, { "before": false, "after": true }]
4950
}
5051
}

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,25 @@ function (state, action) {
181181
}
182182
```
183183

184+
####postfetch
185+
- @description: you can organize chain of calling events after the current endpoint will be successful executed
186+
- @type: Array<Function>
187+
- @default: null
188+
- @example:
189+
```js
190+
{
191+
user: "/user/info",
192+
logout: {
193+
url: "/user/logout",
194+
postfetch: [
195+
function({data, dispatch, getState}) {
196+
dispatch(actions.user.reset());
197+
}
198+
]
199+
}
200+
}
201+
```
202+
184203
####validation (data, callback)
185204
- @param **data** - response data
186205
> type: Object

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-api",
3-
"version": "0.7.1",
3+
"version": "0.7.2",
44
"main": "dist/redux-api.min.js",
55
"dependencies": {}
66
}

dist/redux-api.js

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.min.js

+11-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-api",
3-
"version": "0.7.1",
3+
"version": "0.7.2",
44
"author": {
55
"name": "Efremov Alex",
66
"email": "lexich121@gmail.com",
@@ -31,16 +31,17 @@
3131
},
3232
"devDependencies": {
3333
"babel": "^6.3.13",
34-
"babel-cli": "^6.3.15",
35-
"babel-core": "^6.3.15",
34+
"babel-cli": "^6.3.17",
35+
"babel-core": "^6.3.17",
3636
"babel-eslint": "^4.1.6",
3737
"babel-loader": "^6.2.0",
38+
"babel-plugin-add-module-exports": "^0.1.1",
3839
"babel-preset-es2015": "^6.3.13",
3940
"babel-preset-stage-0": "^6.3.13",
4041
"chai": "^3.4.1",
41-
"coveralls": "^2.11.4",
42+
"coveralls": "^2.11.6",
4243
"eslint": "^1.10.3",
43-
"eslint-config-airbnb": "^2.0.0",
44+
"eslint-config-airbnb": "^2.1.1",
4445
"eslint-plugin-react": "^3.11.3",
4546
"husky": "^0.10.2",
4647
"isparta": "^4.0.0",

src/actionFn.js

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
9696
.then((data)=> {
9797
dispatch({ type: actionSuccess, syncing: false, data });
9898
each(meta.broadcast, (btype)=> dispatch({ type: btype, data }));
99+
each(meta.postfetch, (postfetch)=> {
100+
isFunction(postfetch) && postfetch({ data, getState, dispatch });
101+
});
99102
pubsub.resolve(getState()[name]);
100103
})
101104
.catch((error)=> {

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export default function reduxApi(config) {
9595

9696
const {
9797
url, options, transformer, broadcast,
98-
reducerName, prefetch, validation, helpers
98+
reducerName, prefetch, postfetch, validation, helpers
9999
} = opts;
100100

101101
const ACTIONS = {
@@ -113,7 +113,7 @@ export default function reduxApi(config) {
113113
broadcast,
114114
virtual: !!opts.virtual,
115115
actions: memo.actions,
116-
prefetch, validation, helpers,
116+
prefetch, postfetch, validation, helpers
117117
};
118118

119119
memo.actions[key] = actionFn(url, key, options, ACTIONS, meta);

test/actionFn_spec.js

+45-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe("actionFn", function() {
186186
callOptions++;
187187
return { ...params, test: 1 };
188188
}, ACTIONS, {
189-
fetch: function(url, opts) {
189+
fetch(url, opts) {
190190
checkOptions = opts;
191191
return fetchSuccess();
192192
}
@@ -336,6 +336,49 @@ describe("actionFn", function() {
336336
expect(expData).to.eql({ msg: "hello" });
337337
});
338338
});
339+
it("check postfetch option", function() {
340+
let expectedOpts;
341+
const meta = {
342+
fetch: fetchSuccess,
343+
postfetch: [
344+
function(opts) {
345+
expectedOpts = opts;
346+
opts.dispatch({ type: "One", data: opts.data });
347+
},
348+
function(opts) {
349+
opts.dispatch({ type: "Two", data: opts.data });
350+
}
351+
]
352+
};
353+
const api = actionFn("/test/:id", "test", null, ACTIONS, meta);
354+
const expectedEvent = [{
355+
type: ACTIONS.actionFetch,
356+
syncing: false
357+
}, {
358+
type: ACTIONS.actionSuccess,
359+
data: { msg: "hello" },
360+
syncing: false
361+
}, {
362+
type: "One",
363+
data: { msg: "hello" }
364+
}, {
365+
type: "Two",
366+
data: { msg: "hello" }
367+
}];
368+
function dispatch(msg) {
369+
expect(expectedEvent).to.have.length.above(0);
370+
const exp = expectedEvent.shift();
371+
expect(msg).to.eql(exp);
372+
}
373+
return new Promise((resolve)=> {
374+
api(resolve)(dispatch, getState);
375+
}).then(()=> {
376+
expect(expectedOpts).to.exist;
377+
expect(expectedOpts).to.include.keys("data", "getState", "dispatch");
378+
expect(expectedOpts.getState).to.eql(getState);
379+
expect(expectedOpts.dispatch).to.eql(dispatch);
380+
});
381+
});
339382
it("check prefetch option", function() {
340383
const checkPrefetch = [];
341384
const meta = {
@@ -348,7 +391,7 @@ describe("actionFn", function() {
348391
function(opts, cb) {
349392
checkPrefetch.push(["two", opts]);
350393
cb();
351-
},
394+
}
352395
]
353396
};
354397
const api = actionFn("/test/:id", "test", null, ACTIONS, meta);

0 commit comments

Comments
 (0)