23
23
export class APIClient implements APIClient . Type {
24
24
/** The base URL for all API requests. Must be defined in subclasses. */
25
25
protected readonly baseURL : URL ;
26
+ protected options : APIClient . Options ;
26
27
27
- constructor ( baseURL : URL ) {
28
+ constructor (
29
+ baseURL : URL ,
30
+ options : APIClient . Options = { fetch : globalThis . fetch } ,
31
+ ) {
28
32
this . baseURL = baseURL ;
33
+ this . options = options ;
29
34
}
30
35
31
36
public interceptors = {
@@ -53,7 +58,7 @@ export class APIClient implements APIClient.Type {
53
58
* @param response - The response object that was received.
54
59
* @returns A promise that resolves to the (possibly processed) response.
55
60
*/
56
- protected async after ( request : Request , response : Response ) {
61
+ protected async after ( _request : Request , response : Response ) {
57
62
return response ;
58
63
}
59
64
@@ -74,7 +79,7 @@ export class APIClient implements APIClient.Type {
74
79
request = await listener ( request ) ;
75
80
}
76
81
77
- let response = await fetch ( request ) ;
82
+ let response = await this . options . fetch ( request ) ;
78
83
response = await this . after ( request , response ) ;
79
84
for ( let listener of this . interceptors . after . listeners ) {
80
85
response = await listener ( request , response ) ;
@@ -179,4 +184,8 @@ export namespace APIClient {
179
184
patch ( path : string , init ?: Omit < RequestInit , "method" > ) : Promise < Response > ;
180
185
delete ( path : string , init ?: Omit < RequestInit , "method" > ) : Promise < Response > ;
181
186
}
187
+
188
+ export interface Options {
189
+ fetch : typeof globalThis . fetch ;
190
+ }
182
191
}
0 commit comments