agraboso/redux-api-middleware

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

Open

#208 aperta il 28 lug 2018

Vedi su GitHub
 (4 commenti) (2 reazioni) (0 assegnatari)JavaScript (202 fork)batch import
enhancementhelp wanted

Metriche repository

Star
 (1487 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

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.

Guida contributor