Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/idootop/tauri-plugin-cors-fetch
Enabling Cross-Origin Resource Sharing (CORS) for Fetch Requests within Tauri applications.
https://github.com/idootop/tauri-plugin-cors-fetch
cors fetch tauri unofficial-tauri-plugin
Last synced: 3 months ago
JSON representation
Enabling Cross-Origin Resource Sharing (CORS) for Fetch Requests within Tauri applications.
- Host: GitHub
- URL: https://github.com/idootop/tauri-plugin-cors-fetch
- Owner: idootop
- License: mit
- Created: 2024-03-23T13:09:49.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-08-28T07:38:10.000Z (4 months ago)
- Last Synced: 2024-09-29T09:23:08.455Z (3 months ago)
- Topics: cors, fetch, tauri, unofficial-tauri-plugin
- Language: Rust
- Homepage: https://crates.io/crates/tauri-plugin-cors-fetch
- Size: 352 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
![tauri-plugin-cors-fetch](https://github.com/idootop/tauri-plugin-cors-fetch/raw/main/banner.png)
[![crates.io](https://img.shields.io/crates/v/tauri-plugin-cors-fetch.svg)](https://crates.io/crates/tauri-plugin-cors-fetch)
[![Documentation](https://docs.rs/tauri-plugin-cors-fetch/badge.svg)](https://docs.rs/crate/tauri-plugin-cors-fetch)
[![MIT licensed](https://img.shields.io/crates/l/tauri-plugin-cors-fetch.svg)](./LICENSE)An **unofficial** Tauri plugin that enables seamless cross-origin resource sharing (CORS) for web fetch requests within Tauri applications.
## Overview
When building cross-platform desktop applications with [Tauri](https://tauri.app), we often need to access services like [OpenAI](https://openai.com/product) that are restricted by **Cross-Origin Resource Sharing (CORS)** policies in web environments.
However, on the desktop, we can bypass CORS and access these services directly. While the official [tauri-plugin-http](https://crates.io/crates/tauri-plugin-http) can bypass CORS, it requires modifying your network requests and might not be compatible with third-party dependencies that rely on the standard `fetch` API.
## How it Works
This plugin extends the official [tauri-plugin-http](https://crates.io/crates/tauri-plugin-http) by hooking into the browser's native `fetch` method during webpage initialization. It transparently redirects requests to the [tauri-plugin-http](https://crates.io/crates/tauri-plugin-http), allowing you to use the standard `fetch` API without additional code changes or workarounds.
## Installation
1. Add the plugin to your Tauri project's dependencies:
```shell
# src-tauri
cargo add tauri-plugin-cors-fetch
```2. Initialize the plugin in your Tauri application setup:
```rust
// src-tauri/main.rs
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_cors_fetch::init())
.run(tauri::generate_context!())
.expect("failed to run app");
}
```3. Add permissions in your `capabilities` configuration:
```json
// src-tauri/capabilities/default.json
{
"permissions": ["cors-fetch:default"]
}
```4. Enable `withGlobalTauri` in your Tauri configuration:
```json
// src-tauri/tauri.conf.json
{
"app": {
"withGlobalTauri": true
}
}
```## Usage
After installing and initializing the plugin, you can start making `fetch` requests from your Tauri application without encountering CORS-related errors.
```javascript
// Enable CORS for the hooked fetch globally (default is true on app start)
window.enableCORSFetch(true);// Use the hooked fetch with CORS support
fetch("https://example.com/api")
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));// Use the hooked fetch directly
window.hookedFetch("https://example.com/api");// Use the original, unhooked fetch
window.originalFetch("https://example.com/api");
```## Limitation
1. **No Custom CSP Policy Support**: By default, all HTTP/HTTPS requests will be redirected to local native requests.
2. **No XMLHttpRequest Support**: The plugin is designed specifically to work with the modern `fetch` API and does not support `XMLHttpRequest` (XHR) requests.## License
This project is licensed under the MIT License.