Skip to content

Commit

Permalink
Add fetchCorsForced
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
samholmes committed Dec 10, 2024
1 parent 7cab5f9 commit fa6f1f7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
3 changes: 2 additions & 1 deletion src/core/fake/fake-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export function makeFakeIo(): EdgeIo {

// Networking:
fetch: fakeFetch,
fetchCors: fakeFetch
fetchCors: fakeFetch,
fetchCorsForced: fakeFetch
}
return out
}
8 changes: 8 additions & 0 deletions src/io/browser/browser-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EdgeFetchResponse> {
const response = await fetchCorsProxy(uri, opts)
return response
}
}
}
3 changes: 2 additions & 1 deletion src/io/node/node-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function makeNodeIo(path: string): EdgeIo {

// Networking:
fetch,
fetchCors: fetch
fetchCors: fetch,
fetchCorsForced: fetch
}
}
5 changes: 5 additions & 0 deletions src/io/react-native/react-native-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ async function makeIo(): Promise<EdgeIo> {
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
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 -------------------------------------------------------------
Expand Down

0 comments on commit fa6f1f7

Please sign in to comment.