-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
Extend createAction API to include additional action properties #116
Comments
One note I have discovered when I was rewriting all the tests to
|
@piotrwitek OK cool. I also see that the overload solution is arguably more readable (readability is one reason I created a bunch of extra types for the conditional solution for One question: I've been looking at both I may be confused by the difference between Sneak preview: export function action<T extends StringType, E>(
type: T,
payload: undefined,
meta: undefined,
error: E
): { type: T; error: E };
export function action<T extends StringType, P, E>( // this one may not work
type: T,
payload: P,
meta: undefined,
error: E
): { type: T; payload: P; error: E };
export function action<T extends StringType, M, E>( // this one may not work
type: T,
payload: undefined,
meta: M,
error: E
): { type: T; meta: M; error: E };
export function action<T extends StringType, P, M, E>(
type: T,
payload: P,
meta: M,
error: E
): { type: T; payload: P; meta: M; error: E }; Obviously, this only adds error and not a generic container type. I wonder why that would be reserved for Could you clarify with a little more detail what you're looking for? For example, require support for |
Hey @sjsyrek, I have a feeling that we should go with the fourth param as error property which would be compliant with FSA standard, and I'll update the tutorial to state that it's FSA standard compatible. The reason for that is that now we have I'd agree to go with what you suggested in the sneak preview. 👍 |
@piotrwitek I spent some time today working on this. Results so far: https://github.com/sjsyrek/typesafe-actions/tree/add-error-property-to-action I want to make sure I'm conforming to the spirit of this project, however. And also to the best representation of the FSA spec. It's a little unclear what to do about the relationship between Also, you might have a look at the tests. Everything passes, but that doesn't mean I wrote them correctly. I'm struggling a bit with the overload implementation of I tried something like this, but it's not working: export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(
type: T,
createHandler: (
actionCallback: <P, M>(
payload: P,
meta: M
) => { type: T; payload: P; meta: M }
) => AC
): AC;
export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(
type: T,
createHandler: (
actionCallback: <P>(
payload: P,
) => { type: T; payload: P }
) => AC
): AC;
export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(
type: T,
createHandler: (
actionCallback: <M>(
meta: M,
) => { type: T; meta: M }
) => AC
): AC {
// validation is already done in action function
const actionCreator: AC =
createHandler == null
? ((() => action(type)) as AC)
: createHandler(action.bind(null, type) as Parameters<
typeof createHandler
>[0]);
return Object.assign(actionCreator, {
getType: () => type,
// redux-actions compatibility
toString: () => type,
});
} |
@sjsyrek thanks for the update 👍 Don't worry about the overloads, for now. Let's finish the first part and we can take a look at overloads later. I'll probably have to take a look at it myself and see what the compiler is telling me. Please run |
@piotrwitek Shall we close this one? |
Only when #119 is merged and documentation is updated. |
Thank you for your contribution! 👍 Please makes sure that these checkboxes are checked before submitting your PR, thank you! Related issues: #116 * [x] PR have a corresponding issue which is approved and it's linked above * [x] Rebase before creating a PR to keep commit history clean * [x] Clear node_modules and reinstall all the dependencies: `npm run reinstall` * [x] Run ci-check npm script: `npm run ci-check` For bugfixes: * [x] Add at least one unit test to cover the bug that have been fixed For new feature: * [ ] Update API docs and tutorial * [ ] Add examples to demonstrate new feature * [x] Add type unit tests * [x] Add runtime unit tests
@sjsyrek inspiration for API documentation update example: https://github.com/piotrwitek/typesafe-actions/releases/tag/v3.2.0 |
@piotrwitek For import { StringType, ActionCreator } from './type-helpers';
import { action } from './action';
export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(type: T): AC;
export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(
type: T,
// tslint:disable-next-line: unified-signatures
createHandler: (
actionCallback: <P>(payload: P) => { type: T; payload: P }
) => AC
): AC;
export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(
type: T,
// tslint:disable-next-line: unified-signatures
createHandler: (actionCallback: <M>(meta: M) => { type: T; meta: M }) => AC
): AC;
export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(
type: T,
// tslint:disable-next-line: unified-signatures
createHandler: (actionCallback: <E>(error: E) => { type: T; meta: E }) => AC
): AC;
export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(
type: T,
// tslint:disable-next-line: unified-signatures
createHandler: (
actionCallback: <P, M>(
payload: P,
meta: M
) => { type: T; payload: P; meta: M }
) => AC
): AC;
export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(
type: T,
// tslint:disable-next-line: unified-signatures
createHandler: (
actionCallback: <P, E>(
payload: P,
error: E
) => { type: T; payload: P; error: E }
) => AC
): AC;
export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(
type: T,
// tslint:disable-next-line: unified-signatures
createHandler: (
actionCallback: <M, E>(type: T, meta: M) => { type: T; meta: M; error: E }
) => AC
): AC;
export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(
type: T,
// tslint:disable-next-line: unified-signatures
createHandler: (
actionCallback: <P, M, E>(
type: T,
payload: P,
meta: M
) => { type: T; payload: P; meta: M; error: E }
) => AC
): AC;
/**
* @description typesafe action-creator factory
*/
export function createAction<
T extends StringType,
AC extends ActionCreator<T> = () => { type: T }
>(
type: T,
createHandler?: (
actionCallback: <P = undefined, M = undefined, E = undefined>(
payload?: P,
meta?: M,
error?: E
) => any
) => AC
): AC {
// validation is already done in action function
const actionCreator: AC =
createHandler == null
? ((() => action(type)) as AC)
: createHandler(action.bind(null, type) as Parameters<
typeof createHandler
>[0]);
return Object.assign(actionCreator, {
getType: () => type,
// redux-actions compatibility
toString: () => type,
});
} I haven't updated the tests yet, however, and two of them aren't passing. |
Also noticed this in the redux-actions repo:
Is this something you think worth emulating? |
@piotrwitek I have some time in the next week or so to work on this if you have comments! |
@sjsyrek thanks for the heads-up, let me try to look at it tomorrow |
@piotrwitek ping :-) |
Sorry I was preparing for my vacations, I'll pick it up in 2,5 weeks when I'm back |
@piotrwitek Any update? |
Hey @sjsyrek, thanks for the reminder, I'll try to pick this up this week, I have a lot on my plate now 💻 |
@piotrwitek No worries, me too. I just don't want to forget about this project. I like the library and want to help improve it if I can be useful. |
I'm blocking this issue for now as I'm working on new |
Closing because it is superseded by #207 |
I would like to combine the simplicity of
createAction
with the flexibility ofcreateStandardAction
by extending the API of the former to include additional action properties. For example, it is a common use case to include anerror
property in Flux standard actions, but the current implementation ofcreateAction
does not support this. In order to support additional properties beyond just one (or justerror
), we can make the third parameter to the returned function an object with any number of properties that can be spread over the action.Here is an example of what this might look like:
Initially discussed in #108.
The text was updated successfully, but these errors were encountered: