Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response handler #171

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const defaults = {
ok: res => res.ok,
fetch
};

export default defaults;
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import RSAA from './RSAA';
import { isRSAA, validateRSAA, isValidRSAA } from './validation';
import { InvalidRSAA, InternalError, RequestError, ApiError } from './errors';
import { getJSON } from './util';
import { apiMiddleware } from './middleware';
import { apiMiddleware, createMiddleware } from './middleware';

export {
// Alias RSAA to CALL_API to smooth v1 - v2 migration
Expand All @@ -48,5 +48,6 @@ export {
RequestError,
ApiError,
getJSON,
createMiddleware,
apiMiddleware
};
30 changes: 19 additions & 11 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import RSAA from './RSAA';
import { isRSAA, validateRSAA } from './validation';
import { InvalidRSAA, RequestError } from './errors';
import { normalizeTypeDescriptors, actionWith } from './util';
import defaults from './defaults';

/**
* A Redux middleware that processes RSAA actions.
*
* @type {ReduxMiddleware}
* @access public
*/
function apiMiddleware({ getState }) {
return next => action => {
function createMiddleware(options = {}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document this and how to use it 📚

Would configuring defaults for your middleware be like:

const apiMiddlewareWithDefaults = createMiddleware({
  fetch: myFetch,
  ok: myOk
})

when configuring your store? What other defaults can you set this way?

And then you can override from individual RSAAs?

{
  [RSAA]: {
    ...
    ok: myOtherOk
    ...
  }
}

const middlewareOptions = Object.assign({}, defaults, options);

return ({ getState }) => next => action => {
// Do not process actions without an [RSAA] property
if (!isRSAA(action)) {
return next(action);
Expand Down Expand Up @@ -42,7 +39,8 @@ function apiMiddleware({ getState }) {
body,
headers,
options = {},
fetch: doFetch = fetch
fetch: doFetch = middlewareOptions.fetch,
ok = middlewareOptions.ok
} = callAPI;
const { method, credentials, bailout, types } = callAPI;
const [requestType, successType, failureType] = normalizeTypeDescriptors(
Expand Down Expand Up @@ -176,7 +174,7 @@ function apiMiddleware({ getState }) {
}

// Process the server response
if (res.ok) {
if (ok(res)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets handle errors thrown from the ok function in a try/catch here. Should they be handled like those from doFetch above?

return next(await actionWith(successType, [action, getState(), res]));
} else {
return next(
Expand All @@ -193,4 +191,14 @@ function apiMiddleware({ getState }) {
};
}

export { apiMiddleware };
/**
* A Redux middleware that processes RSAA actions.
*
* @type {ReduxMiddleware}
* @access public
*/
function apiMiddleware({ getState }) {
return createMiddleware()({ getState });
}

export { createMiddleware, apiMiddleware };