Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please don't deprecate the RSAA constant #267

Open
davesag opened this issue Nov 2, 2021 · 5 comments
Open

Please don't deprecate the RSAA constant #267

davesag opened this issue Nov 2, 2021 · 5 comments

Comments

@davesag
Copy link

davesag commented Nov 2, 2021

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 the rsaa action and you need to deconstruct it to get (in my case) access to the meta field supplied to the action.

Please consider not deprecating this.

@iamandrewluca
Copy link
Collaborator

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.

@davesag
Copy link
Author

davesag commented Nov 2, 2021

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

@davesag davesag changed the title Don't deprecate the RSAA constant Please don't deprecate the RSAA constant Nov 2, 2021
@iamandrewluca
Copy link
Collaborator

iamandrewluca commented Nov 2, 2021

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

@davesag
Copy link
Author

davesag commented Nov 2, 2021

Exposing a getAction function as you propose is a good idea. I’d use that instead of the RSAA constant if it was avail.

@davesag
Copy link
Author

davesag commented Nov 2, 2021

To assist in migration exposing getAction would ideally happen before RSAA was un-exposed :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants