Skip to content

Commit

Permalink
[Feature] Allow providing your own implementation of fetch #159
Browse files Browse the repository at this point in the history
  • Loading branch information
opepin committed Jun 8, 2024
1 parent 1ad83a6 commit 278180d
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/static/helpers/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import type {FetchFunction, FetchOptions} from '../clientConfig';
import type {FetchFunction} from '../clientConfig';

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

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

let lazyFetch: FetchFunction;

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
export const fetch: FetchFunction = (() => {
if (isNode) {
Expand All @@ -40,13 +38,12 @@ export const fetch: FetchFunction = (() => {
throw new Error(
'Bad environment: it is not a node environment but fetch is not defined'
);
return (
input: RequestInfo,
init?: FetchOptions | undefined
): Promise<Response> => {
let lazyFetch: FetchFunction;
const browserFetch: FetchFunction = (...args) => {
if (!lazyFetch) {
lazyFetch = globalObject.fetch;
}
return lazyFetch.apply(globalObject, [input, init]);
return lazyFetch.apply(globalObject, args);
};
return browserFetch;
})();

0 comments on commit 278180d

Please sign in to comment.