redux-resource
Version:
Resource management for Redux.
90 lines (65 loc) • 6 kB
JavaScript
exports.__esModule = true;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports.default = resourceReducer;
var _updateResourcesPlugin = require('./update-resources-plugin');
var _updateResourcesPlugin2 = _interopRequireDefault(_updateResourcesPlugin);
var _requestStatusesPlugin = require('./request-statuses-plugin');
var _requestStatusesPlugin2 = _interopRequireDefault(_requestStatusesPlugin);
var _generateDefaultInitialState = require('./utils/generate-default-initial-state');
var _generateDefaultInitialState2 = _interopRequireDefault(_generateDefaultInitialState);
var _composeReducers = require('./utils/compose-reducers');
var _composeReducers2 = _interopRequireDefault(_composeReducers);
var _warning = require('./utils/warning');
var _warning2 = _interopRequireDefault(_warning);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Create a resource reducer.
//
// `resourceType`: the kind of resource that this slice represents. For instance, "books".
// `options`: pass options to change the behavior of the reducer. See the docs
// for more information on the available options.
function resourceReducer(resourceType, options = {}) {
const { plugins = [], initialState = {} } = options;
const defaultInitialState = (0, _generateDefaultInitialState2.default)();
const initial = _extends({}, defaultInitialState, initialState, {
resourceType
});
if (process.env.NODE_ENV !== 'production') {
if (typeof resourceType !== 'string') {
(0, _warning2.default)(`The value of "resourceType" that you passed to resourceReducer was ` + `not a string. The resource name must be a string. You should check ` + `your reducer configuration. ` + `For more information, refer to the documentation at: ` + `https://redux-resource.js.org/docs/requests/request-actions.html`, 'INVALID_RESOURCE_TYPE_PASSED');
}
}
const allPlugins = plugins.concat(_requestStatusesPlugin2.default, _updateResourcesPlugin2.default);
const computedPlugins = allPlugins.map(plugin => {
const result = plugin(resourceType, options);
if (process.env.NODE_ENV !== 'production') {
if (typeof result !== 'function') {
(0, _warning2.default)(`A plugin was initialized that did not return a function. Plugins ` + `should return a function with the same signature as a reducer. ` + `For more information, refer to the documentation on plugins: ` + `https://redux-resource.js.org/docs/other-guides/plugins.html`, 'BAD_PLUGIN_INITIALIZED');
}
}
return result;
});
return function reducer(state = initial, action) {
if (process.env.NODE_ENV !== 'production') {
if (action.type === 'REQUEST_PENDING' || action.type === 'REQUEST_IDLE' || action.type === 'REQUEST_FAILED' || action.type === 'REQUEST_SUCCEEDED') {
(0, _warning2.default)(`You dispatched an action with type ${action.type}. This is a reserved ` + `action type that will be used in a future version of Redux Resource. ` + `We recommend that you use a different type to avoid conflict. ` + `For more information, refer to the documentation at: ` + `https://redux-resource.js.org/docs/api-reference/action-types.html#reserved-action-types`, 'RESERVED_ACTION_TYPE_USED');
}
if (action.resourceName && typeof action.resourceName === 'string') {
(0, _warning2.default)(`You dispatched an action of type ${action.type} with a "resourceName" property. This property has been deprecated in ` + `favor of a new property, "resourceType." This new property serves ` + `the same function; it has simply been renamed. The old property ` + `will continue to work until the next major release of Redux Resource (v4). ` + `Please update your action creators. For more information, refer to ` + `the request action documentation at: ` + `https://redux-resource.js.org/docs/requests/request-actions.html\n\n` + `Also, the migration guide to Redux Resource v3 can be found at: ` + `https://github.com/jamesplease/redux-resource/blob/master/packages/redux-resource/docs/migration-guides/2-to-3.md`, 'DEPRECATED_RESOURCE_NAME_SPECIFIED');
}
if (action.request && typeof action.request !== 'string') {
(0, _warning2.default)(`An invalid request property was included in an action with type ` + `"${action.type}". The request property must be a string. ` + `For more information, refer to the documentation at: ` + `https://redux-resource.js.org/docs/requests/request-actions.html`, 'INVALID_REQUEST_NAME_PASSED');
}
if (action.requestKey && typeof action.requestKey !== 'string') {
(0, _warning2.default)(`An invalid requestKey property was included in an action with type ` + `"${action.type}". The requestKey property must be a string. ` + `For more information, refer to the documentation at: ` + `https://redux-resource.js.org/docs/requests/request-keys.html`, 'INVALID_REQUEST_KEY_PASSED');
}
if (action.requestName && typeof action.requestName !== 'string') {
(0, _warning2.default)(`An invalid requestName property was included in an action with type ` + `"${action.type}". The requestName property must be a string. ` + `For more information, refer to the documentation at: ` + `https://redux-resource.js.org/docs/requests/request-names.html`, 'INVALID_REQUEST_NAME_PASSED');
}
if (action.list && typeof action.list !== 'string') {
(0, _warning2.default)(`An invalid list was included in an action with type ` + `"${action.type}". Lists must be strings.` + `For more information, refer to the documentation at: ` + `https://redux-resource.js.org/docs/resources/lists.html`, 'INVALID_LIST_NAME_PASSED');
}
}
return (0, _composeReducers2.default)(computedPlugins)(state, action);
};
}
;