Giter Site home page Giter Site logo

Comments (5)

iamandrewluca avatar iamandrewluca commented on June 10, 2024

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.

from redux-api-middleware.

davesag avatar davesag commented on June 10, 2024

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

from redux-api-middleware.

iamandrewluca avatar iamandrewluca commented on June 10, 2024

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

from redux-api-middleware.

davesag avatar davesag commented on June 10, 2024

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

from redux-api-middleware.

davesag avatar davesag commented on June 10, 2024

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

from redux-api-middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.