Skip to content

Commit 278180d

Browse files
committed
[Feature] Allow providing your own implementation of fetch #159
1 parent 1ad83a6 commit 278180d

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/static/helpers/environment.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
import type {FetchFunction, FetchOptions} from '../clientConfig';
7+
import type {FetchFunction} from '../clientConfig';
88

99
/*
1010
* Copyright (c) 2022, Salesforce, Inc.
@@ -24,8 +24,6 @@ export const globalObject = isBrowser ? window : globalThis;
2424

2525
export const hasFetchAvailable = typeof globalObject.fetch === 'function';
2626

27-
let lazyFetch: FetchFunction;
28-
2927
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
3028
export const fetch: FetchFunction = (() => {
3129
if (isNode) {
@@ -40,13 +38,12 @@ export const fetch: FetchFunction = (() => {
4038
throw new Error(
4139
'Bad environment: it is not a node environment but fetch is not defined'
4240
);
43-
return (
44-
input: RequestInfo,
45-
init?: FetchOptions | undefined
46-
): Promise<Response> => {
41+
let lazyFetch: FetchFunction;
42+
const browserFetch: FetchFunction = (...args) => {
4743
if (!lazyFetch) {
4844
lazyFetch = globalObject.fetch;
4945
}
50-
return lazyFetch.apply(globalObject, [input, init]);
46+
return lazyFetch.apply(globalObject, args);
5147
};
48+
return browserFetch;
5249
})();

0 commit comments

Comments
 (0)