redux-easy2-crud
Version:
Easy crud programming with Redux
129 lines (102 loc) • 3.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.setRootPath = setRootPath;
exports.rawFetchPromise = rawFetchPromise;
var _isomorphicFetch = require('isomorphic-fetch');
var _isomorphicFetch2 = _interopRequireDefault(_isomorphicFetch);
var _docs = require('../constants/docs');
var TYPES = _interopRequireWildcard(_docs);
var _docs2 = require('../actions/docs');
var docActions = _interopRequireWildcard(_docs2);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function parseJSON(response) {
var contentType = response.headers.get('content-type');
if (contentType && contentType.indexOf('application/json') !== -1) {
return response.json();
}
return response;
}
function checkError(response) {
if (response.ok === false || response.status >= 400) {
var error = new Error(response.status);
error.response = response;
throw error;
}
return response;
}
var ROOT_PATH = 'http://localhost:3001/';
function setRootPath(url) {
ROOT_PATH = url;
}
var config = {};
function rawFetchPromise(url) {
var method = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'GET';
var body = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
var fullPath = ROOT_PATH + url;
return (0, _isomorphicFetch2.default)(fullPath, {
method: method,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}).then(parseJSON).then(checkError);
}
exports.default = function (store) {
return function (next) {
return function (action) {
config.dispatch = store.dispatch;
var returnValue = next(action);
switch (action.type) {
case TYPES.READ:
readStart(action.path);
break;
case TYPES.CREATE:
createStart(action.path, action);
break;
case TYPES.DELETE:
delStart(action);
break;
case TYPES.UPDATE:
updateStart(action);
break;
}
return returnValue;
};
};
};
/**
*
* @param path the path to read. path is the type of doc to read
*/
function readStart(path) {
return rawFetchPromise(path).then(function (result) {
config.dispatch(docActions.readSuccess(path, result));
}).catch(function (err) {
config.dispatch(docActions.crudError(TYPES.READ_ERROR, path, err));
});
}
function createStart(path, doc) {
return rawFetchPromise(path, 'post', doc).then(function (result) {
config.dispatch(docActions.createSuccess(result));
}).catch(function (err) {
config.dispatch(docActions.crudError(TYPES.CREATE_ERROR, path, err));
});
}
function updateStart(aDoc) {
return rawFetchPromise(aDoc.path + '/' + aDoc.id, 'put', aDoc).then(function (result) {
config.dispatch(docActions.updateSuccess(aDoc.path, result));
}).catch(function (err) {
config.dispatch(docActions.crudError(TYPES.UPDATE_ERROR, aDoc.path, err));
});
}
function delStart(aDoc) {
return rawFetchPromise(aDoc.path + '/' + aDoc.id, 'delete', aDoc).then(function (result) {
config.dispatch(docActions.delSuccess(aDoc));
}).catch(function (err) {
config.dispatch(docActions.crudError(TYPES.DELETE_ERROR, aDoc.path, err));
});
}