agraboso/redux-api-middleware

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

开放

#208 创建于 2018年7月28日

 (4 条评论) (2 个反应) (0 位负责人)JavaScript (192 个派生)batch import
enhancementhelp wanted

仓库指标

星标
 (1,471 个星标)
PR 合并指标
 (30 天内没有已合并 PR)

描述

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.

贡献者指南