@@ -2,10 +2,10 @@ import {Client, Command, MetadataBearer} from '@aws-sdk/types';
2
2
import { match , SinonSpyCall , SinonStub } from 'sinon' ;
3
3
import { mockClient } from './mockClient' ;
4
4
5
- export type AwsClientBehavior < TClient extends Client < any , any , any > > =
6
- TClient extends Client < infer TInput , infer TOutput , any > ? Behavior < TInput , TOutput , TOutput > : never ;
5
+ export type AwsClientBehavior < TClient > =
6
+ TClient extends Client < infer TInput , infer TOutput , infer TConfiguration > ? Behavior < TInput , TOutput , TOutput , TConfiguration > : never ;
7
7
8
- export interface Behavior < TInput extends object , TOutput extends MetadataBearer , TCommandOutput extends TOutput > {
8
+ export interface Behavior < TInput extends object , TOutput extends MetadataBearer , TCommandOutput extends TOutput , TConfiguration > {
9
9
10
10
/**
11
11
* Allows specifying the behavior for any Command with given input (parameters).
@@ -29,7 +29,7 @@ export interface Behavior<TInput extends object, TOutput extends MetadataBearer,
29
29
* @param input Command payload to match
30
30
* @param strict Should the payload match strictly (default false, will match if all defined payload properties match)
31
31
*/
32
- onAnyCommand < TCmdInput extends TInput > ( input ?: Partial < TCmdInput > , strict ?: boolean ) : Behavior < TInput , TOutput , TOutput > ;
32
+ onAnyCommand < TCmdInput extends TInput > ( input ?: Partial < TCmdInput > , strict ?: boolean ) : Behavior < TInput , TOutput , TOutput , TConfiguration > ;
33
33
34
34
/**
35
35
* Allows specifying the behavior for a given Command type and its input (parameters).
@@ -42,14 +42,14 @@ export interface Behavior<TInput extends object, TOutput extends MetadataBearer,
42
42
*/
43
43
on < TCmdInput extends TInput , TCmdOutput extends TOutput > (
44
44
command : new ( input : TCmdInput ) => AwsCommand < TCmdInput , TCmdOutput > , input ?: Partial < TCmdInput > , strict ?: boolean ,
45
- ) : Behavior < TInput , TOutput , TCmdOutput > ;
45
+ ) : Behavior < TInput , TOutput , TCmdOutput , TConfiguration > ;
46
46
47
47
/**
48
48
* Sets a successful response that will be returned from any `Client#send()` invocation.
49
49
*
50
50
* @param response Content to be returned
51
51
*/
52
- resolves ( response : CommandResponse < TCommandOutput > ) : AwsStub < TInput , TOutput > ;
52
+ resolves ( response : CommandResponse < TCommandOutput > ) : AwsStub < TInput , TOutput , TConfiguration > ;
53
53
54
54
/**
55
55
* Sets a successful response that will be returned from one `Client#send()` invocation.
@@ -67,15 +67,15 @@ export interface Behavior<TInput extends object, TOutput extends MetadataBearer,
67
67
*
68
68
* @param response Content to be returned
69
69
*/
70
- resolvesOnce ( response : CommandResponse < TCommandOutput > ) : Behavior < TInput , TOutput , TCommandOutput > ;
70
+ resolvesOnce ( response : CommandResponse < TCommandOutput > ) : Behavior < TInput , TOutput , TCommandOutput , TConfiguration > ;
71
71
72
72
/**
73
73
* Sets a failure response that will be returned from any `Client#send()` invocation.
74
74
* The response will always be an `Error` instance.
75
75
*
76
76
* @param error Error text, Error instance or Error parameters to be returned
77
77
*/
78
- rejects ( error ?: string | Error | AwsError ) : AwsStub < TInput , TOutput > ;
78
+ rejects ( error ?: string | Error | AwsError ) : AwsStub < TInput , TOutput , TConfiguration > ;
79
79
80
80
/**
81
81
* Sets a failure response that will be returned from one `Client#send()` invocation.
@@ -94,14 +94,14 @@ export interface Behavior<TInput extends object, TOutput extends MetadataBearer,
94
94
*
95
95
* @param error Error text, Error instance or Error parameters to be returned
96
96
*/
97
- rejectsOnce ( error ?: string | Error | AwsError ) : Behavior < TInput , TOutput , TCommandOutput > ;
97
+ rejectsOnce ( error ?: string | Error | AwsError ) : Behavior < TInput , TOutput , TCommandOutput , TConfiguration > ;
98
98
99
99
/**
100
100
* Sets a function that will be called on any `Client#send()` invocation.
101
101
*
102
102
* @param fn Function taking Command input and returning result
103
103
*/
104
- callsFake ( fn : ( input : any ) => any ) : AwsStub < TInput , TOutput > ; // TODO Types
104
+ callsFake ( fn : ( input : any , getClient : ( ) => Client < TInput , TOutput , TConfiguration > ) => any ) : AwsStub < TInput , TOutput , TConfiguration > ; // TODO Types
105
105
106
106
/**
107
107
* Sets a function that will be called on any `Client#send()` invocation.
@@ -119,7 +119,7 @@ export interface Behavior<TInput extends object, TOutput extends MetadataBearer,
119
119
*
120
120
* @param fn Function taking Command input and returning result
121
121
*/
122
- callsFakeOnce ( fn : ( input : any ) => any ) : Behavior < TInput , TOutput , TCommandOutput > ; // TODO Types
122
+ callsFakeOnce ( fn : ( input : any , getClient : ( ) => Client < TInput , TOutput , TConfiguration > ) => any ) : Behavior < TInput , TOutput , TCommandOutput , TConfiguration > ; // TODO Types
123
123
124
124
}
125
125
@@ -133,8 +133,8 @@ export interface Behavior<TInput extends object, TOutput extends MetadataBearer,
133
133
* snsMock = mockClient(SNSClient);
134
134
* ```
135
135
*/
136
- export type AwsClientStub < TClient extends Client < any , any , any > > =
137
- TClient extends Client < infer TInput , infer TOutput , any > ? AwsStub < TInput , TOutput > : never ;
136
+ export type AwsClientStub < TClient > =
137
+ TClient extends Client < infer TInput , infer TOutput , infer TConfiguration > ? AwsStub < TInput , TOutput , TConfiguration > : never ;
138
138
139
139
/**
140
140
* Wrapper on the mocked `Client#send()` method,
@@ -144,7 +144,7 @@ export type AwsClientStub<TClient extends Client<any, any, any>> =
144
144
*
145
145
* To define resulting variable type easily, use {@link AwsClientStub}.
146
146
*/
147
- export class AwsStub < TInput extends object , TOutput extends MetadataBearer > implements Behavior < TInput , TOutput , TOutput > {
147
+ export class AwsStub < TInput extends object , TOutput extends MetadataBearer , TConfiguration > implements Behavior < TInput , TOutput , TOutput , TConfiguration > {
148
148
149
149
/**
150
150
* Underlying `Client#send()` method Sinon stub.
@@ -154,7 +154,7 @@ export class AwsStub<TInput extends object, TOutput extends MetadataBearer> impl
154
154
public send : SinonStub < [ AwsCommand < TInput , TOutput > ] , Promise < TOutput > > ;
155
155
156
156
constructor (
157
- private client : Client < TInput , TOutput , any > ,
157
+ private client : Client < TInput , TOutput , TConfiguration > ,
158
158
send : SinonStub < [ AwsCommand < TInput , TOutput > ] , Promise < TOutput > > ,
159
159
) {
160
160
this . send = send ;
@@ -168,7 +168,7 @@ export class AwsStub<TInput extends object, TOutput extends MetadataBearer> impl
168
168
/**
169
169
* Resets stub. It will replace the stub with a new one, with clean history and behavior.
170
170
*/
171
- reset ( ) : AwsStub < TInput , TOutput > {
171
+ reset ( ) : AwsStub < TInput , TOutput , TConfiguration > {
172
172
/* sinon.stub.reset() does not remove the fakes which in some conditions can break subsequent stubs,
173
173
* so instead of calling send.reset(), we recreate the stub.
174
174
* See: https://github.com/sinonjs/sinon/issues/1572
@@ -180,7 +180,7 @@ export class AwsStub<TInput extends object, TOutput extends MetadataBearer> impl
180
180
}
181
181
182
182
/** Resets stub's calls history. */
183
- resetHistory ( ) : AwsStub < TInput , TOutput > {
183
+ resetHistory ( ) : AwsStub < TInput , TOutput , TConfiguration > {
184
184
this . send . resetHistory ( ) ;
185
185
return this ;
186
186
}
@@ -214,7 +214,7 @@ export class AwsStub<TInput extends object, TOutput extends MetadataBearer> impl
214
214
commandCalls < TCmd extends AwsCommand < any , any > ,
215
215
TCmdInput extends TCmd extends AwsCommand < infer TIn , any > ? TIn : never ,
216
216
TCmdOutput extends TCmd extends AwsCommand < any , infer TOut > ? TOut : never ,
217
- > (
217
+ > (
218
218
commandType : new ( input : TCmdInput ) => TCmd ,
219
219
input ?: Partial < TCmdInput > ,
220
220
strict ?: boolean ,
@@ -227,17 +227,17 @@ export class AwsStub<TInput extends object, TOutput extends MetadataBearer> impl
227
227
} ) ;
228
228
}
229
229
230
- onAnyCommand < TCmdInput extends TInput > ( input ?: Partial < TCmdInput > , strict = false ) : CommandBehavior < TInput , TOutput , TOutput > {
230
+ onAnyCommand < TCmdInput extends TInput > ( input ?: Partial < TCmdInput > , strict = false ) : CommandBehavior < TInput , TOutput , TOutput , TConfiguration > {
231
231
const cmdStub = this . send . withArgs ( this . createInputMatcher ( input , strict ) ) ;
232
232
return new CommandBehavior ( this , cmdStub ) ;
233
233
}
234
234
235
235
on < TCmdInput extends TInput , TCmdOutput extends TOutput > (
236
236
command : new ( input : TCmdInput ) => AwsCommand < TCmdInput , TCmdOutput > , input ?: Partial < TCmdInput > , strict = false ,
237
- ) : CommandBehavior < TInput , TOutput , TCmdOutput > {
237
+ ) : CommandBehavior < TInput , TOutput , TCmdOutput , TConfiguration > {
238
238
const matcher = match . instanceOf ( command ) . and ( this . createInputMatcher ( input , strict ) ) ;
239
239
const cmdStub = this . send . withArgs ( matcher ) ;
240
- return new CommandBehavior < TInput , TOutput , TCmdOutput > ( this , cmdStub ) ;
240
+ return new CommandBehavior < TInput , TOutput , TCmdOutput , TConfiguration > ( this , cmdStub ) ;
241
241
}
242
242
243
243
private createInputMatcher < TCmdInput extends TInput > ( input ?: Partial < TCmdInput > , strict = false ) {
@@ -246,71 +246,78 @@ export class AwsStub<TInput extends object, TOutput extends MetadataBearer> impl
246
246
: match . any ;
247
247
}
248
248
249
- resolves ( response : CommandResponse < TOutput > ) : AwsStub < TInput , TOutput > {
249
+ resolves ( response : CommandResponse < TOutput > ) : AwsStub < TInput , TOutput , TConfiguration > {
250
250
return this . onAnyCommand ( ) . resolves ( response ) ;
251
251
}
252
252
253
- resolvesOnce ( response : CommandResponse < TOutput > ) : CommandBehavior < TInput , TOutput , TOutput > {
253
+ resolvesOnce ( response : CommandResponse < TOutput > ) : CommandBehavior < TInput , TOutput , TOutput , TConfiguration > {
254
254
return this . onAnyCommand ( ) . resolvesOnce ( response ) ;
255
255
}
256
256
257
- rejects ( error ?: string | Error | AwsError ) : AwsStub < TInput , TOutput > {
257
+ rejects ( error ?: string | Error | AwsError ) : AwsStub < TInput , TOutput , TConfiguration > {
258
258
return this . onAnyCommand ( ) . rejects ( error ) ;
259
259
}
260
260
261
- rejectsOnce ( error ?: string | Error | AwsError ) : CommandBehavior < TInput , TOutput , TOutput > {
261
+ rejectsOnce ( error ?: string | Error | AwsError ) : CommandBehavior < TInput , TOutput , TOutput , TConfiguration > {
262
262
return this . onAnyCommand ( ) . rejectsOnce ( error ) ;
263
263
}
264
264
265
- callsFake ( fn : ( input : any ) => any ) : AwsStub < TInput , TOutput > {
265
+ callsFake ( fn : ( input : any , getClient : ( ) => Client < TInput , TOutput , TConfiguration > ) => any ) : AwsStub < TInput , TOutput , TConfiguration > {
266
266
return this . onAnyCommand ( ) . callsFake ( fn ) ;
267
267
}
268
268
269
- callsFakeOnce ( fn : ( input : any ) => any ) : CommandBehavior < TInput , TOutput , TOutput > {
269
+ callsFakeOnce ( fn : ( input : any , getClient : ( ) => Client < TInput , TOutput , TConfiguration > ) => any ) : CommandBehavior < TInput , TOutput , TOutput , TConfiguration > {
270
270
return this . onAnyCommand ( ) . callsFakeOnce ( fn ) ;
271
271
}
272
272
}
273
273
274
- export class CommandBehavior < TInput extends object , TOutput extends MetadataBearer , TCommandOutput extends TOutput > implements Behavior < TInput , TOutput , TCommandOutput > {
274
+ export class CommandBehavior < TInput extends object , TOutput extends MetadataBearer , TCommandOutput extends TOutput , TConfiguration > implements Behavior < TInput , TOutput , TCommandOutput , TConfiguration > {
275
275
276
276
/**
277
277
* Counter to simulate chainable `resolvesOnce()` and similar `*Once()` methods with Sinon `Stub#onCall()`.
278
278
* The counter is increased with every `*Once()` method call.
279
279
*/
280
280
private nextChainableCallNumber = 0 ;
281
281
282
+ /**
283
+ * Function to get the current Client object from inside the `callsFake()` callback.
284
+ * Since this is called from the callback when the mock function is executed,
285
+ * the current Client is the last on the Sinon `Stub#thisValues` list.
286
+ */
287
+ private getClient = ( ) => this . send . thisValues [ this . send . thisValues . length - 1 ] as Client < TInput , TOutput , TConfiguration > ;
288
+
282
289
constructor (
283
- private clientStub : AwsStub < TInput , TOutput > ,
290
+ private clientStub : AwsStub < TInput , TOutput , TConfiguration > ,
284
291
private send : SinonStub < [ AwsCommand < TInput , TOutput > ] , unknown > ,
285
292
) {
286
293
}
287
294
288
- onAnyCommand < TCmdInput extends TInput > ( input ?: Partial < TCmdInput > , strict ?: boolean ) : Behavior < TInput , TOutput , TOutput > {
295
+ onAnyCommand < TCmdInput extends TInput > ( input ?: Partial < TCmdInput > , strict ?: boolean ) : Behavior < TInput , TOutput , TOutput , TConfiguration > {
289
296
return this . clientStub . onAnyCommand ( input , strict ) ;
290
297
}
291
298
292
299
on < TCmdInput extends TInput , TCmdOutput extends TOutput > (
293
300
command : new ( input : TCmdInput ) => AwsCommand < TCmdInput , TCmdOutput > , input ?: Partial < TCmdInput > , strict = false ,
294
- ) : CommandBehavior < TInput , TOutput , TCmdOutput > {
301
+ ) : CommandBehavior < TInput , TOutput , TCmdOutput , TConfiguration > {
295
302
return this . clientStub . on ( command , input , strict ) ;
296
303
}
297
304
298
- resolves ( response : CommandResponse < TCommandOutput > ) : AwsStub < TInput , TOutput > {
305
+ resolves ( response : CommandResponse < TCommandOutput > ) : AwsStub < TInput , TOutput , TConfiguration > {
299
306
this . send . resolves ( response ) ;
300
307
return this . clientStub ;
301
308
}
302
309
303
- resolvesOnce ( response : CommandResponse < TCommandOutput > ) : CommandBehavior < TInput , TOutput , TCommandOutput > {
310
+ resolvesOnce ( response : CommandResponse < TCommandOutput > ) : CommandBehavior < TInput , TOutput , TCommandOutput , TConfiguration > {
304
311
this . send = this . send . onCall ( this . nextChainableCallNumber ++ ) . resolves ( response ) ;
305
312
return this ;
306
313
}
307
314
308
- rejects ( error ?: string | Error | AwsError ) : AwsStub < TInput , TOutput > {
315
+ rejects ( error ?: string | Error | AwsError ) : AwsStub < TInput , TOutput , TConfiguration > {
309
316
this . send . rejects ( CommandBehavior . normalizeError ( error ) ) ;
310
317
return this . clientStub ;
311
318
}
312
319
313
- rejectsOnce ( error ?: string | Error | AwsError ) : CommandBehavior < TInput , TOutput , TCommandOutput > {
320
+ rejectsOnce ( error ?: string | Error | AwsError ) : CommandBehavior < TInput , TOutput , TCommandOutput , TConfiguration > {
314
321
this . send . onCall ( this . nextChainableCallNumber ++ ) . rejects ( CommandBehavior . normalizeError ( error ) ) ;
315
322
return this ;
316
323
}
@@ -327,13 +334,13 @@ export class CommandBehavior<TInput extends object, TOutput extends MetadataBear
327
334
return error ;
328
335
}
329
336
330
- callsFake ( fn : ( input : any ) => any ) : AwsStub < TInput , TOutput > {
331
- this . send . callsFake ( cmd => fn ( cmd . input ) ) ;
337
+ callsFake ( fn : ( input : any , getClient : ( ) => Client < TInput , TOutput , TConfiguration > ) => any ) : AwsStub < TInput , TOutput , TConfiguration > {
338
+ this . send . callsFake ( cmd => fn ( cmd . input , this . getClient ) ) ;
332
339
return this . clientStub ;
333
340
}
334
341
335
- callsFakeOnce ( fn : ( input : any ) => any ) : CommandBehavior < TInput , TOutput , TCommandOutput > {
336
- this . send . onCall ( this . nextChainableCallNumber ++ ) . callsFake ( cmd => fn ( cmd . input ) ) ;
342
+ callsFakeOnce ( fn : ( input : any , getClient : ( ) => Client < TInput , TOutput , TConfiguration > ) => any ) : CommandBehavior < TInput , TOutput , TCommandOutput , TConfiguration > {
343
+ this . send . onCall ( this . nextChainableCallNumber ++ ) . callsFake ( cmd => fn ( cmd . input , this . getClient ) ) ;
337
344
return this ;
338
345
}
339
346
}
0 commit comments