From fa6f1f7973c7713e29ffc75da808f6100bd71052 Mon Sep 17 00:00:00 2001 From: Sam Holmes Date: Mon, 9 Dec 2024 16:57:23 -0800 Subject: [PATCH] Add `fetchCorsForced` This is to be able to to access `nativeFetch` in React Native environements. This `fetchCorsForced` method will also forcibly use the CORS servers in browser environemnts. It's idompodent in Node environements. --- CHANGELOG.md | 2 ++ src/core/fake/fake-io.ts | 3 ++- src/io/browser/browser-io.ts | 8 ++++++++ src/io/node/node-io.ts | 3 ++- src/io/react-native/react-native-worker.ts | 5 +++++ src/types/types.ts | 6 ++++++ 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d0b300ff..d0f67c932 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- added: Added `fetchCorsForced` for access to nativeFetch in React Native. + ## 2.21.0 (2024-12-02) - added: Added `networkFees` to `EdgeTransaction`. diff --git a/src/core/fake/fake-io.ts b/src/core/fake/fake-io.ts index 321836fbf..ac537efb3 100644 --- a/src/core/fake/fake-io.ts +++ b/src/core/fake/fake-io.ts @@ -45,7 +45,8 @@ export function makeFakeIo(): EdgeIo { // Networking: fetch: fakeFetch, - fetchCors: fakeFetch + fetchCors: fakeFetch, + fetchCorsForced: fakeFetch } return out } diff --git a/src/io/browser/browser-io.ts b/src/io/browser/browser-io.ts index e02238374..0482d118d 100644 --- a/src/io/browser/browser-io.ts +++ b/src/io/browser/browser-io.ts @@ -90,6 +90,14 @@ export function makeBrowserIo(): EdgeIo { // Throw the error from the first fetch instead of the one from // proxy server. throw errorToThrow + }, + + async fetchCorsForced( + uri: string, + opts?: EdgeFetchOptions + ): Promise { + const response = await fetchCorsProxy(uri, opts) + return response } } } diff --git a/src/io/node/node-io.ts b/src/io/node/node-io.ts index 38ea7dbf4..742f2eb83 100644 --- a/src/io/node/node-io.ts +++ b/src/io/node/node-io.ts @@ -23,6 +23,7 @@ export function makeNodeIo(path: string): EdgeIo { // Networking: fetch, - fetchCors: fetch + fetchCors: fetch, + fetchCorsForced: fetch } } diff --git a/src/io/react-native/react-native-worker.ts b/src/io/react-native/react-native-worker.ts index 525db37f9..df5fe130c 100644 --- a/src/io/react-native/react-native-worker.ts +++ b/src/io/react-native/react-native-worker.ts @@ -208,6 +208,11 @@ async function makeIo(): Promise { const response = await nativeFetch(uri, opts) state.nativeSuccess = true return response + }, + + async fetchCorsForced(uri: string, opts: EdgeFetchOptions = {}) { + const response = await nativeFetch(uri, opts) + return response } } diff --git a/src/types/types.ts b/src/types/types.ts index 5fe4ba9ca..3064f23ea 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -78,6 +78,12 @@ export interface EdgeIo { * on platforms where that may be a problem. */ readonly fetchCors: EdgeFetchFunction + /** + * This is like `fetch`, but will try to avoid CORS limitations + * on platforms always by forcing to use the native fetch implementation, + * which by-passes CORS issues. + */ + readonly fetchCorsForced: EdgeFetchFunction } // logging -------------------------------------------------------------