Skip to content

Instantly share code, notes, and snippets.

@yoo2001818
Created September 17, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoo2001818/05aad13f1af7fb5eaf38 to your computer and use it in GitHub Desktop.
Save yoo2001818/05aad13f1af7fb5eaf38 to your computer and use it in GitHub Desktop.
export const GET = 'GET';
export const POST = 'POST';
export const DELETE = 'DELETE';
export const PUT = 'PUT';
export function api(type, endpoint, options) {
return {
apiRequest: true,
type,
endpoint,
options
};
}
export const apiMiddleware = client => store => next => action => {
if (action == null) return next(action);
if (action.payload == null) return next(action);
if (!action.payload.apiRequest) return next(action);
// Client function should return a Promise
const { type, endpoint, options } = action.payload;
let promise = client(type, endpoint, options);
if (promise == null) {
// Dispatch an Error!
return store.dispatch(Object.assign({}, action, {
error: true,
payload: new Error('Client did not return a Promise object')
}));
}
return store.dispatch(Object.assign({}, action, {
payload: promise
}));
};
export default apiMiddleware;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment