@@ -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,15 @@ declare global {
97
99
: never
98
100
: never
99
101
102
+ // Extract share props from global middleware or MiddlewareSchema
103
+ type ExtractShareContent < M > = M extends keyof Sirutils . WizardMiddlewares
104
+ ? Sirutils . WizardMiddlewares [ M ] extends Sirutils . Wizard . MiddlewareSchema < infer S , BlobType >
105
+ ? S
106
+ : never
107
+ : M extends Sirutils . Wizard . MiddlewareSchema < infer S , BlobType >
108
+ ? S
109
+ : never
110
+
100
111
interface ServiceApi {
101
112
service : <
102
113
const T extends string ,
@@ -136,7 +147,7 @@ declare global {
136
147
} ,
137
148
]
138
149
139
- interface ActionContext < B , P , Q > {
150
+ interface ActionContext < B , P , Q , S > {
140
151
logger : Moleculer . LoggerInstance
141
152
body : Simplify <
142
153
( B extends Sirutils . Schema . ValidationSchema < BlobType >
@@ -154,23 +165,39 @@ declare global {
154
165
res ?: ServerResponse
155
166
streams ?: Sirutils . Wizard . StreamData [ ]
156
167
raw ?: MoleculerContext
168
+ share ?: Partial < Pick < ContextShare , S extends keyof ContextShare ? S : never > >
169
+ }
170
+ // Only difference between ActionContext and MiddlewareContext is
171
+ // MiddlewareContext's share property is not optional
172
+ interface MiddlewareContext < B , P , Q , S > extends Omit < ActionContext < B , P , Q , S > , "share" > {
173
+ share : Pick < ContextShare , S extends keyof ContextShare ? S : never >
157
174
}
158
175
176
+ interface ContextShare { }
177
+
159
178
interface ActionSchema < B , P , Q , R > extends MoleculerActionSchema { }
179
+ interface MiddlewareSchema < S extends keyof ContextShare , R > {
180
+ logger : unknown ,
181
+ share : S [ ] ,
182
+ handler : Sirutils . Wizard . MiddlewareHandler < S , R >
183
+ }
160
184
161
185
type ActionList = Record <
162
186
string ,
163
187
Sirutils . Wizard . ActionSchema < BlobType , BlobType , BlobType , BlobType >
164
188
>
165
189
166
- type ActionHandler < B , P , Q , R > = ( ctx : Sirutils . Wizard . ActionContext < B , P , Q > ) => R
190
+ type ActionHandler < B , P , Q , S , R > = ( ctx : Sirutils . Wizard . ActionContext < B , P , Q , S > ) => R
191
+ type MiddlewareHandler < S , R > = ( ctx : Sirutils . Wizard . MiddlewareContext < BlobType , BlobType , BlobType , S > , next : unknown ) => 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 keyof Sirutils . WizardMiddlewares
200
+ | Sirutils . Wizard . MiddlewareSchema < keyof Sirutils . Wizard . ContextShare , Hr > = never ,
174
201
> (
175
202
meta : {
176
203
body ?: B
@@ -179,9 +206,13 @@ declare global {
179
206
rest ?: true | string | string [ ]
180
207
cache ?: boolean | CacherOptions
181
208
stream ?: boolean
209
+ middlewares ?: M [ ]
182
210
multipart ?: formidable . Options | boolean
183
211
} ,
184
- handler : Sirutils . Wizard . ActionHandler < NoInfer < B > , NoInfer < P > , NoInfer < Q > , Hr >
212
+ handler : Sirutils . Wizard . ActionHandler <
213
+ NoInfer < B > , NoInfer < P > , NoInfer < Q > , ExtractShareContent < M > ,
214
+ Hr
215
+ >
185
216
) => (
186
217
serviceOptions : Sirutils . Wizard . ServiceOptions < BlobType , BlobType , BlobType > ,
187
218
actionName : string
@@ -199,9 +230,26 @@ declare global {
199
230
>
200
231
}
201
232
233
+ interface MiddlewareApi {
234
+ createMiddleware : <
235
+ Hr ,
236
+ const S extends keyof ContextShare = never ,
237
+ > (
238
+ meta : {
239
+ name ?: keyof WizardMiddlewares
240
+ share ?: S [ ]
241
+ } ,
242
+ handler : Sirutils . Wizard . MiddlewareHandler < S , Hr >
243
+ ) => Sirutils . Wizard . MiddlewareSchema < S , Hr >
244
+ processMiddlewares : (
245
+ ctx : Sirutils . Wizard . ActionContext < BlobType , BlobType , BlobType , BlobType > ,
246
+ middlewares : ( keyof WizardMiddlewares | Sirutils . Wizard . MiddlewareSchema < keyof ContextShare , BlobType > ) [ ]
247
+ ) => Promise < { continue : true } | { continue : false , returnedData : BlobType } >
248
+ }
249
+
202
250
type Context = Sirutils . PluginSystem . Context <
203
251
Sirutils . Wizard . Options ,
204
- Sirutils . Wizard . BaseApi & Sirutils . Wizard . ServiceApi & Sirutils . Wizard . ActionApi
252
+ Sirutils . Wizard . BaseApi & Sirutils . Wizard . ServiceApi & Sirutils . Wizard . ActionApi & Sirutils . Wizard . MiddlewareApi
205
253
>
206
254
}
207
255
}
0 commit comments