11import {
22 HandlerArgument ,
3+ JSONValue ,
34 MockHandler ,
45 MockHandlerFunction ,
56 ResponseData
67} from './types' ;
8+ import { testPromise } from './internal-utils' ;
79
8- function execHandler (
9- handler : MockHandler ,
10- args : HandlerArgument
11- ) : Promise < ResponseData > {
10+ function execHandler ( handler : MockHandler , args : HandlerArgument ) : Promise < ResponseData > {
1211 if ( typeof handler === 'function' ) {
13- return handler ( args ) ;
12+ let result = handler ( args ) ;
13+ const isPromise = testPromise ( result ) ;
14+ if ( isPromise ) {
15+ return result as Promise < ResponseData > ;
16+ } else {
17+ return Promise . resolve ( { body : result } as ResponseData ) ;
18+ }
1419 } else {
1520 return ResponseUtils . jsonPromise ( handler ) ;
1621 }
1722}
1823
19- function unwrap (
20- args : HandlerArgument
21- ) : ( handler : MockHandler ) => Promise < ResponseData > {
24+ function unwrap ( args : HandlerArgument ) : ( handler : MockHandler ) => Promise < ResponseData > {
2225 return ( handler : MockHandler ) => {
2326 if ( typeof handler === 'function' ) {
24- return handler ( args ) ;
27+ const result = handler ( args ) ;
28+ const isPromise = testPromise ( result ) ;
29+
30+ if ( isPromise ) {
31+ return result as Promise < ResponseData > ;
32+ }
33+
34+ return Promise . resolve ( { body : result } as ResponseData ) ;
2535 } else {
2636 return ResponseUtils . jsonPromise ( handler ) ;
2737 }
@@ -34,16 +44,16 @@ function merge(into: ResponseData, data: ResponseData): ResponseData {
3444 status : into . status || data . status ,
3545 statusText : into . statusText || data . statusText ,
3646 headers : Object . assign ( { } , data . headers , into . headers )
37- } ;
47+ } as ResponseData ;
3848}
3949
4050export default class ResponseUtils {
41- static json ( json : object ) : MockHandlerFunction {
51+ static json ( json : JSONValue ) : MockHandlerFunction {
4252 return ( ) => ResponseUtils . jsonPromise ( json ) ;
4353 }
4454
4555 static jsonPromise ( json : MockHandler ) : Promise < ResponseData > {
46- const response : ResponseData = { body : JSON . stringify ( json ) } ;
56+ const response : ResponseData = { body : JSON . stringify ( json ) } as ResponseData ;
4757 return Promise . resolve ( response ) ;
4858 }
4959
@@ -57,23 +67,23 @@ export default class ResponseUtils {
5767
5868 static statusCode ( status : number ) : MockHandler {
5969 return ( args : HandlerArgument ) => {
60- const response : ResponseData = { status } ;
70+ const response : ResponseData = { status } as ResponseData ;
6171 return Promise . resolve ( response ) ;
6272 } ;
6373 }
6474
6575 static statusText ( statusText : string ) : MockHandler {
6676 return ( args : HandlerArgument ) => {
67- const response : ResponseData = { statusText } ;
77+ const response : ResponseData = { statusText } as ResponseData ;
6878 return Promise . resolve ( response ) ;
6979 } ;
7080 }
7181
7282 static combine ( ...handlers : MockHandler [ ] ) : MockHandler {
7383 return ( args : HandlerArgument ) => {
74- return Promise . all (
75- handlers . map ( unwrap ( args ) )
76- ) . then ( ( data : ResponseData [ ] ) => data . reduce ( merge , { } ) ) ;
84+ return Promise . all ( handlers . map ( unwrap ( args ) ) ) . then ( ( data : ResponseData [ ] ) =>
85+ data . reduce ( merge , { } as ResponseData )
86+ ) ;
7787 } ;
7888 }
7989}
0 commit comments