1- export type Action = {
2- type :'INCREMENT_COUNTER' ,
3- delta :number ,
4- } | {
5- type :'RESET_COUNTER' ,
6- }
1+ type Q < T > = { request :T }
2+ type S < T > = { response :T }
3+ type E = { error :Error }
4+
5+ type QEmpty = Q < null >
6+ type QValue = Q < { value :number } >
7+
8+ export type Action =
9+ // UI actions
10+ { type :'INCREMENT_COUNTER' , delta :number }
11+ | { type :'RESET_COUNTER' }
12+
13+ // API Requests
14+ | ( { type :'SAVE_COUNT_REQUEST' } & QValue )
15+ | ( { type :'SAVE_COUNT_SUCCESS' } & QValue & S < { } > )
16+ | ( { type :'SAVE_COUNT_ERROR' } & QValue & E )
17+
18+ | ( { type :'LOAD_COUNT_REQUEST' } & QEmpty )
19+ | ( { type :'LOAD_COUNT_SUCCESS' } & QEmpty & S < { value :number } > )
20+ | ( { type :'LOAD_COUNT_ERROR' } & QEmpty & E )
721
822export const incrementCounter = ( delta :number ) :Action => ( {
923type :'INCREMENT_COUNTER' ,
@@ -13,3 +27,27 @@ export const incrementCounter = (delta: number): Action => ({
1327export const resetCounter = ( ) :Action => ( {
1428type :'RESET_COUNTER' ,
1529} )
30+
31+ export type ApiActionGroup < _Q , _S > = {
32+ request :( q ?:_Q ) => Action & Q < _Q >
33+ success :( s :_S , q ?:_Q ) => Action & Q < _Q > & S < _S >
34+ error :( e :Error , q ?:_Q ) => Action & Q < _Q > & E
35+ }
36+
37+ export const saveCount :ApiActionGroup < { value :number } , { } > = {
38+ request :( request ) =>
39+ ( { type :'SAVE_COUNT_REQUEST' , request} ) ,
40+ success :( response , request ) =>
41+ ( { type :'SAVE_COUNT_SUCCESS' , request, response} ) ,
42+ error :( error , request ) =>
43+ ( { type :'SAVE_COUNT_ERROR' , request, error} ) ,
44+ }
45+
46+ export const loadCount :ApiActionGroup < null , { value :number } > = {
47+ request :( request ) =>
48+ ( { type :'LOAD_COUNT_REQUEST' , request :null } ) ,
49+ success :( response , request ) =>
50+ ( { type :'LOAD_COUNT_SUCCESS' , request :null , response} ) ,
51+ error :( error , request ) =>
52+ ( { type :'LOAD_COUNT_ERROR' , request :null , error} ) ,
53+ }