Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

Commit

Permalink
Released v2.0.0-alpha.3 with a basic endpoint reducer (does not suppo…
Browse files Browse the repository at this point in the history
…rt simultaneous requests atm), ref #2
  • Loading branch information
DanielleHuisman committed Jan 13, 2017
1 parent 7f5050a commit 77ad9cd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "redux-cached-api",
"description": "Redux middleware for calling and caching a (REST) API",
"version": "2.0.0-alpha.2",
"version": "2.0.0-alpha.3",
"license": "MIT",
"author": "Vereniging Campus Kabel <info@vck.tv> (https://vck.tv)",
"contributors": [
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './actions';
export * from './api';
export * from './errors';
export * from './middleware';
export * from './reducer';
export * from './request';
export * from './types';
export * from './util';
Expand Down
66 changes: 66 additions & 0 deletions src/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {Map} from 'immutable';

import {InvalidConfigError} from './errors';
import {API_SIGNATURE} from './types';

/**
* Create a Redux reducer for an API.
*
* @param {object} api API configuration
* @return {function} The created Redux reducer
*/
export const createApiReducer = (api) => {
// Validate API configuration
if (typeof api !== 'object') {
throw new InvalidConfigError(`Invalid API configuration: ${api}`);
}

// Gather relevant information
const {
// entityReducer: options,
mergedTypes: {
request: requestTypes,
success: successTypes,
failure: failureTypes
}
} = api;

const initialState = new Map();

// Return the reducer
return (state = initialState, action) => {
// Check if this is one of our actions (quicker than looping through 3 arrays)
if (action.signature === API_SIGNATURE) {
if (action.isEntity) {
// Succes type
// if (successTypes.indexOf(action.type) !== -1) {
//
// }
return state;
}

// Endpoint action
if (requestTypes.indexOf(action.type) !== -1) {
return state
.setIn(['endpoints', action.endpoint, 'loading'], true)
.setIn(['endpoints', action.endpoint, 'finished'], false)
.setIn(['endpoints', action.endpoint, 'data'], null)
.setIn(['endpoints', action.endpoint, 'error'], null);
} else if (successTypes.indexOf(action.type) !== -1) {
return state
.setIn(['endpoints', action.endpoint, 'loading'], false)
.setIn(['endpoints', action.endpoint, 'finished'], true)
.setIn(['endpoints', action.endpoint, 'data'], action.payload)
.setIn(['endpoints', action.endpoint, 'error'], action.payloadError);
} else if (failureTypes.indexOf(action.type) !== -1) {
return state
.setIn(['endpoints', action.endpoint, 'loading'], false)
.setIn(['endpoints', action.endpoint, 'finished'], true)
.setIn(['endpoints', action.endpoint, 'data'], action.payload)
.setIn(['endpoints', action.endpoint, 'error'], action.hasPayloadError ? action.payloadError : action.error);
}
}

return state;
};
};

0 comments on commit 77ad9cd

Please sign in to comment.