@@ -33,32 +33,47 @@ export default class FetchInterceptor extends Base{
3333 private intercept ( ) {
3434 // eslint-disable-next-line @typescript-eslint/no-this-alias
3535 const me = this ;
36- this . global . fetch = function ( input : string | FetchRequest , init : AnyObject ) {
36+ this . global . fetch = function (
37+ input : string | FetchRequest | URL ,
38+ init : AnyObject
39+ ) {
3740 let url : string ;
3841 let params : FetchRequest | AnyObject ;
39- // https://developer.mozilla.org/en-US/docs/Web/API/Request
40- // Note: the first argument of fetch maybe a Request object.
41- if ( typeof input === 'object' ) {
42+ // https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch
43+ // Note: the first argument of fetch maybe a Request or URL object.
44+ if ( input instanceof URL ) {
45+ url = input . toString ( ) ;
46+ params = init || { } ;
47+ } else if ( typeof input === 'object' ) {
4248 url = input . url ;
4349 params = input ;
4450 } else {
4551 url = input ;
4652 params = init || { } ;
4753 }
48- const method = ( params && params . method ? params . method : 'GET' ) as unknown as HttpVerb ;
54+ const method = ( params && params . method
55+ ? params . method
56+ : 'GET' ) as unknown as HttpVerb ;
4957 const requestUrl = me . getFullRequestUrl ( url , method ) ;
5058
5159 return new Promise ( ( resolve , reject ) => {
52- const mockItem :MockItem | null = me . matchMockRequest ( requestUrl , method ) ;
60+ const mockItem : MockItem | null = me . matchMockRequest (
61+ requestUrl ,
62+ method
63+ ) ;
5364
5465 if ( ! mockItem ) {
55- me . fetch ( requestUrl , params ) . then ( resolve ) . catch ( reject ) ;
66+ me . fetch ( input , init ) . then ( resolve ) . catch ( reject ) ;
5667 return ;
5768 }
5869
5970 me . setTimeoutForSingal ( params as FetchRequest , reject ) ;
6071
61- const requestInfo = me . getRequestInfo ( { ...params , url : requestUrl , method : method as HttpVerb } ) ;
72+ const requestInfo = me . getRequestInfo ( {
73+ ...params ,
74+ url : requestUrl ,
75+ method : method as HttpVerb ,
76+ } ) ;
6277 requestInfo . doOriginalCall = async ( ) : Promise < OriginalResponse > => {
6378 const res = await me . getOriginalResponse ( requestUrl , params ) ;
6479 requestInfo . doOriginalCall = undefined ;
@@ -69,13 +84,20 @@ export default class FetchInterceptor extends Base{
6984 if ( remoteInfo ) {
7085 params . method = remoteInfo . method || method ;
7186 me . setRequestHeadersForRemoteRequest ( mockItem , params as AnyObject ) ;
72- me . fetch ( remoteInfo . url , params ) . then ( ( fetchResponse : FetchResponse ) => {
73- me . sendRemoteResult ( fetchResponse , mockItem , requestInfo , resolve ) ;
74- } ) . catch ( reject ) ;
87+ me . fetch ( remoteInfo . url , params )
88+ . then ( ( fetchResponse : FetchResponse ) => {
89+ me . sendRemoteResult (
90+ fetchResponse ,
91+ mockItem ,
92+ requestInfo ,
93+ resolve
94+ ) ;
95+ } )
96+ . catch ( reject ) ;
7597 return ;
7698 }
7799
78- me . doMockRequest ( mockItem , requestInfo , resolve ) . then ( isBypassed => {
100+ me . doMockRequest ( mockItem , requestInfo , resolve ) . then ( ( isBypassed ) => {
79101 if ( isBypassed ) {
80102 me . fetch ( requestUrl , params ) . then ( resolve ) . catch ( reject ) ;
81103 }
0 commit comments