@@ -24,6 +24,9 @@ interface Global {
2424 AsyncState : {
2525 [ modelName : string ] : undefined | ( ( context ?: any ) => Promise < Partial < any > > )
2626 }
27+ Middlewares : {
28+ [ modelName : string ] : Middleware [ ]
29+ }
2730 subscriptions : Subscriptions
2831 Setter : Setter
2932 devTools : any
@@ -48,12 +51,12 @@ type Action<S = {}, P = any, ActionKeys = {}> = (
4851 | Promise < void >
4952
5053// v3.0 Action
51- type NextAction < S = { } , P = any , ActionKeys = { } > = (
54+ type NextAction < S = { } , P = any , ActionKeys = { } , ExtContext = { } > = (
5255 params : P ,
5356 context : {
5457 state : S
5558 actions : getConsumerNextActionsType < NextActions < S , ActionKeys > >
56- }
59+ } & ExtContext
5760) =>
5861 | Partial < S >
5962 | Promise < Partial < S > >
@@ -103,6 +106,7 @@ interface InnerContext<S = {}> extends BaseContext<S> {
103106
104107type Context < S = { } > = ( InnerContext < S > ) & {
105108 next : Function
109+ modelMiddlewares ?: Middleware [ ]
106110}
107111
108112type Middleware < S = { } > = ( C : Context < S > , M : Middleware < S > [ ] ) => void
@@ -150,7 +154,9 @@ interface APIs<M extends Models> {
150154 modelName : K
151155 ) => M [ K ] extends ModelType
152156 ? Readonly < getConsumerActionsType < Get < M [ K ] , 'actions' > > >
153- : undefined
157+ : M [ K ] extends API
158+ ? M [ K ] [ 'actions' ]
159+ : unknown
154160 getInitialState : < T extends any > (
155161 context ?: T | undefined
156162 ) => Promise < {
@@ -166,7 +172,11 @@ interface APIs<M extends Models> {
166172 actionName : keyof Get < M [ K ] , 'actions' > | Array < keyof Get < M [ K ] , 'actions' > >
167173 ) => void
168174 actions : {
169- [ K in keyof M ] : Readonly < getConsumerActionsType < Get < M [ K ] , 'actions' > > >
175+ [ K in keyof M ] : M [ K ] extends ModelType
176+ ? Readonly < getConsumerActionsType < Get < M [ K ] , 'actions' > > >
177+ : M [ K ] extends API
178+ ? M [ K ] [ 'actions' ]
179+ : unknown
170180 }
171181}
172182
@@ -179,14 +189,16 @@ type ModelType<InitStateType = any, ActionKeys = any> = {
179189}
180190
181191// v3.0
182- type NextModelType < InitStateType = any , ActionKeys = any > = {
192+ type NextModelType < InitStateType = any , ActionKeys = any , ExtContext = { } > = {
183193 actions : {
184194 [ P in keyof ActionKeys ] : NextAction <
185195 InitStateType ,
186196 ActionKeys [ P ] ,
187- ActionKeys
197+ ActionKeys ,
198+ ExtContext
188199 >
189200 }
201+ middlewares ?: Middleware [ ]
190202 state : InitStateType
191203 asyncState ?: ( context ?: any ) => Promise < Partial < InitStateType > >
192204}
0 commit comments