redux-resource-support-unshift-list
Version:
Resource management for Redux.
76 lines (62 loc) • 5.7 kB
JavaScript
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; };
import updateResourcesPlugin from './update-resources-plugin';
import requestStatusesPlugin from './request-statuses-plugin';
import generateDefaultInitialState from './utils/generate-default-initial-state';
import composeReducers from './utils/compose-reducers';
import warning from './utils/warning';
// 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.
export default function resourceReducer(resourceType) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _options$plugins = options.plugins,
plugins = _options$plugins === undefined ? [] : _options$plugins,
_options$initialState = options.initialState,
initialState = _options$initialState === undefined ? {} : _options$initialState;
var defaultInitialState = generateDefaultInitialState();
var initial = _extends({}, defaultInitialState, initialState, {
resourceType: resourceType
});
if (process.env.NODE_ENV !== 'production') {
if (typeof resourceType !== 'string') {
warning('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');
}
}
var allPlugins = plugins.concat(requestStatusesPlugin, updateResourcesPlugin);
var computedPlugins = allPlugins.map(function (plugin) {
var result = plugin(resourceType, options);
if (process.env.NODE_ENV !== 'production') {
if (typeof result !== 'function') {
warning('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() {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initial;
var action = arguments[1];
if (process.env.NODE_ENV !== 'production') {
if (action.type === 'REQUEST_PENDING' || action.type === 'REQUEST_IDLE' || action.type === 'REQUEST_FAILED' || action.type === 'REQUEST_SUCCEEDED') {
warning('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') {
warning('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') {
warning('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') {
warning('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') {
warning('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') {
warning('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 composeReducers(computedPlugins)(state, action);
};
}