11import {
2- FetchMethod ,
2+ Configuration ,
33 HttpMethod ,
44 MatcherUrl ,
55 MockHandler ,
6- MockHandlerFunction ,
76 RequestUrl ,
87 Route ,
98 RouteMatcher
@@ -18,20 +17,32 @@ import {
1817import MatcherUtils from './matcher-utils' ;
1918import ResponseUtils from './response-utils' ;
2019
20+ const defaultConfiguration : Configuration = {
21+ enableFallback : true ,
22+ middleware : ( request , response ) => response
23+ } ;
24+
2125class FetchMock {
22- public realFetch : FetchMethod ;
26+ private realFetch : (
27+ input : RequestInfo ,
28+ init ?: RequestInit
29+ ) => Promise < Response > ;
30+ private configuration : Configuration ;
2331 private routes : Route [ ] ;
2432 private scope : GlobalFetch ;
2533
26- constructor ( scope : GlobalFetch ) {
34+ constructor ( scope : GlobalFetch , configuration : Partial < Configuration > ) {
2735 this . scope = scope ;
36+ this . configuration = Object . assign ( { } , defaultConfiguration , configuration ) ;
2837 this . realFetch = scope . fetch ;
2938 this . routes = [ ] ;
3039 this . scope . fetch = this . fetchproxy . bind ( this ) ;
3140 }
3241
33- static init ( ) : FetchMock {
34- return new FetchMock ( window ) ;
42+ static configure (
43+ configuration : Partial < Configuration > = defaultConfiguration
44+ ) : FetchMock {
45+ return new FetchMock ( window , configuration ) ;
3546 }
3647
3748 restore ( ) {
@@ -55,9 +66,7 @@ class FetchMock {
5566 }
5667
5768 mock ( matcher : RouteMatcher , handler : MockHandler ) {
58- if ( handler === this . realFetch ) {
59- this . routes . push ( { matcher, handler : ResponseUtils . use ( this . realFetch ) } ) ;
60- } else if ( typeof handler === 'function' ) {
69+ if ( typeof handler === 'function' ) {
6170 this . routes . push ( { matcher, handler } ) ;
6271 } else {
6372 this . routes . push ( { matcher, handler : ResponseUtils . json ( handler ) } ) ;
@@ -72,18 +81,38 @@ class FetchMock {
7281 input ,
7382 init
7483 ) ;
75- if ( typeof matchingRoute === 'undefined' ) {
76- throw new Error ( 'Matching route not found...' ) ;
77- }
78-
79- const handler : MockHandlerFunction = matchingRoute . handler ;
8084 const url : RequestUrl = findRequestUrl ( input , init ) ;
8185 const method : HttpMethod = findRequestMethod ( input , init ) ;
82- const pathParams = findPathParams ( url , matchingRoute . matcher . matcherUrl ) ;
8386 const queryParams = findQueryParams ( url ) ;
8487 const body = findBody ( input , init ) ;
88+ let pathParams : object = { } ;
89+ let response : Promise < Response > ;
8590
86- return handler ( { input, init, url, method, pathParams, queryParams, body } ) ;
91+ if ( typeof matchingRoute === 'undefined' ) {
92+ if ( this . configuration . enableFallback ) {
93+ response = this . realFetch ( input , init ) ;
94+ } else {
95+ throw new Error ( `Did not find any matching route for url: ${ url } ` ) ;
96+ }
97+ } else {
98+ pathParams = findPathParams ( url , matchingRoute . matcher . matcherUrl ) ;
99+ response = matchingRoute . handler ( {
100+ input,
101+ init,
102+ url,
103+ method,
104+ pathParams,
105+ queryParams,
106+ body
107+ } ) ;
108+ }
109+
110+ return response . then ( resp =>
111+ this . configuration . middleware (
112+ { input, init, url, method, queryParams, pathParams, body } ,
113+ resp
114+ )
115+ ) ;
87116 }
88117
89118 private findMatchingRoute (
0 commit comments