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

Allowing types to be passed as an object #72

Closed
joshwcomeau opened this issue Apr 27, 2016 · 3 comments
Closed

Allowing types to be passed as an object #72

joshwcomeau opened this issue Apr 27, 2016 · 3 comments

Comments

@joshwcomeau
Copy link

joshwcomeau commented Apr 27, 2016

Just started using redux-api-middleware; really well-thought-out!

A part of the API I don't love is the necessity for types to be a 3-value array. It isn't immediately clear that the order is what distinguishes them, as opposed to a specific naming convention. I'd much rather be able to pass an object, like:

{
  type: 'CATS',
  [CALL_API]: {
    endpoint: 'http://www.example.com/api/users',
    method: 'GET',
    types: {
      request: 'CATS_REQUEST',
      success: 'CATS_RESPONSE',
      failure: 'CATS_ERROR'
    }
  }
}

Would be happy to take a shot at a PR if this is deemed to be a worthwhile addition :)

@peterpme
Copy link

Hey Josh!

If you're building your own api wrapper see #65

You can easily wrap this yourself and never have to look at it again :)

Otherwise, building it into should be straight forward :)

@joshwcomeau
Copy link
Author

joshwcomeau commented Apr 27, 2016

Hi Peter!

That is totally a worthwhile solution, but I'm wondering if it makes sense to be built in for others, like me, for whom it would be intuitive and who don't want to fuss with wrappers.

I'm happy either way. Depends on whether you guys think it's worth the extra bit of logic or not :)

@unrevised6419
Copy link
Collaborator

While this can be achieved on user land, I think there is no need to add this into the library, because features that can be achieved on user land, make the library more harder to maintain, for each user that request a feature. 🙂

import { createAction } from 'redux-api-middleware'

function customRSAA(apiCall) {
	const { types, ...rest } = apiCall
	const { request, success, failure } = types
	return createAction({
		...rest,
		types: [request, success, failure]
	})
}

const action = customRSAA({
  endpoint: 'http://www.example.com/api/users',
  method: 'GET',
  types: {
    request: 'CATS_REQUEST',
    success: 'CATS_RESPONSE',
    failure: 'CATS_ERROR'
  }
})

store.dispatch(action)

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

3 participants