1
+ import { EndpointParameterInstructions } from "@smithy/middleware-endpoint" ;
1
2
import { constructStack } from "@smithy/middleware-stack" ;
2
- import { Command as ICommand , Handler , MetadataBearer , MiddlewareStack as IMiddlewareStack } from "@smithy/types" ;
3
+ import type { HttpRequest } from "@smithy/protocol-http" ;
4
+ import type {
5
+ Command as ICommand ,
6
+ FinalizeHandlerArguments ,
7
+ Handler ,
8
+ HandlerExecutionContext ,
9
+ HttpRequest as IHttpRequest ,
10
+ HttpResponse as IHttpResponse ,
11
+ Logger ,
12
+ MetadataBearer ,
13
+ MiddlewareStack as IMiddlewareStack ,
14
+ Pluggable ,
15
+ RequestHandler ,
16
+ SerdeContext ,
17
+ } from "@smithy/types" ;
18
+ import { SMITHY_CONTEXT_KEY } from "@smithy/types" ;
3
19
4
20
/**
5
21
* @public
@@ -11,11 +27,256 @@ export abstract class Command<
11
27
ClientInput extends object = any ,
12
28
ClientOutput extends MetadataBearer = any
13
29
> implements ICommand < ClientInput , Input , ClientOutput , Output , ResolvedClientConfiguration > {
14
- abstract input : Input ;
15
- readonly middlewareStack : IMiddlewareStack < Input , Output > = constructStack < Input , Output > ( ) ;
30
+ public abstract input : Input ;
31
+ public readonly middlewareStack : IMiddlewareStack < Input , Output > = constructStack < Input , Output > ( ) ;
32
+
33
+ /**
34
+ * Factory for Command ClassBuilder.
35
+ * @internal
36
+ */
37
+ public static classBuilder <
38
+ I extends SI ,
39
+ O extends SO ,
40
+ C extends { logger : Logger ; requestHandler : RequestHandler < any , any , any > } ,
41
+ SI extends object = any ,
42
+ SO extends MetadataBearer = any
43
+ > ( ) {
44
+ return new ClassBuilder < I , O , C , SI , SO > ( ) ;
45
+ }
46
+
16
47
abstract resolveMiddleware (
17
48
stack : IMiddlewareStack < ClientInput , ClientOutput > ,
18
49
configuration : ResolvedClientConfiguration ,
19
50
options : any
20
51
) : Handler < Input , Output > ;
52
+
53
+ /**
54
+ * @internal
55
+ */
56
+ public resolveMiddlewareWithContext (
57
+ clientStack : IMiddlewareStack < any , any > ,
58
+ configuration : { logger : Logger ; requestHandler : RequestHandler < any , any , any > } ,
59
+ options : any ,
60
+ {
61
+ middlewareFn,
62
+ clientName,
63
+ commandName,
64
+ inputFilterSensitiveLog,
65
+ outputFilterSensitiveLog,
66
+ smithyContext,
67
+ additionalContext,
68
+ CommandCtor,
69
+ } : ResolveMiddlewareContextArgs
70
+ ) {
71
+ for ( const mw of middlewareFn . bind ( this ) ( CommandCtor , clientStack , configuration , options ) ) {
72
+ this . middlewareStack . use ( mw ) ;
73
+ }
74
+ const stack = clientStack . concat ( this . middlewareStack ) ;
75
+ const { logger } = configuration ;
76
+ const handlerExecutionContext : HandlerExecutionContext = {
77
+ logger,
78
+ clientName,
79
+ commandName,
80
+ inputFilterSensitiveLog,
81
+ outputFilterSensitiveLog,
82
+ [ SMITHY_CONTEXT_KEY ] : {
83
+ ...smithyContext ,
84
+ } ,
85
+ ...additionalContext ,
86
+ } ;
87
+ const { requestHandler } = configuration ;
88
+ return stack . resolve (
89
+ ( request : FinalizeHandlerArguments < any > ) => requestHandler . handle ( request . request as HttpRequest , options || { } ) ,
90
+ handlerExecutionContext
91
+ ) ;
92
+ }
93
+ }
94
+
95
+ /**
96
+ * @internal
97
+ */
98
+ type ResolveMiddlewareContextArgs = {
99
+ middlewareFn : ( CommandCtor : any , clientStack : any , config : any , options : any ) => Pluggable < any , any > [ ] ;
100
+ clientName : string ;
101
+ commandName : string ;
102
+ smithyContext : Record < string , unknown > ;
103
+ additionalContext : HandlerExecutionContext ;
104
+ inputFilterSensitiveLog : ( _ : any ) => any ;
105
+ outputFilterSensitiveLog : ( _ : any ) => any ;
106
+ CommandCtor : any /* Command constructor */ ;
107
+ } ;
108
+
109
+ /**
110
+ * @internal
111
+ */
112
+ class ClassBuilder <
113
+ I extends SI ,
114
+ O extends SO ,
115
+ C extends { logger : Logger ; requestHandler : RequestHandler < any , any , any > } ,
116
+ SI extends object = any ,
117
+ SO extends MetadataBearer = any
118
+ > {
119
+ private _init : ( _ : Command < I , O , C , SI , SO > ) => void = ( ) => { } ;
120
+ private _ep : EndpointParameterInstructions = { } ;
121
+ private _middlewareFn : ( CommandCtor : any , clientStack : any , config : any , options : any ) => Pluggable < any , any > [ ] = ( ) => [ ] ;
122
+ private _commandName = "" ;
123
+ private _clientName = "" ;
124
+ private _additionalContext = { } as HandlerExecutionContext ;
125
+ private _smithyContext = { } as Record < string , unknown > ;
126
+ private _inputFilterSensitiveLog = ( _ : any ) => _ ;
127
+ private _outputFilterSensitiveLog = ( _ : any ) => _ ;
128
+ private _serializer : ( input : I , context : SerdeContext | any ) => Promise < IHttpRequest > = null as any ;
129
+ private _deserializer : ( output : IHttpResponse , context : SerdeContext | any ) => Promise < O > = null as any ;
130
+ /**
131
+ * Optional init callback.
132
+ */
133
+ public init ( cb : ( _ : Command < I , O , C , SI , SO > ) => void ) {
134
+ this . _init = cb ;
135
+ }
136
+ /**
137
+ * Set the endpoint parameter instructions.
138
+ */
139
+ public ep ( endpointParameterInstructions : EndpointParameterInstructions ) : ClassBuilder < I , O , C , SI , SO > {
140
+ this . _ep = endpointParameterInstructions ;
141
+ return this ;
142
+ }
143
+ /**
144
+ * Add any number of middleware.
145
+ */
146
+ public m (
147
+ middlewareSupplier : ( CommandCtor : any , clientStack : any , config : any , options : any ) => Pluggable < any , any > [ ]
148
+ ) : ClassBuilder < I , O , C , SI , SO > {
149
+ this . _middlewareFn = middlewareSupplier ;
150
+ return this ;
151
+ }
152
+ /**
153
+ * Set the initial handler execution context Smithy field.
154
+ */
155
+ public s (
156
+ service : string ,
157
+ operation : string ,
158
+ smithyContext : Record < string , unknown > = { }
159
+ ) : ClassBuilder < I , O , C , SI , SO > {
160
+ this . _smithyContext = {
161
+ service,
162
+ operation,
163
+ ...smithyContext ,
164
+ } ;
165
+ return this ;
166
+ }
167
+ /**
168
+ * Set the initial handler execution context.
169
+ */
170
+ public c ( additionalContext : HandlerExecutionContext = { } ) : ClassBuilder < I , O , C , SI , SO > {
171
+ this . _additionalContext = additionalContext ;
172
+ return this ;
173
+ }
174
+ /**
175
+ * Set constant string identifiers for the operation.
176
+ */
177
+ public n ( clientName : string , commandName : string ) : ClassBuilder < I , O , C , SI , SO > {
178
+ this . _clientName = clientName ;
179
+ this . _commandName = commandName ;
180
+ return this ;
181
+ }
182
+ /**
183
+ * Set the input and output sensistive log filters.
184
+ */
185
+ public f (
186
+ inputFilter : ( _ : any ) => any = ( _ ) => _ ,
187
+ outputFilter : ( _ : any ) => any = ( _ ) => _
188
+ ) : ClassBuilder < I , O , C , SI , SO > {
189
+ this . _inputFilterSensitiveLog = inputFilter ;
190
+ this . _outputFilterSensitiveLog = outputFilter ;
191
+ return this ;
192
+ }
193
+ /**
194
+ * Sets the serializer.
195
+ */
196
+ public ser (
197
+ serializer : ( input : I , context ?: SerdeContext | any ) => Promise < IHttpRequest >
198
+ ) : ClassBuilder < I , O , C , SI , SO > {
199
+ this . _serializer = serializer ;
200
+ return this ;
201
+ }
202
+ /**
203
+ * Sets the deserializer.
204
+ */
205
+ public de (
206
+ deserializer : ( output : IHttpResponse , context ?: SerdeContext | any ) => Promise < O >
207
+ ) : ClassBuilder < I , O , C , SI , SO > {
208
+ this . _deserializer = deserializer ;
209
+ return this ;
210
+ }
211
+ /**
212
+ * @returns a Command class with the classBuilder properties.
213
+ */
214
+ public build ( ) : {
215
+ new ( input : I ) : CommandImpl < I , O , C , SI , SO > ;
216
+ getEndpointParameterInstructions ( ) : EndpointParameterInstructions ;
217
+ } {
218
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
219
+ const closure = this ;
220
+ let CommandRef : any ;
221
+
222
+ return ( CommandRef = class extends Command < I , O , C , SI , SO > {
223
+ /**
224
+ * @public
225
+ */
226
+ public static getEndpointParameterInstructions ( ) : EndpointParameterInstructions {
227
+ return closure . _ep ;
228
+ }
229
+
230
+ /**
231
+ * @public
232
+ */
233
+ public constructor ( readonly input : I ) {
234
+ super ( ) ;
235
+ closure . _init ( this ) ;
236
+ }
237
+
238
+ /**
239
+ * @internal
240
+ */
241
+ public resolveMiddleware ( stack : IMiddlewareStack < any , any > , configuration : C , options : any ) : Handler < any , any > {
242
+ return this . resolveMiddlewareWithContext ( stack , configuration , options , {
243
+ CommandCtor : CommandRef ,
244
+ middlewareFn : closure . _middlewareFn ,
245
+ clientName : closure . _clientName ,
246
+ commandName : closure . _commandName ,
247
+ inputFilterSensitiveLog : closure . _inputFilterSensitiveLog ,
248
+ outputFilterSensitiveLog : closure . _outputFilterSensitiveLog ,
249
+ smithyContext : closure . _smithyContext ,
250
+ additionalContext : closure . _additionalContext ,
251
+ } ) ;
252
+ }
253
+
254
+ /**
255
+ * @internal
256
+ */
257
+ // @ts -ignore used in middlewareFn closure.
258
+ public serialize = closure . _serializer ;
259
+
260
+ /**
261
+ * @internal
262
+ */
263
+ // @ts -ignore used in middlewareFn closure.
264
+ public deserialize = closure . _deserializer ;
265
+ } ) ;
266
+ }
267
+ }
268
+
269
+ /**
270
+ * A concrete implementation of ICommand with no abstract members.
271
+ * @public
272
+ */
273
+ export interface CommandImpl <
274
+ I extends SI ,
275
+ O extends SO ,
276
+ C extends { logger : Logger ; requestHandler : RequestHandler < any , any , any > } ,
277
+ SI extends object = any ,
278
+ SO extends MetadataBearer = any
279
+ > extends Command < I , O , C , SI , SO > {
280
+ readonly input : I ;
281
+ resolveMiddleware ( stack : IMiddlewareStack < SI , SO > , configuration : C , options : any ) : Handler < I , O > ;
21
282
}
0 commit comments