@@ -26,6 +26,7 @@ declare global {
26
26
}
27
27
28
28
interface WizardServices { }
29
+ interface WizardMiddlewares { }
29
30
30
31
namespace Wizard {
31
32
interface Options {
@@ -51,6 +52,7 @@ declare global {
51
52
interface BaseApi {
52
53
broker : ServiceBroker
53
54
gateway : MoleculerService
55
+ middleware : MoleculerService
54
56
}
55
57
56
58
interface ServiceOptions <
@@ -67,7 +69,7 @@ declare global {
67
69
68
70
actions ?: R
69
71
70
- created ?: < B , P , Q > ( ctx : Sirutils . Wizard . ActionContext < B , P , Q > ) => BlobType
72
+ created ?: < B , P , Q , S > ( ctx : Sirutils . Wizard . ActionContext < B , P , Q , S > ) => BlobType
71
73
}
72
74
73
75
type ActionNames = {
@@ -97,6 +99,14 @@ declare global {
97
99
: never
98
100
: never
99
101
102
+ type ExtractShareContent < M > = M extends keyof Sirutils . WizardMiddlewares
103
+ ? Sirutils . WizardMiddlewares [ M ] extends Sirutils . Wizard . MiddlewareSchema < infer S , BlobType >
104
+ ? S
105
+ : never
106
+ : M extends Sirutils . Wizard . MiddlewareSchema < infer S , BlobType >
107
+ ? S
108
+ : never
109
+
100
110
interface ServiceApi {
101
111
service : <
102
112
const T extends string ,
@@ -136,7 +146,7 @@ declare global {
136
146
} ,
137
147
]
138
148
139
- interface ActionContext < B , P , Q > {
149
+ interface ActionContext < B , P , Q , S > {
140
150
logger : Moleculer . LoggerInstance
141
151
body : Simplify <
142
152
( B extends Sirutils . Schema . ValidationSchema < BlobType >
@@ -154,23 +164,41 @@ declare global {
154
164
res ?: ServerResponse
155
165
streams ?: Sirutils . Wizard . StreamData [ ]
156
166
raw ?: MoleculerContext
167
+ share ?: Partial < Pick < ContextShare , S extends keyof ContextShare ? S : never > >
157
168
}
169
+ interface MiddlewareContext < B , P , Q , S > extends Omit < ActionContext < B , P , Q , S > , 'share' > {
170
+ share : Pick < ContextShare , S extends keyof ContextShare ? S : never >
171
+ }
172
+
173
+ interface ContextShare { }
158
174
159
175
interface ActionSchema < B , P , Q , R > extends MoleculerActionSchema { }
176
+ interface MiddlewareSchema < S extends keyof ContextShare , R > {
177
+ logger : unknown
178
+ share : S [ ]
179
+ handler : Sirutils . Wizard . MiddlewareHandler < S , R >
180
+ }
160
181
161
182
type ActionList = Record <
162
183
string ,
163
184
Sirutils . Wizard . ActionSchema < BlobType , BlobType , BlobType , BlobType >
164
185
>
165
186
166
- type ActionHandler < B , P , Q , R > = ( ctx : Sirutils . Wizard . ActionContext < B , P , Q > ) => R
187
+ type ActionHandler < B , P , Q , S , R > = ( ctx : Sirutils . Wizard . ActionContext < B , P , Q , S > ) => R
188
+ type MiddlewareHandler < S , R > = (
189
+ ctx : Sirutils . Wizard . MiddlewareContext < BlobType , BlobType , BlobType , S > ,
190
+ next : unknown
191
+ ) => R
167
192
168
193
interface ActionApi {
169
194
createAction : <
170
195
const B extends Sirutils . Schema . ValidationSchema < BlobType > ,
171
196
const P extends Sirutils . Schema . ValidationSchema < BlobType > ,
172
197
const Q extends Sirutils . Schema . ValidationSchema < BlobType > ,
173
198
Hr ,
199
+ const M extends
200
+ | keyof Sirutils . WizardMiddlewares
201
+ | Sirutils . Wizard . MiddlewareSchema < keyof Sirutils . Wizard . ContextShare , Hr > = never ,
174
202
> (
175
203
meta : {
176
204
body ?: B
@@ -179,9 +207,16 @@ declare global {
179
207
rest ?: true | string | string [ ]
180
208
cache ?: boolean | CacherOptions
181
209
stream ?: boolean
210
+ middlewares ?: M [ ]
182
211
multipart ?: formidable . Options | boolean
183
212
} ,
184
- handler : Sirutils . Wizard . ActionHandler < NoInfer < B > , NoInfer < P > , NoInfer < Q > , Hr >
213
+ handler : Sirutils . Wizard . ActionHandler <
214
+ NoInfer < B > ,
215
+ NoInfer < P > ,
216
+ NoInfer < Q > ,
217
+ ExtractShareContent < M > ,
218
+ Hr
219
+ >
185
220
) => (
186
221
serviceOptions : Sirutils . Wizard . ServiceOptions < BlobType , BlobType , BlobType > ,
187
222
actionName : string
@@ -199,9 +234,29 @@ declare global {
199
234
>
200
235
}
201
236
237
+ interface MiddlewareApi {
238
+ createMiddleware : < Hr , const S extends keyof ContextShare = never > (
239
+ meta : {
240
+ name ?: keyof WizardMiddlewares
241
+ share ?: S [ ]
242
+ } ,
243
+ handler : Sirutils . Wizard . MiddlewareHandler < S , Hr >
244
+ ) => Sirutils . Wizard . MiddlewareSchema < S , Hr >
245
+ processMiddlewares : (
246
+ ctx : Sirutils . Wizard . ActionContext < BlobType , BlobType , BlobType , BlobType > ,
247
+ middlewares : (
248
+ | keyof WizardMiddlewares
249
+ | Sirutils . Wizard . MiddlewareSchema < keyof ContextShare , BlobType >
250
+ ) [ ]
251
+ ) => Promise < { contiune : true } | { contiune : false ; returnedData : BlobType } >
252
+ }
253
+
202
254
type Context = Sirutils . PluginSystem . Context <
203
255
Sirutils . Wizard . Options ,
204
- Sirutils . Wizard . BaseApi & Sirutils . Wizard . ServiceApi & Sirutils . Wizard . ActionApi
256
+ Sirutils . Wizard . BaseApi &
257
+ Sirutils . Wizard . ServiceApi &
258
+ Sirutils . Wizard . ActionApi &
259
+ Sirutils . Wizard . MiddlewareApi
205
260
>
206
261
}
207
262
}
0 commit comments