aikoven / typescript-fsa

Type-safe action creator utilities
MIT License
607 stars 28 forks source link

Customizing the returned payload #44

Closed luisatmaniak closed 7 years ago

luisatmaniak commented 7 years ago

How could I do something like the following in typescript-fsa?

interface AddTodoInputPayload {
   name: string
}

interface AddTodoPayload extends AddTodoInputPayload {
   type: 'ADD_TODO',
   id: string
}
const addTodoAction = (payload: AddTodoInputPayload): AddTodoPayload => ({...payload, type: 'ADD_TODO',  id: uuid.v4()})

I know the type is added automatically and all, but I want to be able to augment/modify the passed payload, is this possible? I'd also like to preserve composability with typescript-fsa-reducers.

aikoven commented 7 years ago

Right now there are a few possibilities:

You could add the UUID at the place where you create that action.

You could also have a middleware that adds it.

Another option is to make another function that calls that action creator:

const addTodo = actionCreator<{name: string, id: string}>('ADD_TODO')

const createAddTodo = (payload: {name: string}) => addTodo({...payload, id: uuid.v4()})

Then use it to create actions, but use original addTodo in reducers.

luisatmaniak commented 7 years ago

Thanks for answering. Yes, that seems like a good solution, it's a bit painful to have to export two actions creators (one for actually creating the action another one for .case(..) in fsa-reducers), but besides that, I think this will work.

Thank again.

aikoven commented 7 years ago

I agree that it's not as beautiful as it could be. I'm open for API proposals if you have one.