@@ -3,21 +3,27 @@ import {
3
3
Handler ,
4
4
HandlerArguments ,
5
5
FinalizeHandlerArguments ,
6
+ Middleware ,
7
+ HandlerExecutionContext ,
8
+ FinalizeMiddleware ,
9
+ FinalizeHandler
6
10
} from "@aws-sdk/types" ;
7
11
8
12
type input = Array < string > ;
9
13
type output = object ;
10
14
11
- function concatMiddleware (
12
- message : string ,
13
- next : Handler < input , output >
14
- ) : Handler < input , output > {
15
- return ( args : HandlerArguments < input > ) : Promise < output > => next ( {
16
- ...args ,
17
- input : args . input . concat ( message ) ,
18
- } ) ;
15
+ //return tagged union to make compiler happy
16
+ function getConcatMiddleware ( message : string ) : Middleware < input , output > | FinalizeMiddleware < input , output > {
17
+ return (
18
+ next : Handler < input , output > ,
19
+ ) : Handler < input , output > => {
20
+ return ( args : HandlerArguments < input > ) : Promise < output > => next ( {
21
+ ...args ,
22
+ input : args . input . concat ( message ) ,
23
+ } ) ;
24
+ }
25
+
19
26
}
20
-
21
27
function shuffle < T > ( arr : Array < T > ) : Array < T > {
22
28
arr = [ ...arr ] ;
23
29
for ( let i = arr . length ; i > 0 ; i -- ) {
@@ -33,22 +39,22 @@ describe('MiddlewareStack', () => {
33
39
const stack = new MiddlewareStack < input , output > ( ) ;
34
40
35
41
const middleware = shuffle ( [
36
- [ concatMiddleware . bind ( null , 'second' ) ] ,
37
- [ concatMiddleware . bind ( null , 'first' ) , { priority : 10 } ] ,
38
- [ concatMiddleware . bind ( null , 'fourth' ) , { step : 'build' } ] ,
42
+ [ getConcatMiddleware ( 'second' ) , { } ] ,
43
+ [ getConcatMiddleware ( 'first' ) , { priority : 10 } ] ,
44
+ [ getConcatMiddleware ( 'fourth' ) , { step : 'build' } ] ,
39
45
[
40
- concatMiddleware . bind ( null , 'third' ) ,
46
+ getConcatMiddleware ( 'third' ) ,
41
47
{ step : 'build' , priority : 1 }
42
48
] ,
43
- [ concatMiddleware . bind ( null , 'fifth' ) , { step : 'finalize' } ] ,
49
+ [ getConcatMiddleware ( 'fifth' ) , { step : 'finalize' } ] ,
44
50
[
45
- concatMiddleware . bind ( null , 'sixth' ) ,
51
+ getConcatMiddleware ( 'sixth' ) ,
46
52
{ step : 'finalize' , priority : - 1 }
47
53
] ,
48
54
] ) ;
49
55
50
56
for ( const [ mw , options ] of middleware ) {
51
- stack . add ( mw , options ) ;
57
+ stack . add ( mw as any , options ) ;
52
58
}
53
59
54
60
const inner = jest . fn ( ( { input} : FinalizeHandlerArguments < input > ) => {
@@ -71,8 +77,11 @@ describe('MiddlewareStack', () => {
71
77
72
78
it ( 'should allow cloning' , async ( ) => {
73
79
const stack = new MiddlewareStack < input , output > ( ) ;
74
- stack . add ( concatMiddleware . bind ( null , 'second' ) ) ;
75
- stack . add ( concatMiddleware . bind ( null , 'first' ) , { priority : 100 } ) ;
80
+ stack . add ( getConcatMiddleware ( 'second' ) as Middleware < input , output > ) ;
81
+ stack . add (
82
+ getConcatMiddleware ( 'first' ) as Middleware < input , output > ,
83
+ { priority : 100 }
84
+ ) ;
76
85
77
86
const secondStack = stack . clone ( ) ;
78
87
@@ -89,13 +98,19 @@ describe('MiddlewareStack', () => {
89
98
90
99
it ( 'should allow combining stacks' , async ( ) => {
91
100
const stack = new MiddlewareStack < input , output > ( ) ;
92
- stack . add ( concatMiddleware . bind ( null , 'second' ) ) ;
93
- stack . add ( concatMiddleware . bind ( null , 'first' ) , { priority : 100 } ) ;
101
+ stack . add ( getConcatMiddleware ( 'second' ) as Middleware < input , output > ) ;
102
+ stack . add (
103
+ getConcatMiddleware ( 'first' ) as Middleware < input , output > ,
104
+ { priority : 100 }
105
+ ) ;
94
106
95
107
const secondStack = new MiddlewareStack < input , output > ( ) ;
96
- secondStack . add ( concatMiddleware . bind ( null , 'fourth' ) , { step : 'build' } ) ;
97
108
secondStack . add (
98
- concatMiddleware . bind ( null , 'third' ) ,
109
+ getConcatMiddleware ( 'fourth' ) as FinalizeMiddleware < input , output > ,
110
+ { step : 'build' }
111
+ ) ;
112
+ secondStack . add (
113
+ getConcatMiddleware ( 'third' ) as FinalizeMiddleware < input , output > ,
99
114
{ step : 'build' , priority : 100 }
100
115
) ;
101
116
@@ -114,10 +129,10 @@ describe('MiddlewareStack', () => {
114
129
} ) ;
115
130
116
131
it ( 'should allow the removal of middleware by constructor identity' , async ( ) => {
117
- const MyMiddleware = concatMiddleware . bind ( null , 'remove me!' ) ;
132
+ const MyMiddleware = getConcatMiddleware ( 'remove me!' ) as Middleware < input , output > ;
118
133
const stack = new MiddlewareStack < input , output > ( ) ;
119
134
stack . add ( MyMiddleware ) ;
120
- stack . add ( concatMiddleware . bind ( null , "don't remove me" ) ) ;
135
+ stack . add ( getConcatMiddleware ( "don't remove me" ) as Middleware < input , output > ) ;
121
136
122
137
await stack . resolve (
123
138
( { input} : FinalizeHandlerArguments < Array < string > > ) => {
@@ -144,11 +159,11 @@ describe('MiddlewareStack', () => {
144
159
it ( 'should allow the removal of middleware by tag' , async ( ) => {
145
160
const stack = new MiddlewareStack < input , output > ( ) ;
146
161
stack . add (
147
- concatMiddleware . bind ( null , 'not removed' ) ,
162
+ getConcatMiddleware ( 'not removed' ) as Middleware < input , output > ,
148
163
{ tags : { foo : true , bar : true } }
149
164
) ;
150
165
stack . add (
151
- concatMiddleware . bind ( null , 'remove me!' ) ,
166
+ getConcatMiddleware ( 'remove me!' ) as Middleware < input , output > ,
152
167
{ tags : { foo : true , bar : true , baz : true } }
153
168
) ;
154
169
0 commit comments