Skip to content

Commit 3c8e769

Browse files
committed
Merge pull request #18 from lexich/request_method
Extract `request` method for pure xhr request
2 parents b2a1192 + df3b950 commit 3c8e769

File tree

11 files changed

+108
-37
lines changed

11 files changed

+108
-37
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,11 @@ dispatch(rest.actions.entries.sync());
337337
338338
###Actions sub methods
339339
340-
#### sync
340+
#### sync(urlparams, params, callback)
341341
- @description: this method save you from twice requests flag `sync`. if `sync===true` requst wouldn't execute. In server-side mode calls twice
342+
- @param **urlparams** - update url according Url schema
343+
- @param **params** - add additional params to rest request
344+
- @param **callback** - callback function when action ends
342345
- @type: Function
343346
- @example:
344347
@@ -350,7 +353,7 @@ function onEnter(state, replaceState, callback) {
350353

351354
```
352355
353-
#### reset
356+
#### reset()
354357
- @description: Reset state of current reducer
355358
- @type: Function
356359
- @example:
@@ -362,6 +365,16 @@ function onLeave(state, replaceState, callback) {
362365

363366
```
364367
368+
#### request()
369+
- @description: Pure xhr request without sending events or catching reducers.
370+
- @type: Function
371+
- @example:
372+
```js
373+
import {actions} from "./rest";
374+
actions.entries.request().then((data)=> {
375+
....
376+
});
377+
```
365378
366379
### Url schema
367380
/api/v1/user/:id

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-api",
3-
"version": "0.6.6",
3+
"version": "0.6.7",
44
"main": "dist/redux-api.min.js",
55
"dependencies": {}
66
}

dist/redux-api.js

Lines changed: 39 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/isomorphic/app/pages/Repo.jsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import { Link } from "react-router";
55
class Repo extends React.Component {
66
render() {
77
const { repo } = this.props;
8-
const data = repo.data;
8+
const data = repo.data || {};
9+
const owner = data.owner || {};
910
return !repo.loading ? (
1011
<div className="Repo media">
1112
<div className="media-left">
12-
<Link to={`/${data.owner.login}`}>
13-
<img className="Repo__media media-object" src={data.owner.avatar_url } />
13+
<Link to={`/${owner.login}`}>
14+
<img className="Repo__media media-object" src={owner.avatar_url } />
1415
</Link>
1516
</div>
1617
<div className="media-body">
1718
<h4>{ data.name }</h4>
18-
<Link to={`/${data.owner.login}`}>@{ data.owner.login }</Link>&nbsp;
19+
<Link to={`/${owner.login}`}>@{ owner.login }</Link>&nbsp;
1920
Stars { data.stargazers_count } Forks {data.forks }
2021
</div>
2122
</div>) : ( <div className="Repo">Loading</div> );

examples/isomorphic/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212
"dependencies": {
1313
"ejs": "^2.3.4",
1414
"express": "^4.13.3",
15-
"history": "^1.11.1",
15+
"history": "^1.12.5",
1616
"isomorphic-fetch": "^2.1.1",
1717
"react": "^0.14.0",
18+
"react-dom": "^0.14.0",
1819
"react-redux": "^3.0.0",
1920
"react-router": "^1.0.0-rc3",
2021
"redux": "^3.0.2",
21-
"redux-thunk": "^1.0.0",
22-
"react-dom": "^0.14.0",
23-
"whatwg-fetch": "^0.9.0"
22+
"redux-thunk": "^1.0.0"
2423
},
2524
"devDependencies": {
2625
"babel-core": "^5.8.25",

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-api",
3-
"version": "0.6.6",
3+
"version": "0.6.7",
44
"author": {
55
"name": "Efremov Alex",
66
"email": "[email protected]",
@@ -26,19 +26,19 @@
2626
"dependencies": {
2727
"fast-apply": "0.0.3",
2828
"lodash": "^3.10.1",
29-
"qs": "^5.1.0"
29+
"qs": "^5.2.0"
3030
},
3131
"devDependencies": {
3232
"babel": "^5.8.23",
3333
"babel-eslint": "^4.1.3",
3434
"babel-loader": "^5.3.2",
3535
"chai": "^3.3.0",
3636
"coveralls": "^2.11.4",
37-
"eslint": "^1.5.1",
37+
"eslint": "^1.6.0",
3838
"eslint-config-airbnb": "^0.1.0",
39-
"eslint-plugin-react": "^3.4.2",
39+
"eslint-plugin-react": "^3.5.1",
4040
"husky": "^0.10.1",
41-
"istanbul": "^0.3.21",
41+
"istanbul": "^0.3.22",
4242
"mocha": "^2.3.3",
4343
"mocha-lcov-reporter": "^1.0.0",
4444
"webpack": "^1.12.2"

src/actionFn.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ function extractArgs(args) {
3737
export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
3838
const {actionFetch, actionSuccess, actionFail, actionReset} = ACTIONS;
3939
const pubsub = new PubSub();
40+
41+
/**
42+
* Fetch data from server
43+
* @param {Object} pathvars path vars for url
44+
* @param {Object} params fetch params
45+
* @param {Function} getState helper meta function
46+
*/
47+
const request = (pathvars, params, getState=none)=> {
48+
const urlT = urlTransform(url, pathvars);
49+
const baseOptions = isFunction(options) ? options(urlT, params, getState) : options;
50+
const opts = { ...baseOptions, ...params };
51+
return meta.holder.fetch(urlT, opts);
52+
};
53+
4054
/**
4155
* Fetch data from server
4256
* @param {Object} pathvars path vars for url
@@ -45,8 +59,6 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
4559
*/
4660
const fn = (...args)=> {
4761
const [pathvars, params, callback] = extractArgs(args);
48-
49-
const urlT = urlTransform(url, pathvars);
5062
const syncing = params ? !!params.syncing : false;
5163
params && delete params.syncing;
5264
pubsub.push(callback);
@@ -56,19 +68,15 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
5668
if (store && store.loading) {
5769
return;
5870
}
59-
6071
dispatch({ type: actionFetch, syncing});
61-
const baseOptions = isFunction(options) ? options(urlT, params, getState) : options;
62-
const opts = { ...baseOptions, ...params };
63-
6472
const fetchResolverOpts = {
6573
dispatch, getState,
6674
actions: meta.actions,
6775
prefetch: meta.prefetch
6876
};
6977

7078
fetchResolver(0, fetchResolverOpts,
71-
(err)=> err ? pubsub.reject(err) : meta.holder.fetch(urlT, opts)
79+
(err)=> err ? pubsub.reject(err) : request(pathvars, params, getState)
7280
.then((data)=> !meta.validation ? data :
7381
new Promise((resolve, reject)=> meta.validation(data,
7482
(err)=> err ? reject(err) : resolve(data))))
@@ -83,10 +91,17 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
8391
}));
8492
};
8593
};
94+
95+
/*
96+
Pure rest request
97+
*/
98+
fn.request = request;
99+
86100
/**
87101
* Reset store to initial state
88102
*/
89103
fn.reset = ()=> ({type: actionReset});
104+
90105
/**
91106
* Sync store with server. In server mode works as usual method.
92107
* If data have already synced, data would not fetch after call this method.

0 commit comments

Comments
 (0)