Open davesag opened 3 years ago
Hi @davesag I don't quite understand your request. Can you show a code example?
Do you mean you cannot detect a RSAA action? You can use isRSAA
to check if an action is RSAA.
Once you have checked to see if an action isRSAA
and then you want to actually extract the types from it (or in my case I needed the meta
info from the flux action) you need a way to check action[RSAA].types
- if the RSAA
constant is not exposed then you need to define it yourself, which of course raises the risk that, should you change that constant in future, then the code will break. If you don't deprecate the constant then I can safely use it.
I need to do this because I have a payload function that wants to know what the meta data passed to the flux action that previously executed was.
I've written this:
import { isRSAA, RSAA } from 'redux-api-middleware'
const toAction = type => (typeof type === 'string' ? { type } : type)
const fluxAction = (rsaaAction, suffix) => {
if (!isRSAA(rsaaAction)) return rsaaAction
const actions = rsaaAction[RSAA].types.map(toAction)
if (suffix) {
const regex = new RegExp(suffix + '$', 'i')
return actions.find(({ type }) => type.match(regex) !== null)
}
return actions[0]
}
export default fluxAction
using in a payload function like
import { fluxAction } from 'utils'
import { getContentLikesWithId } from '../../selectors'
const successPayload = async (action, state) => {
const {
meta: { id }
} = fluxAction(action, 'SUCCESS')
const { count = 0 } = getContentLikesWithId(id)(state)
return {
count: count + 1,
currentUser: true
}
}
export default successPayload
and also in a mock API Middleware function I use in my storybook stories
import { action as clickAction } from '@storybook/addon-actions'
import { isRSAA } from 'redux-api-middleware'
import { fluxAction } from 'utils'
const mockApiMiddleware =
({ dispatch }) =>
next =>
async rsaaAction => {
if (!isRSAA(rsaaAction)) return next(rsaaAction)
const action = fluxAction(rsaaAction)
dispatch(action)
clickAction('dispatched')(action)
}
export default mockApiMiddleware
Well mostly that action will not be deprecated, we just wanted to make it internal. And if that will happen it will be in a major release. And I think we will expose a new function getAction
🤔
So what you need is a reverse of createAction
. You can create your own function getAction
, in case of a major release to be easier to migrate
import { isRSAA, RSAA } from 'redux-api-middleware'
const getAction = (action) => isRSAA(action) ? action[RSAA] : action
Exposing a getAction
function as you propose is a good idea. I’d use that instead of the RSAA
constant if it was avail.
To assist in migration exposing getAction
would ideally happen before RSAA
was un-exposed :-)
In https://github.com/agraboso/redux-api-middleware/blob/master/src/RSAA.js#L6 you have deprecated the RSAA constant, however it's essential to be able to access that in the case where a
payload
function accepts thersaa action
and you need to deconstruct it to get (in my case) access to themeta
field supplied to the action.Please consider not deprecating this.