hc-redux-middleware
Advanced tools
Comparing version 1.0.2 to 1.0.3
113
index.js
@@ -1,53 +0,72 @@ | ||
import axios from 'axios' | ||
'use strict'; | ||
export function requestSendingMiddleware (store) { | ||
return next => action => { | ||
const { payload, meta, type } = action | ||
// if its a network request | ||
// fire an action that indicates the request has | ||
// been sent | ||
if (payload && payload.then) { | ||
const requestSendingAction = { | ||
payload: null, | ||
type: type + 'Sent', | ||
meta | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
exports.requestSendingMiddleware = requestSendingMiddleware; | ||
exports.send = send; | ||
exports.hcMiddleware = hcMiddleware; | ||
var _axios = require('axios'); | ||
var _axios2 = _interopRequireDefault(_axios); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function requestSendingMiddleware(store) { | ||
return function (next) { | ||
return function (action) { | ||
var payload = action.payload, | ||
meta = action.meta, | ||
type = action.type; | ||
// if its a network request | ||
// fire an action that indicates the request has | ||
// been sent | ||
if (payload && payload.then) { | ||
var requestSendingAction = { | ||
payload: null, | ||
type: type + 'Sent', | ||
meta: meta | ||
}; | ||
store.dispatch(requestSendingAction); | ||
} | ||
store.dispatch(requestSendingAction) | ||
return next(action); | ||
}; | ||
}; | ||
} | ||
function send(namespace, fnName, data) { | ||
var stringified = (typeof data === 'undefined' ? 'undefined' : _typeof(data)) === "object" ? JSON.stringify(data) : data; | ||
return _axios2.default.post('/fn/' + namespace + '/' + fnName, stringified).then(function (res) { | ||
if (typeof res.data === "string" && res.data.indexOf("Error") > -1) { | ||
return Promise.reject(new Error(res.data)); | ||
} | ||
return next(action) | ||
} | ||
return Promise.resolve(res.data); | ||
}).catch(function (err) { | ||
console.log(err); | ||
}); | ||
} | ||
export function send (namespace, fnName, data) { | ||
const stringified = typeof data === "object" ? JSON.stringify(data) : data | ||
return axios.post(`/fn/${namespace}/${fnName}`, stringified) | ||
.then(res => { | ||
if (typeof res.data === "string" && res.data.indexOf("Error") > -1) { | ||
return Promise.reject(new Error(res.data)) | ||
} | ||
return Promise.resolve(res.data) | ||
}) | ||
.catch(err => { | ||
console.log(err) | ||
}) | ||
function hcMiddleware(store) { | ||
return function (next) { | ||
return function (action) { | ||
var type = action.type, | ||
meta = action.meta; | ||
if (!(meta && meta.isHc)) return next(action); | ||
// the rest will be handled by redux-promises | ||
var sendRequest = send(meta.namespace, type, meta.data); | ||
sendRequest = meta.then ? sendRequest.then(meta.then) : sendRequest; | ||
var newAction = Object.assign({}, action, { | ||
payload: sendRequest | ||
}, { | ||
meta: Object.assign({}, meta, { isHc: false }) | ||
}); | ||
return next(newAction); | ||
}; | ||
}; | ||
} | ||
export function hcMiddleware (store) { | ||
return next => action => { | ||
const { type, meta } = action | ||
if (!(meta && meta.isHc)) return next(action) | ||
// the rest will be handled by redux-promises | ||
let sendRequest = send(meta.namespace, type, meta.data) | ||
sendRequest = meta.then ? sendRequest.then(meta.then) : sendRequest | ||
const newAction = Object.assign({}, | ||
action, | ||
{ | ||
payload: sendRequest | ||
}, | ||
{ | ||
meta: Object.assign({}, meta, { isHc: false }) | ||
} | ||
) | ||
return next(newAction) | ||
} | ||
} |
{ | ||
"name": "hc-redux-middleware", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "a few methods of redux middleware for holochain UI apps", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "babel main.js --out-file index.js" | ||
}, | ||
@@ -14,3 +15,7 @@ "author": "connorturland@gmail.com", | ||
"redux-promise": "^0.5.3" | ||
}, | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.0.0-beta.37", | ||
"babel-cli": "^6.26.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45846
7
110
2