Skip to content

Commit

Permalink
🌟 Add configure method for entire package
Browse files Browse the repository at this point in the history
  • Loading branch information
greena13 committed Jul 10, 2018
1 parent 90a9804 commit 80978db
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/action-creators/buildActionCreators.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import arrayFrom from '../utils/array/arrayFrom';
import generateUrl from './helpers/generateUrl';
import getItemKey from './helpers/getItemKey';
import getCollectionKey from './helpers/getCollectionKey';
import { getConfiguration } from '../configuration';

function fetchCollection(options, params, actionCreatorOptions = { }) {
const {
Expand Down Expand Up @@ -284,6 +285,8 @@ const STANDARD_ACTION_CREATORS = {
function buildActionCreators(resourceOptions, actions, actionsOptions) {
const { name } = resourceOptions;

const configuration = getConfiguration();

return Object.keys(actionsOptions).reduce((memo, key) => {
const actionName = actions.get(key);

Expand All @@ -306,10 +309,11 @@ function buildActionCreators(resourceOptions, actions, actionsOptions) {
{
keyBy: 'id'
},
configuration,
resourceOptions,
actionOptions,
[
'url', 'keyBy', 'resourceType', 'urlOnlyParams', 'responseAdaptor', 'progress', 'requestErrorHandler'
'url', 'keyBy', 'resourceType', 'urlOnlyParams', 'responseAdaptor', 'progress', 'requestErrorHandler', 'request'
]
);

Expand Down
1 change: 0 additions & 1 deletion src/action-creators/helpers/makeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ function makeRequest(options, actionCreatorOptions = {}) {
const { status } = response;

if (status < 400) {

return response.json().then((json) => {
const _json = function () {
if (responseAdaptor) {
Expand Down
12 changes: 12 additions & 0 deletions src/configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let configuration = {};

export function setConfiguration(customConfiguration) {
configuration = {
...configuration,
customConfiguration
};
}

export function getConfiguration() {
return { ...configuration };
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as serializeKey } from './utils/serializeKey';
export { setConfiguration as configure, getConfiguration } from './configuration';

export { RESOURCE, RESOURCES, COLLECTION, ITEM } from './constants/DataStructures';
export { NEW, EDITING, FETCHING, CREATING, UPDATING, DESTROYING, DESTROY_ERROR, SUCCESS, PROGRESS, ERROR } from './constants/Statuses';
Expand Down
25 changes: 17 additions & 8 deletions src/reducers/buildReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import addAssociationReducer from './helpers/addAsssociationReducer';
import removeItemsFromResources from './helpers/removeItemsFromResources';
import resolveOptions from '../action-creators/helpers/resolveOptions';
import progressReducer from './helpers/progressReducer';
import { getConfiguration } from '../configuration';

function setCollection(resources, { status, items, key, httpCode, collection, error }) {
const currentList = resources.collections[key] || COLLECTION;
Expand Down Expand Up @@ -566,19 +567,27 @@ function buildReducers(resourceOptions, actionsOptions, options) {
* Build the map of actions that should effect the current resource
*/

const configuration = getConfiguration();

const reducersDict = Object.keys(options).reduce((memo, key) => {
const actionOptions = options[key];
const reducer = (isObject(actionOptions) && actionOptions.reducer) || STANDARD_REDUCERS[key];

if (reducer) {
const reducerOptions = resolveOptions({
beforeReducers: [],
afterReducers: [],
}, resourceOptions, actionOptions, [
'progress',
'beforeReducers',
'afterReducers',
]);
const reducerOptions = resolveOptions(
{
beforeReducers: [],
afterReducers: [],
},
configuration,
resourceOptions,
actionOptions,
[
'progress',
'beforeReducers',
'afterReducers',
]
);

if (reducerOptions.progress && (!STANDARD_REDUCERS[key] || PROGRESS_COMPATIBLE_ACTIONS[key])) {
reducerOptions.beforeReducers = [
Expand Down

0 comments on commit 80978db

Please sign in to comment.