Skip to content

Commit

Permalink
[Feature] Allow providing your own implementation of fetch Salesforce…
Browse files Browse the repository at this point in the history
  • Loading branch information
opepin committed Jun 8, 2024
1 parent 361aed9 commit 59127b9
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 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} from '../clientConfig';
import type {FetchFunction, FetchOptions} from '../clientConfig';

/*
* Copyright (c) 2022, Salesforce, Inc.
Expand All @@ -24,6 +24,8 @@ 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 @@ -38,6 +40,13 @@ export const fetch: FetchFunction = (() => {
throw new Error(
'Bad environment: it is not a node environment but fetch is not defined'
);

return globalObject.fetch;
return (
input: RequestInfo,
init?: FetchOptions | undefined
): Promise<Response> => {
if (!lazyFetch) {
lazyFetch = globalObject.fetch;
}
return lazyFetch.apply(globalObject, [input, init]);
};
})();

0 comments on commit 59127b9

Please sign in to comment.