Skip to content

Commit a09c1c5

Browse files
committed
fix(types): correct the type of actions returned by next model
1 parent 1913871 commit a09c1c5

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-model",
3-
"version": "2.7.0",
3+
"version": "2.7.1",
44
"description": "The State management library for React",
55
"main": "./dist/react-model.js",
66
"umd:main": "./dist/react-model.umd.js",

src/global.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const State = {}
22
const Actions = {}
33
const AsyncState = {}
4+
const Middlewares = {}
45
// Communicate between Provider-Consumer and Hooks
56
const Setter: Setter = {
67
// classSetter stores the setState from Provider
@@ -21,6 +22,7 @@ let uid = Math.random() // The unique id of hooks
2122
export default {
2223
Actions,
2324
AsyncState,
25+
Middlewares,
2426
Setter,
2527
State,
2628
devTools,

src/index.d.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

104107
type Context<S = {}> = (InnerContext<S>) & {
105108
next: Function
109+
modelMiddlewares?: Middleware[]
106110
}
107111

108112
type 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
}

src/index.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ function Model<M extends Models, MT extends NextModelType>(
3131
Global.uid += 1
3232
const hash = '__' + Global.uid
3333
Global.State[hash] = models.state
34+
if (models.middlewares) {
35+
Global.Middlewares[hash] = models.middlewares
36+
}
3437
const nextActions: Actions = Object.entries(models.actions).reduce(
3538
(o: { [name: string]: Action }, [name, action]) => {
3639
o[name] = async (state, actions, params) => {
@@ -63,6 +66,9 @@ function Model<M extends Models, MT extends NextModelType>(
6366
}
6467
Object.entries(models).forEach(([name, model]) => {
6568
if (!isAPI(model)) {
69+
console.warn(
70+
'we recommend you to use NextModel now, document link: https://github.com/byte-fe/react-model#model'
71+
)
6672
if (!Global.State[name]) {
6773
Global.State[name] = model.state
6874
}
@@ -156,7 +162,11 @@ const getActions = (
156162
...baseContext,
157163
Global
158164
}
159-
await applyMiddlewares(actionMiddlewares, context)
165+
if (Global.Middlewares[modelName]) {
166+
await applyMiddlewares(Global.Middlewares[modelName], context)
167+
} else {
168+
await applyMiddlewares(actionMiddlewares, context)
169+
}
160170
})
161171
)
162172
return updaters

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
"sourceMap": false,
1616
"strict": true
1717
},
18-
"include": ["src/**/*"],
18+
"include": ["./src/*"],
1919
"exclude": ["node_modules"]
2020
}

0 commit comments

Comments
 (0)