Skip to content

Commit

Permalink
Merge pull request #20 from lexich/request_validation
Browse files Browse the repository at this point in the history
Add validation to request helper
  • Loading branch information
lexich committed Nov 11, 2015
2 parents 3c8e769 + 935ffe1 commit f83288e
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 229 deletions.
3 changes: 1 addition & 2 deletions .babelrc
@@ -1,4 +1,3 @@
{
"stage": 0,
"loose": "all"
"presets": ["es2015", "stage-0"],
}
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "redux-api",
"version": "0.6.7",
"version": "0.6.8",
"main": "dist/redux-api.min.js",
"dependencies": {}
}
263 changes: 139 additions & 124 deletions dist/redux-api.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

136 changes: 68 additions & 68 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.

26 changes: 15 additions & 11 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "redux-api",
"version": "0.6.7",
"version": "0.6.8",
"author": {
"name": "Efremov Alex",
"email": "lexich121@gmail.com",
Expand All @@ -12,9 +12,9 @@
"repository": "http://github.com/lexich/redux-api",
"scripts": {
"test": "npm run eslint && npm run mocha",
"mocha": "istanbul test _mocha --report html -- --require babel/register test/*_spec.js --reporter spec",
"mocha": "istanbul test _mocha --report html -- --require babel-core/register test/*_spec.js --reporter spec",
"build": "rm -rf dist lib && npm run browser-dev && npm run browser-min && npm run compile",
"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",
"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",
"eslint": "node_modules/.bin/eslint src test examples/isomorphic/app examples/isomorphic/server.js",
"compile": "node_modules/.bin/babel src --out-dir lib",
"browser-dev": "node_modules/.bin/webpack -d src/index.js dist/redux-api.js",
Expand All @@ -29,19 +29,23 @@
"qs": "^5.2.0"
},
"devDependencies": {
"babel": "^5.8.23",
"babel": "^6.0.15",
"babel-cli": "^6.1.2",
"babel-core": "^6.1.2",
"babel-eslint": "^4.1.3",
"babel-loader": "^5.3.2",
"chai": "^3.3.0",
"babel-loader": "^6.1.0",
"babel-preset-es2015": "^6.1.2",
"babel-preset-stage-0": "^6.1.2",
"chai": "^3.4.1",
"coveralls": "^2.11.4",
"eslint": "^1.6.0",
"eslint-config-airbnb": "^0.1.0",
"eslint-plugin-react": "^3.5.1",
"eslint": "^1.9.0",
"eslint-config-airbnb": "^1.0.0",
"eslint-plugin-react": "^3.8.0",
"husky": "^0.10.1",
"istanbul": "^0.3.22",
"istanbul": "^0.4.0",
"mocha": "^2.3.3",
"mocha-lcov-reporter": "^1.0.0",
"webpack": "^1.12.2"
"webpack": "^1.12.4"
},
"engines": {
"node": ">=0.12.0"
Expand Down
9 changes: 5 additions & 4 deletions src/actionFn.js
Expand Up @@ -48,7 +48,11 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
const urlT = urlTransform(url, pathvars);
const baseOptions = isFunction(options) ? options(urlT, params, getState) : options;
const opts = { ...baseOptions, ...params };
return meta.holder.fetch(urlT, opts);
const response = meta.holder.fetch(urlT, opts);
return !meta.validation ? response : response.then(
(data)=> new Promise(
(resolve, reject)=> meta.validation(data,
(err)=> err ? reject(err) : resolve(data))));
};

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

fetchResolver(0, fetchResolverOpts,
(err)=> err ? pubsub.reject(err) : request(pathvars, params, getState)
.then((data)=> !meta.validation ? data :
new Promise((resolve, reject)=> meta.validation(data,
(err)=> err ? reject(err) : resolve(data))))
.then((data)=> {
dispatch({ type: actionSuccess, syncing: false, data });
each(meta.broadcast, (btype)=> dispatch({type: btype, data}));
Expand Down
34 changes: 29 additions & 5 deletions test/actionFn_spec.js
Expand Up @@ -76,14 +76,21 @@ describe("actionFn", function() {

it("check request method", function() {
let executeCounter = 0;
const api = actionFn("/test", "test", null, ACTIONS, {holder: {fetch: ()=> {
executeCounter++;
return fetchSuccess();
}}});
const async = api.request();
let urlFetch, paramsFetch;
const api = actionFn("/test/:id", "test", null, ACTIONS, {holder: {
fetch: (url, params)=> {
executeCounter++;
urlFetch = url;
paramsFetch = params;
return fetchSuccess();
}
}});
const async = api.request({id: 2}, {hello: "world"});
expect(async).to.be.an.instanceof(Promise);
return async.then((data)=> {
expect(data).to.eql({msg: "hello"});
expect(urlFetch).to.eql("/test/2");
expect(paramsFetch).to.eql({hello: "world"});
});
});

Expand Down Expand Up @@ -250,6 +257,23 @@ describe("actionFn", function() {
expect(expectedEvent).to.have.length(0);
});
});
it("check validation with request method", function() {
let expData, counter = 0;
const meta = {
holder: {fetch: fetchSuccess},
validation(data, cb) {
counter++;
expData = data;
cb();
}
};
const api = actionFn("/test/:id", "test", null, ACTIONS, meta);
return api.request({id: 1}).then((data)=> {
expect(data).to.eql({msg: "hello"});
expect(counter).to.eql(1);
expect(expData).to.eql({msg: "hello"});
});
});
it("check success validation", function() {
let expData, counter = 0;
const meta = {
Expand Down
4 changes: 2 additions & 2 deletions test/adapters_fetch_spec.js
@@ -1,8 +1,8 @@
"use strict";
/* global describe, it */

const expect = require("chai").expect;
const fetch = require("../src/adapters/fetch");
import {expect} from "chai";
import fetch from "../src/adapters/fetch";

describe("fetch adapters", function() {
it("check", function() {
Expand Down
10 changes: 5 additions & 5 deletions test/index_spec.js
@@ -1,11 +1,11 @@
"use strict";
/* global describe, it */

const expect = require("chai").expect;
const reduxApi = require("../src/index.js").default;
const transformers = require("../src/index.js").transformers;
const isFunction = require("lodash/lang/isFunction");
const size = require("lodash/collection/size");
import {expect} from "chai";
import reduxApi from "../src/index.js";
import {transformers} from "../src/index.js";
import isFunction from "lodash/lang/isFunction";
import size from "lodash/collection/size";

function getState() {
return {test: {loading: false, data: {}}};
Expand Down
6 changes: 3 additions & 3 deletions test/reducerFn_spec.js
@@ -1,9 +1,9 @@
"use strict";
/* global describe, it */

const expect = require("chai").expect;
const reducerFn = require("../src/reducerFn");
const isFunction = require("lodash/lang/isFunction");
import {expect} from "chai";
import reducerFn from "../src/reducerFn";
import isFunction from "lodash/lang/isFunction";

describe("reducerFn", function() {
it("check null params", function() {
Expand Down
4 changes: 2 additions & 2 deletions test/urlTransform_spec.js
@@ -1,8 +1,8 @@
"use strict";
/* global describe, it */

const expect = require("chai").expect;
const urlTransform = require("../src/urlTransform");
import {expect} from "chai";
import urlTransform from "../src/urlTransform";

describe("urlTransform", function() {
it("check null params", function() {
Expand Down

0 comments on commit f83288e

Please sign in to comment.