Skip to content

Commit 07fb078

Browse files
authored
Allow passing custom fetch function (#218)
* Allow passing custom fetch function * Fixing a mistake * Using ApiConfig instead of second param
1 parent 14e40cf commit 07fb078

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

templates/base/http-clients/fetch-http-client.eta

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface ApiConfig<SecurityDataType = unknown> {
3131
baseUrl?: string;
3232
baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">;
3333
securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void;
34+
customFetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>
3435
}
3536

3637
export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response {
@@ -51,6 +52,7 @@ export class HttpClient<SecurityDataType = unknown> {
5152
private securityData: SecurityDataType | null = null;
5253
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
5354
private abortControllers = new Map<CancelToken, AbortController>();
55+
private customFetch = fetch;
5456

5557
private baseApiParams: RequestParams = {
5658
credentials: 'same-origin',
@@ -118,7 +120,7 @@ export class HttpClient<SecurityDataType = unknown> {
118120
},
119121
};
120122
}
121-
123+
122124
private createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => {
123125
if (this.abortControllers.has(cancelToken)) {
124126
const abortController = this.abortControllers.get(cancelToken);
@@ -127,7 +129,7 @@ export class HttpClient<SecurityDataType = unknown> {
127129
}
128130
return void 0;
129131
}
130-
132+
131133
const abortController = new AbortController();
132134
this.abortControllers.set(cancelToken, abortController);
133135
return abortController.signal;
@@ -141,7 +143,7 @@ export class HttpClient<SecurityDataType = unknown> {
141143
this.abortControllers.delete(cancelToken);
142144
}
143145
}
144-
146+
145147
public request = async <T = any, E = any>({
146148
body,
147149
secure,
@@ -159,7 +161,7 @@ export class HttpClient<SecurityDataType = unknown> {
159161
const payloadFormatter = this.contentFormatters[type || ContentType.Json];
160162
const responseFormat = format && requestParams.format;
161163

162-
return fetch(
164+
return this.customFetch(
163165
`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`,
164166
{
165167
...requestParams,
@@ -188,7 +190,7 @@ export class HttpClient<SecurityDataType = unknown> {
188190
r.error = e;
189191
return r;
190192
});
191-
193+
192194
if (cancelToken) {
193195
this.abortControllers.delete(cancelToken);
194196
}

0 commit comments

Comments
 (0)