Skip to content

Commit f83288e

Browse files
committed
Merge pull request #20 from lexich/request_validation
Add validation to request helper
2 parents 3c8e769 + 935ffe1 commit f83288e

13 files changed

+272
-229
lines changed

.babelrc

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"stage": 0,
3-
"loose": "all"
2+
"presets": ["es2015", "stage-0"],
43
}

bower.json

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

dist/redux-api.js

+139-124
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

+68-68
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

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-api",
3-
"version": "0.6.7",
3+
"version": "0.6.8",
44
"author": {
55
"name": "Efremov Alex",
66
"email": "lexich121@gmail.com",
@@ -12,9 +12,9 @@
1212
"repository": "http://github.com/lexich/redux-api",
1313
"scripts": {
1414
"test": "npm run eslint && npm run mocha",
15-
"mocha": "istanbul test _mocha --report html -- --require babel/register test/*_spec.js --reporter spec",
15+
"mocha": "istanbul test _mocha --report html -- --require babel-core/register test/*_spec.js --reporter spec",
1616
"build": "rm -rf dist lib && npm run browser-dev && npm run browser-min && npm run compile",
17-
"coveralls": "istanbul cover node_modules/.bin/_mocha --report html --report lcovonly -- --require babel/register test/*_spec.js && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
17+
"coveralls": "istanbul cover node_modules/.bin/_mocha --report html --report lcovonly -- --require babel-core/register test/*_spec.js && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
1818
"eslint": "node_modules/.bin/eslint src test examples/isomorphic/app examples/isomorphic/server.js",
1919
"compile": "node_modules/.bin/babel src --out-dir lib",
2020
"browser-dev": "node_modules/.bin/webpack -d src/index.js dist/redux-api.js",
@@ -29,19 +29,23 @@
2929
"qs": "^5.2.0"
3030
},
3131
"devDependencies": {
32-
"babel": "^5.8.23",
32+
"babel": "^6.0.15",
33+
"babel-cli": "^6.1.2",
34+
"babel-core": "^6.1.2",
3335
"babel-eslint": "^4.1.3",
34-
"babel-loader": "^5.3.2",
35-
"chai": "^3.3.0",
36+
"babel-loader": "^6.1.0",
37+
"babel-preset-es2015": "^6.1.2",
38+
"babel-preset-stage-0": "^6.1.2",
39+
"chai": "^3.4.1",
3640
"coveralls": "^2.11.4",
37-
"eslint": "^1.6.0",
38-
"eslint-config-airbnb": "^0.1.0",
39-
"eslint-plugin-react": "^3.5.1",
41+
"eslint": "^1.9.0",
42+
"eslint-config-airbnb": "^1.0.0",
43+
"eslint-plugin-react": "^3.8.0",
4044
"husky": "^0.10.1",
41-
"istanbul": "^0.3.22",
45+
"istanbul": "^0.4.0",
4246
"mocha": "^2.3.3",
4347
"mocha-lcov-reporter": "^1.0.0",
44-
"webpack": "^1.12.2"
48+
"webpack": "^1.12.4"
4549
},
4650
"engines": {
4751
"node": ">=0.12.0"

src/actionFn.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
4848
const urlT = urlTransform(url, pathvars);
4949
const baseOptions = isFunction(options) ? options(urlT, params, getState) : options;
5050
const opts = { ...baseOptions, ...params };
51-
return meta.holder.fetch(urlT, opts);
51+
const response = meta.holder.fetch(urlT, opts);
52+
return !meta.validation ? response : response.then(
53+
(data)=> new Promise(
54+
(resolve, reject)=> meta.validation(data,
55+
(err)=> err ? reject(err) : resolve(data))));
5256
};
5357

5458
/**
@@ -77,9 +81,6 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
7781

7882
fetchResolver(0, fetchResolverOpts,
7983
(err)=> err ? pubsub.reject(err) : request(pathvars, params, getState)
80-
.then((data)=> !meta.validation ? data :
81-
new Promise((resolve, reject)=> meta.validation(data,
82-
(err)=> err ? reject(err) : resolve(data))))
8384
.then((data)=> {
8485
dispatch({ type: actionSuccess, syncing: false, data });
8586
each(meta.broadcast, (btype)=> dispatch({type: btype, data}));

test/actionFn_spec.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,21 @@ describe("actionFn", function() {
7676

7777
it("check request method", function() {
7878
let executeCounter = 0;
79-
const api = actionFn("/test", "test", null, ACTIONS, {holder: {fetch: ()=> {
80-
executeCounter++;
81-
return fetchSuccess();
82-
}}});
83-
const async = api.request();
79+
let urlFetch, paramsFetch;
80+
const api = actionFn("/test/:id", "test", null, ACTIONS, {holder: {
81+
fetch: (url, params)=> {
82+
executeCounter++;
83+
urlFetch = url;
84+
paramsFetch = params;
85+
return fetchSuccess();
86+
}
87+
}});
88+
const async = api.request({id: 2}, {hello: "world"});
8489
expect(async).to.be.an.instanceof(Promise);
8590
return async.then((data)=> {
8691
expect(data).to.eql({msg: "hello"});
92+
expect(urlFetch).to.eql("/test/2");
93+
expect(paramsFetch).to.eql({hello: "world"});
8794
});
8895
});
8996

@@ -250,6 +257,23 @@ describe("actionFn", function() {
250257
expect(expectedEvent).to.have.length(0);
251258
});
252259
});
260+
it("check validation with request method", function() {
261+
let expData, counter = 0;
262+
const meta = {
263+
holder: {fetch: fetchSuccess},
264+
validation(data, cb) {
265+
counter++;
266+
expData = data;
267+
cb();
268+
}
269+
};
270+
const api = actionFn("/test/:id", "test", null, ACTIONS, meta);
271+
return api.request({id: 1}).then((data)=> {
272+
expect(data).to.eql({msg: "hello"});
273+
expect(counter).to.eql(1);
274+
expect(expData).to.eql({msg: "hello"});
275+
});
276+
});
253277
it("check success validation", function() {
254278
let expData, counter = 0;
255279
const meta = {

test/adapters_fetch_spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use strict";
22
/* global describe, it */
33

4-
const expect = require("chai").expect;
5-
const fetch = require("../src/adapters/fetch");
4+
import {expect} from "chai";
5+
import fetch from "../src/adapters/fetch";
66

77
describe("fetch adapters", function() {
88
it("check", function() {

test/index_spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use strict";
22
/* global describe, it */
33

4-
const expect = require("chai").expect;
5-
const reduxApi = require("../src/index.js").default;
6-
const transformers = require("../src/index.js").transformers;
7-
const isFunction = require("lodash/lang/isFunction");
8-
const size = require("lodash/collection/size");
4+
import {expect} from "chai";
5+
import reduxApi from "../src/index.js";
6+
import {transformers} from "../src/index.js";
7+
import isFunction from "lodash/lang/isFunction";
8+
import size from "lodash/collection/size";
99

1010
function getState() {
1111
return {test: {loading: false, data: {}}};

test/reducerFn_spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22
/* global describe, it */
33

4-
const expect = require("chai").expect;
5-
const reducerFn = require("../src/reducerFn");
6-
const isFunction = require("lodash/lang/isFunction");
4+
import {expect} from "chai";
5+
import reducerFn from "../src/reducerFn";
6+
import isFunction from "lodash/lang/isFunction";
77

88
describe("reducerFn", function() {
99
it("check null params", function() {

test/urlTransform_spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use strict";
22
/* global describe, it */
33

4-
const expect = require("chai").expect;
5-
const urlTransform = require("../src/urlTransform");
4+
import {expect} from "chai";
5+
import urlTransform from "../src/urlTransform";
66

77
describe("urlTransform", function() {
88
it("check null params", function() {

0 commit comments

Comments
 (0)