@@ -31,6 +31,7 @@ export interface ApiConfig<SecurityDataType = unknown> {
31
31
baseUrl?: string;
32
32
baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">;
33
33
securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void;
34
+ customFetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>
34
35
}
35
36
36
37
export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response {
@@ -51,6 +52,7 @@ export class HttpClient<SecurityDataType = unknown> {
51
52
private securityData: SecurityDataType | null = null;
52
53
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
53
54
private abortControllers = new Map<CancelToken, AbortController>();
55
+ private customFetch = fetch;
54
56
55
57
private baseApiParams: RequestParams = {
56
58
credentials: 'same-origin',
@@ -118,7 +120,7 @@ export class HttpClient<SecurityDataType = unknown> {
118
120
},
119
121
};
120
122
}
121
-
123
+
122
124
private createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => {
123
125
if (this.abortControllers.has(cancelToken)) {
124
126
const abortController = this.abortControllers.get(cancelToken);
@@ -127,7 +129,7 @@ export class HttpClient<SecurityDataType = unknown> {
127
129
}
128
130
return void 0;
129
131
}
130
-
132
+
131
133
const abortController = new AbortController();
132
134
this.abortControllers.set(cancelToken, abortController);
133
135
return abortController.signal;
@@ -141,7 +143,7 @@ export class HttpClient<SecurityDataType = unknown> {
141
143
this.abortControllers.delete(cancelToken);
142
144
}
143
145
}
144
-
146
+
145
147
public request = async <T = any, E = any>({
146
148
body,
147
149
secure,
@@ -159,7 +161,7 @@ export class HttpClient<SecurityDataType = unknown> {
159
161
const payloadFormatter = this.contentFormatters[type || ContentType.Json];
160
162
const responseFormat = format && requestParams.format;
161
163
162
- return fetch (
164
+ return this.customFetch (
163
165
`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`,
164
166
{
165
167
...requestParams,
@@ -188,7 +190,7 @@ export class HttpClient<SecurityDataType = unknown> {
188
190
r.error = e;
189
191
return r;
190
192
});
191
-
193
+
192
194
if (cancelToken) {
193
195
this.abortControllers.delete(cancelToken);
194
196
}
0 commit comments