agraboso / agraboso/redux-api-middleware

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

Open
#208 4 comments 2 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
JavaScript
Stars
1.5k
Forks
190
PR merge metrics
No merged PRs in 30d

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:
```js
// 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:
```js
// 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:
```js
// actual…
{
type: 'REQUEST',
payload: { },
}
```

## Cause
This is because the `payload` function is being passed the unparsed, top-level RSAA action [here](https://github.com/agraboso/redux-api-middleware/blob/master/src/middleware.js#L150):
```js
// 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:
```js
// 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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.