@@ -21,13 +21,7 @@ export function resetMocks(): void {
21
21
matchers = [ ] ;
22
22
}
23
23
24
- function fetchMock ( matcher : MatcherFunction , response : Response ) : void {
25
- matchers . push ( new Matcher ( matcher , response ) ) ;
26
-
27
- overrideFetch ( ) ;
28
- }
29
-
30
- function overrideFetch ( ) {
24
+ function overrideFetch ( ) : void {
31
25
globalThis . fetch = async (
32
26
input : URL | RequestInfo ,
33
27
options : RequestInit | undefined ,
@@ -46,11 +40,36 @@ function overrideFetch() {
46
40
} ;
47
41
}
48
42
43
+ type FetchMockHelper = {
44
+ ( url : string | RegExp , response : Response ) : void ;
45
+ startsWith : FetchMockStringHelper ;
46
+ endsWith : FetchMockStringHelper ;
47
+ includes : FetchMockStringHelper ;
48
+ } ;
49
+ type FetchMockStringHelper = ( url : string , response : Response ) => void ;
50
+ type ValidStringMethod = 'startsWith' | 'endsWith' | 'includes' ;
51
+
52
+ type FetchMockFunction = {
53
+ ( matcher : MatcherFunction , response : Response ) : void ;
54
+ get : FetchMockHelper ;
55
+ post : FetchMockHelper ;
56
+ put : FetchMockHelper ;
57
+ patch : FetchMockHelper ;
58
+ delete : FetchMockHelper ;
59
+ } ;
60
+
61
+ const fetchMock : FetchMockFunction = ( matcher , response ) : void => {
62
+ matchers . push ( new Matcher ( matcher , response ) ) ;
63
+
64
+ overrideFetch ( ) ;
65
+ } ;
66
+
49
67
/**
50
- * Simple matcher for quick mocking
68
+ * Simple matcher for quick mocking.
69
+ * For example `fetchMock.post(url: string | RegExp, response: Response)` helper.
51
70
*/
52
71
function generateFetchMockHelper ( method : string ) : FetchMockHelper {
53
- return ( url : string | RegExp , response : Response ) => {
72
+ const fn : FetchMockHelper = ( url : string | RegExp , response : Response ) => {
54
73
return fetchMock ( ( input , options ) => {
55
74
if ( getOptionMethod ( options ) !== method ) {
56
75
return false ;
@@ -69,28 +88,36 @@ function generateFetchMockHelper(method: string): FetchMockHelper {
69
88
) ;
70
89
} , response ) ;
71
90
} ;
72
- }
73
-
74
- type FetchMockHelper = ( url : string | RegExp , response : Response ) => void ;
75
-
76
- const get : FetchMockHelper = ( url , response ) : void =>
77
- generateFetchMockHelper ( 'GET' ) ( url , response ) ;
78
91
79
- const post : FetchMockHelper = ( url , response ) : void =>
80
- generateFetchMockHelper ( 'POST' ) ( url , response ) ;
92
+ fn . startsWith = generateFetchMockStringHelper ( method , 'startsWith' ) ;
93
+ fn . endsWith = generateFetchMockStringHelper ( method , 'endsWith' ) ;
94
+ fn . includes = generateFetchMockStringHelper ( method , 'includes' ) ;
81
95
82
- const put : FetchMockHelper = ( url , response ) : void =>
83
- generateFetchMockHelper ( 'PUT' ) ( url , response ) ;
96
+ return fn ;
97
+ }
84
98
85
- const patch : FetchMockHelper = ( url , response ) : void =>
86
- generateFetchMockHelper ( 'PATCH' ) ( url , response ) ;
99
+ /**
100
+ * This function generate string helper functions.
101
+ * For example `fetchMock.post.startsWith(startUrl: string, response: Response)` helper.
102
+ */
103
+ function generateFetchMockStringHelper (
104
+ method : string ,
105
+ methodName : ValidStringMethod ,
106
+ ) : FetchMockStringHelper {
107
+ return ( url , response ) => {
108
+ return fetchMock (
109
+ ( input , options ) =>
110
+ getOptionMethod ( options ) === method &&
111
+ getInputUrl ( input ) [ methodName ] ( url ) ,
112
+ response ,
113
+ ) ;
114
+ } ;
115
+ }
87
116
88
- const deleteFn : FetchMockHelper = ( url , response ) : void =>
89
- generateFetchMockHelper ( 'DELETE' ) ( url , response ) ;
90
- fetchMock . get = get ;
91
- fetchMock . post = post ;
92
- fetchMock . put = put ;
93
- fetchMock . patch = patch ;
94
- fetchMock . delete = deleteFn ;
117
+ fetchMock . get = generateFetchMockHelper ( 'GET' ) ;
118
+ fetchMock . post = generateFetchMockHelper ( 'POST' ) ;
119
+ fetchMock . put = generateFetchMockHelper ( 'PUT' ) ;
120
+ fetchMock . patch = generateFetchMockHelper ( 'PATCH' ) ;
121
+ fetchMock . delete = generateFetchMockHelper ( 'DELETE' ) ;
95
122
96
123
export default fetchMock ;
0 commit comments