agraboso/redux-api-middleware

Request descriptor's payload/meta functions not passed the parsed action

Open

#208 opened on Jul 28, 2018

View on GitHub
 (4 comments) (2 reactions) (0 assignees)JavaScript (1,487 stars) (202 forks)batch import
enhancementhelp wanted

Description

It looks like the request type descriptors' payload and meta functions are being passed the raw, top-level RSAA as the action argument, instead of an action with parsed values.

For instance, if we take the example from the README:

// Input RSAA
{
  [RSAA]: {
    endpoint: 'http://www.example.com/api/users',
    method: 'GET',
    types: [
      {
        type: 'REQUEST',
        payload: (action, state) => ({ endpoint: action.endpoint })
      },
      'SUCCESS',
      'FAILURE'
    ]
  }
}

The documentation implies that the above RSAA action should result in the following FSA request action being dispatched:

// expected…
{
  type: 'REQUEST',
  payload: { endpoint: 'http://www.example.com/api/users' },
}

Instead, the dispatched FSA request action has an empty object as it's payload:

// actual…
{
  type: 'REQUEST',
  payload: {  },
}

Cause

This is because the payload function is being passed the unparsed, top-level RSAA action here:

// We can now dispatch the request FSA
if (
  typeof requestType.payload === 'function' ||
  typeof requestType.meta === 'function'
) {
  //          top-level [RSAA] action ⌄⌄⌄⌄⌄⌄
  next(await actionWith(requestType, [action, getState()]));
} else {
  next(requestType);
}

Based on the docs, I was expecting the following:

// We can now dispatch the request FSA
if (
  typeof requestType.payload === 'function' ||
  typeof requestType.meta === 'function'
) {
  const parsedAction = {
    ...action[RSAA],
    method,
    body,
    headers,
  };
  next(await actionWith(requestType, [parsedAction, getState()]));
} else {
  next(requestType);
}

This behavior appears in both the master and next branches.

I'd be happy to submit a PR, if this is indeed a bug.

Contributor guide