redux-supermodel
Advanced tools
Comparing version 0.10.1 to 0.11.0
@@ -29,5 +29,7 @@ 'use strict'; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
const actionMap = exports.actionMap = { fetch: 'get', create: 'post', update: 'put', destroy: 'delete' }; | ||
var actionMap = exports.actionMap = { fetch: 'get', create: 'post', update: 'put', destroy: 'delete' }; | ||
@@ -39,6 +41,8 @@ function urlAppend() { | ||
const first = parts[0], | ||
rest = parts.slice(1); | ||
var first = parts[0], | ||
rest = parts.slice(1); | ||
return rest.reduce((memo, str) => memo.replace(/[^/]$/, '$&/') + str.replace(/^\/|\/$/g, ''), first); | ||
return rest.reduce(function (memo, str) { | ||
return memo.replace(/[^/]$/, '$&/') + str.replace(/^\/|\/$/g, ''); | ||
}, first); | ||
} | ||
@@ -51,3 +55,3 @@ | ||
function resourceActionCreators(baseUrl, resourceName, definition) { | ||
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
@@ -58,8 +62,8 @@ if (!definition) { | ||
const idAttribute = definition.idAttribute, | ||
url = definition.url, | ||
urlRoot = definition.urlRoot, | ||
rootParam = definition.rootParam; | ||
var idAttribute = definition.idAttribute, | ||
url = definition.url, | ||
urlRoot = definition.urlRoot, | ||
rootParam = definition.rootParam; | ||
let agent = options.agent, | ||
var agent = options.agent, | ||
before = options.before, | ||
@@ -74,33 +78,33 @@ rest = _objectWithoutProperties(options, ['agent', 'before']); | ||
const fn = (memo, action) => { | ||
const method = actionMap[action] || action; | ||
var fn = function fn(memo, action) { | ||
var method = actionMap[action] || action; | ||
function getResourceUrl() { | ||
let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
if (urlRoot) { | ||
const part = typeof urlRoot === 'function' && urlRoot.call(definition, { data: data, method: method }) || urlRoot; | ||
const id = data[idAttribute || 'id']; | ||
var _part = typeof urlRoot === 'function' && urlRoot.call(definition, { data: data, method: method }) || urlRoot; | ||
var id = data[idAttribute || 'id']; | ||
if (!id) { | ||
return urlAppend(baseUrl, part); | ||
return urlAppend(baseUrl, _part); | ||
} | ||
return urlAppend(baseUrl, part, encodeURIComponent(id)); | ||
return urlAppend(baseUrl, _part, encodeURIComponent(id)); | ||
} | ||
const part = typeof url === 'function' && url.call(definition, { data: data, method: method }) || url; | ||
var part = typeof url === 'function' && url.call(definition, { data: data, method: method }) || url; | ||
return urlAppend(baseUrl, part); | ||
} | ||
memo[action] = inputData => { | ||
const requestUrl = getResourceUrl(inputData); | ||
memo[action] = function (inputData) { | ||
var requestUrl = getResourceUrl(inputData); | ||
let data = inputData; | ||
var data = inputData; | ||
if (rootParam === true || typeof rootParam === 'string') { | ||
const rootLabel = rootParam === true ? resourceName : rootParam; | ||
data = { [rootLabel]: data }; | ||
var rootLabel = rootParam === true ? resourceName : rootParam; | ||
data = _defineProperty({}, rootLabel, data); | ||
} | ||
let config = _extends({}, rest, { url: requestUrl, method: method, data: data }); | ||
var config = _extends({}, rest, { url: requestUrl, method: method, data: data }); | ||
if (before) { | ||
@@ -110,3 +114,3 @@ config = before(config) || config; | ||
const payload = agent(config); | ||
var payload = agent(config); | ||
@@ -123,3 +127,3 @@ return { | ||
const resource = { | ||
var resource = { | ||
reset: function reset(payload) { | ||
@@ -126,0 +130,0 @@ return { |
@@ -6,11 +6,11 @@ 'use strict'; | ||
}); | ||
const PREFIX = exports.PREFIX = '@@redux-supermodel/'; | ||
var PREFIX = exports.PREFIX = '@@redux-supermodel/'; | ||
const NUKE = exports.NUKE = `${ PREFIX }NUKE`; | ||
const REQUEST = exports.REQUEST = `${ PREFIX }REQUEST`; | ||
const RESET = exports.RESET = `${ PREFIX }RESET`; | ||
var NUKE = exports.NUKE = PREFIX + 'NUKE'; | ||
var REQUEST = exports.REQUEST = PREFIX + 'REQUEST'; | ||
var RESET = exports.RESET = PREFIX + 'RESET'; | ||
// AJAX Action Types | ||
const PENDING = exports.PENDING = `${ PREFIX }REQUEST_PENDING`; | ||
const FULFILLED = exports.FULFILLED = `${ PREFIX }REQUEST_FULFILLED`; | ||
const REJECTED = exports.REJECTED = `${ PREFIX }REQUEST_REJECTED`; | ||
var PENDING = exports.PENDING = PREFIX + 'REQUEST_PENDING'; | ||
var FULFILLED = exports.FULFILLED = PREFIX + 'REQUEST_FULFILLED'; | ||
var REJECTED = exports.REJECTED = PREFIX + 'REQUEST_REJECTED'; |
@@ -9,2 +9,4 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
exports.default = bindResource; | ||
@@ -20,6 +22,12 @@ | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function bindResource(resources) { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
const redux = require('redux'); | ||
var redux = require('redux'); | ||
if (!redux) { | ||
@@ -29,3 +37,3 @@ throw new Error('You must install redux before you can call bindResource.'); | ||
const reactRedux = require('react-redux'); | ||
var reactRedux = require('react-redux'); | ||
if (!reactRedux) { | ||
@@ -35,43 +43,64 @@ throw new Error('You must install react-redux before you can call bindResource.'); | ||
const mount = options.mount; | ||
var mount = options.mount; | ||
return function bindResourceWrapper(WrappedComponent) { | ||
class ResourceBound extends _react2.default.Component { | ||
constructor() { | ||
super(); | ||
this.refWrapped = this.refWrapped.bind(this); | ||
} | ||
var ResourceBound = function (_React$Component) { | ||
_inherits(ResourceBound, _React$Component); | ||
componentDidMount() { | ||
if (mount) { | ||
return mount(this.props); | ||
} else if (mount !== false) { | ||
return this.props.fetchAll(); | ||
} | ||
} | ||
function ResourceBound() { | ||
_classCallCheck(this, ResourceBound); | ||
componentWillUnmount() { | ||
this.props.resetAll(); | ||
} | ||
var _this = _possibleConstructorReturn(this, (ResourceBound.__proto__ || Object.getPrototypeOf(ResourceBound)).call(this)); | ||
refWrapped(ref) { | ||
this.wrapped = ref; | ||
_this.refWrapped = _this.refWrapped.bind(_this); | ||
return _this; | ||
} | ||
render() { | ||
return _react2.default.createElement(WrappedComponent, _extends({ ref: this.refWrapped }, this.props)); | ||
} | ||
} | ||
_createClass(ResourceBound, [{ | ||
key: 'componentDidMount', | ||
value: function componentDidMount() { | ||
if (mount) { | ||
var result = mount(this.props); | ||
if (result && typeof result.catch === 'function') { | ||
return result.catch(this.props.onMountError); | ||
} else { | ||
return result; | ||
} | ||
} else if (mount !== false) { | ||
return this.props.fetchAll().catch(this.props.onMountError); | ||
} | ||
} | ||
}, { | ||
key: 'componentWillUnmount', | ||
value: function componentWillUnmount() { | ||
this.props.resetAll().catch(this.props.onUnmountError); | ||
} | ||
}, { | ||
key: 'refWrapped', | ||
value: function refWrapped(ref) { | ||
this.wrapped = ref; | ||
} | ||
}, { | ||
key: 'render', | ||
value: function render() { | ||
return _react2.default.createElement(WrappedComponent, _extends({ ref: this.refWrapped }, this.props)); | ||
} | ||
}]); | ||
return ResourceBound; | ||
}(_react2.default.Component); | ||
ResourceBound.propTypes = { | ||
fetchAll: _react2.default.PropTypes.func.isRequired, | ||
resetAll: _react2.default.PropTypes.func.isRequired | ||
resetAll: _react2.default.PropTypes.func.isRequired, | ||
onMountError: _react2.default.PropTypes.func, | ||
onUnmountError: _react2.default.PropTypes.func | ||
}; | ||
ResourceBound.displayName = `ResourceBound(${ WrappedComponent.displayName || WrappedComponent.name || 'Anonymous' })`; | ||
ResourceBound.displayName = 'ResourceBound(' + (WrappedComponent.displayName || WrappedComponent.name || 'Anonymous') + ')'; | ||
const keys = Object.keys(resources); | ||
var keys = Object.keys(resources); | ||
function mapStateToProps(state) { | ||
return keys.reduce((accum, key) => { | ||
return keys.reduce(function (accum, key) { | ||
accum[key] = resources[key](state); | ||
@@ -83,14 +112,13 @@ return accum; | ||
function mapDispatchToProps(dispatch, ownProps) { | ||
const actions = ['fetch', 'create', 'update', 'destroy', 'reset']; | ||
const actionName = (action, _ref) => { | ||
var _ref2 = _toArray(_ref); | ||
let first = _ref2[0], | ||
var actions = ['fetch', 'create', 'update', 'destroy', 'reset']; | ||
var actionName = function actionName(action, _ref) { | ||
var _ref2 = _toArray(_ref), | ||
first = _ref2[0], | ||
rest = _ref2.slice(1); | ||
return `${ action }${ first.toUpperCase() }${ rest.join('') }`; | ||
return '' + action + first.toUpperCase() + rest.join(''); | ||
}; | ||
const actionCreators = redux.bindActionCreators(keys.reduce((accum, key) => { | ||
actions.forEach(action => { | ||
var actionCreators = redux.bindActionCreators(keys.reduce(function (accum, key) { | ||
actions.forEach(function (action) { | ||
accum[actionName(action, key)] = resources[key][action]; | ||
@@ -103,6 +131,10 @@ }); | ||
fetchAll: function fetchAll() { | ||
keys.forEach(key => dispatch(resources[key].fetch(ownProps))); | ||
return Promise.all(keys.map(function (key) { | ||
return dispatch(resources[key].fetch(ownProps)); | ||
})); | ||
}, | ||
resetAll: function resetAll() { | ||
keys.forEach(key => dispatch(resources[key].reset())); | ||
return Promise.all(keys.map(function (key) { | ||
return dispatch(resources[key].reset()); | ||
})); | ||
} | ||
@@ -109,0 +141,0 @@ }); |
@@ -9,2 +9,4 @@ 'use strict'; | ||
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.default = createClient; | ||
@@ -21,5 +23,5 @@ | ||
function createClient(baseUrl) { | ||
let clientOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var clientOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (arguments.length === 1 && typeof baseUrl === 'object') { | ||
if (arguments.length === 1 && (typeof baseUrl === 'undefined' ? 'undefined' : _typeof(baseUrl)) === 'object') { | ||
// Calling createClient({ ... }) is equivalent to createClient('/', {...}) | ||
@@ -31,5 +33,5 @@ clientOptions = baseUrl; | ||
return function createResource(name) { | ||
let definitionOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var definitionOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
let options = definitionOptions; | ||
var options = definitionOptions; | ||
@@ -44,5 +46,5 @@ if (!name) { | ||
const actionCreators = (0, _actionCreators.resourceActionCreators)(baseUrl, name, options, clientOptions); | ||
var actionCreators = (0, _actionCreators.resourceActionCreators)(baseUrl, name, options, clientOptions); | ||
return Object.assign((0, _mapResourceStateToProps2.default)(name, options), options, actionCreators); | ||
}; | ||
} |
@@ -10,26 +10,28 @@ 'use strict'; | ||
exports.default = withDefinition; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
// Generates a function when given a resource definition that will transform the resource's state in redux into a prop for the component | ||
const _metaTag = exports._metaTag = Symbol('redux.supermodel.resource'); | ||
var _metaTag = exports._metaTag = Symbol('redux.supermodel.resource'); | ||
function withDefinition(name) { | ||
let definition = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
const defaultPayload = definition.defaultPayload; | ||
var definition = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var defaultPayload = definition.defaultPayload; | ||
return function mapResourceStateToProps(state) { | ||
let mapOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var mapOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
const mountedAt = mapOptions.mountedAt || 'resource'; | ||
const resource = state[mountedAt] && state[mountedAt][name]; | ||
var mountedAt = mapOptions.mountedAt || 'resource'; | ||
var resource = state[mountedAt] && state[mountedAt][name]; | ||
if (!resource || !resource.initialized) { | ||
return { | ||
payload: defaultPayload, | ||
[_metaTag]: true | ||
}; | ||
return _defineProperty({ | ||
payload: defaultPayload | ||
}, _metaTag, true); | ||
} | ||
const pendingAction = resource.busy && resource.meta && resource.meta.action; | ||
return _extends({}, resource, { | ||
var pendingAction = resource.busy && resource.meta && resource.meta.action; | ||
return _extends({}, resource, _defineProperty({ | ||
payload: resource.payload || defaultPayload, | ||
@@ -40,6 +42,5 @@ ready: !resource.busy, | ||
pendingUpdate: pendingAction === 'update', | ||
pendingDestroy: pendingAction === 'destroy', | ||
[_metaTag]: true | ||
}); | ||
pendingDestroy: pendingAction === 'destroy' | ||
}, _metaTag, true)); | ||
}; | ||
} |
@@ -10,3 +10,3 @@ 'use strict'; | ||
function chainablePropType(predicate) { | ||
const propType = (props, propName, componentName) => { | ||
var propType = function propType(props, propName, componentName) { | ||
if (props[propName] == null) return; | ||
@@ -16,5 +16,5 @@ return predicate(props, propName, componentName); | ||
propType.isRequired = (props, propName, componentName) => { | ||
propType.isRequired = function (props, propName, componentName) { | ||
if (props[propName] == null) { | ||
return new Error(`Required prop \`${ propName }\` was not specified in \`${ componentName }\`.`); | ||
return new Error('Required prop `' + propName + '` was not specified in `' + componentName + '`.'); | ||
} | ||
@@ -34,3 +34,3 @@ | ||
if (!isValidResource(props[propName])) { | ||
return new Error(`Invalid prop \`${ propName }\` supplied to \`${ componentName }\`, expected a \`redux-supermodel\` resource.`); | ||
return new Error('Invalid prop `' + propName + '` supplied to `' + componentName + '`, expected a `redux-supermodel` resource.'); | ||
} | ||
@@ -37,0 +37,0 @@ |
@@ -23,8 +23,10 @@ 'use strict'; | ||
const initialState = {}; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var initialState = {}; | ||
function reducer() { | ||
let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; | ||
let _ref = arguments[1]; | ||
let type = _ref.type, | ||
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; | ||
var _ref = arguments[1]; | ||
var type = _ref.type, | ||
payload = _ref.payload, | ||
@@ -40,3 +42,3 @@ meta = _ref.meta; | ||
if (!meta || !meta.resourceName) { | ||
throw new Error(`Invalid ${ type }: Missing "meta.resourceName" property`); | ||
throw new Error('Invalid ' + type + ': Missing "meta.resourceName" property'); | ||
} | ||
@@ -48,3 +50,3 @@ | ||
if (payload === undefined) { | ||
const newState = _extends({}, state); | ||
var newState = _extends({}, state); | ||
delete newState[meta.resourceName]; | ||
@@ -54,4 +56,4 @@ return newState; | ||
const resource = { initialized: true, busy: false, payload: payload, previous: null, meta: meta }; | ||
return _extends({}, state, { [meta.resourceName]: resource }); | ||
var resource = { initialized: true, busy: false, payload: payload, previous: null, meta: meta }; | ||
return _extends({}, state, _defineProperty({}, meta.resourceName, resource)); | ||
} | ||
@@ -61,5 +63,5 @@ | ||
{ | ||
const previous = state[meta.resourceName] && state[meta.resourceName].payload; | ||
const transform = meta.definition.transform || _defaultTransform2.default; | ||
const resource = { | ||
var previous = state[meta.resourceName] && state[meta.resourceName].payload; | ||
var transform = meta.definition.transform || _defaultTransform2.default; | ||
var _resource = { | ||
initialized: true, | ||
@@ -72,3 +74,3 @@ busy: true, | ||
return _extends({}, state, { [meta.resourceName]: resource }); | ||
return _extends({}, state, _defineProperty({}, meta.resourceName, _resource)); | ||
} | ||
@@ -78,8 +80,8 @@ | ||
{ | ||
const previousState = state[meta.resourceName] && state[meta.resourceName]; | ||
const transform = meta.definition.transform || _defaultTransform2.default; | ||
const resource = { | ||
var previousState = state[meta.resourceName] && state[meta.resourceName]; | ||
var _transform = meta.definition.transform || _defaultTransform2.default; | ||
var _resource2 = { | ||
initialized: true, | ||
busy: false, | ||
payload: transform(payload, previousState.payload, true, meta), | ||
payload: _transform(payload, previousState.payload, true, meta), | ||
previous: null, | ||
@@ -89,3 +91,3 @@ meta: meta | ||
return _extends({}, state, { [meta.resourceName]: resource }); | ||
return _extends({}, state, _defineProperty({}, meta.resourceName, _resource2)); | ||
} | ||
@@ -95,7 +97,7 @@ | ||
{ | ||
const previousState = state[meta.resourceName] && state[meta.resourceName]; | ||
const resource = { | ||
var _previousState = state[meta.resourceName] && state[meta.resourceName]; | ||
var _resource3 = { | ||
initialized: true, | ||
busy: false, | ||
payload: previousState.payload, | ||
payload: _previousState.payload, | ||
previous: null, | ||
@@ -106,8 +108,8 @@ error: payload, | ||
return _extends({}, state, { [meta.resourceName]: resource }); | ||
return _extends({}, state, _defineProperty({}, meta.resourceName, _resource3)); | ||
} | ||
default: | ||
throw new Error(`Unrecognized action type: ${ type }`); | ||
throw new Error('Unrecognized action type: ' + type); | ||
} | ||
} |
{ | ||
"name": "redux-supermodel", | ||
"version": "0.10.1", | ||
"version": "0.11.0", | ||
"description": "A package of action creator functions and reducers that deal with the state management of REST-like APIs for you... all you need is a URL!", | ||
@@ -10,3 +10,3 @@ "repository": "https://github.com/MrLeebo/redux-supermodel", | ||
"prepublish": "npm run build", | ||
"test": "nyc --require babel-core/register mocha -u bdd-lazy-var/rspec --recursive --reporter dot --compilers jsx:babel-core/register --require ./test/setup.js test", | ||
"test": "nyc --require babel-core/register --require babel-polyfill mocha -u bdd-lazy-var/rspec --recursive --reporter dot --compilers jsx:babel-core/register --require ./test/setup.js test", | ||
"lint": "eslint lib test || true" | ||
@@ -29,5 +29,7 @@ }, | ||
"babel-plugin-transform-object-rest-spread": "^6.20.2", | ||
"babel-polyfill": "^6.20.0", | ||
"babel-preset-es2015-node5": "^1.2.0", | ||
"babel-plugin-transform-regenerator": "^6.22.0", | ||
"babel-polyfill": "^6.23.0", | ||
"babel-preset-es2015": "^6.22.0", | ||
"babel-preset-react": "^6.16.0", | ||
"babel-preset-stage-0": "^6.22.0", | ||
"babel-register": "^6.18.0", | ||
@@ -34,0 +36,0 @@ "bdd-lazy-var": "^1.2.0", |
Sorry, the diff of this file is not supported yet
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
27487
411
31