Last active
November 22, 2019 10:47
-
-
Save Dominent/1049e5e6a848ad9eb9cc07d87bb27cc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IPayload<T> { | |
payload: T; | |
} | |
class Payload { | |
public static empty: IPayload<{}> = { payload: null } as IPayload<{}>; | |
} | |
interface IHttpTypedAction<R, S, F> { | |
request: ActionCreator<string, (props: IPayload<R>) => IPayload<R> & TypedAction<string>>; | |
success: ActionCreator<string, (props: IPayload<S>) => IPayload<S> & TypedAction<string>>; | |
failure: ActionCreator<string, (props: IPayload<F>) => IPayload<F> & TypedAction<string>>; | |
} | |
function createHttpAction<R = {}, S = {}, F = {}>( | |
type: string | |
): IHttpTypedAction<R, S, F> { | |
return { | |
request: createAction<string, IPayload<R>>(`REQUEST_${type}`, props<IPayload<R>>()), | |
success: createAction<string, IPayload<S>>(`SUCCESS_${type}`, props<IPayload<S>>()), | |
failure: createAction<string, IPayload<F>>(`FAILURE_${type}`, props<IPayload<F>>()) | |
}; | |
} | |
export const fetchSomethingAction = createHttpAction<number, IType>('FETCH_SOMETHING'); | |
fetchSomethingAction.request({ payload: 1232 }); | |
fetchSomethingAction.success({ payload: {} as ISOMETHING }); | |
fetchSomethingAction.failure(Payload.empty); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment