https://github.com/wong2/electron-fetch-via-main
Simple cross origin requests in Electron
https://github.com/wong2/electron-fetch-via-main
electron fetch
Last synced: 2 months ago
JSON representation
Simple cross origin requests in Electron
- Host: GitHub
- URL: https://github.com/wong2/electron-fetch-via-main
- Owner: wong2
- License: mit
- Created: 2023-10-24T11:22:18.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-25T06:46:25.000Z (over 2 years ago)
- Last Synced: 2025-09-23T02:59:14.651Z (9 months ago)
- Topics: electron, fetch
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/electron-fetch-via-main
- Size: 26.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# electron-fetch-via-main
## Why
This module helps you bypass restrictions when making cross origin requests in renderer process, by delegate the requests to main process.
## Install
```
npm install electron-fetch-via-main
```
## Usage
> Note that this module only work with `contextIsolation: false`
In main process
```
import { setupMainFetchlistener } from 'electron-fetch-via-main'
setupMainFetchlistener()
```
In preload scripts, expose `fetchViaMain` to renderer
```
import { fetchViaMain } from 'electron-fetch-via-main'
window.fetchViaMain = fetchViaMain
```
In renderer process, just replace `fetch` with `fetchViaMain`
```
const resp = await window.fetchViaMain('https://...', {
method: 'POST',
body: { ... },
})
// read json
await resp.json()
// streaming response
for await (const chunk of resp.body) {
...
}
```