tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[feat] Universal Fetch API

Open Nickersoft opened this issue 3 years ago • 4 comments

Describe the problem

Hey folks, I'm curious as to why Tauri does not opt-in for a kind of universal fetch polyfill (i.e. one that would call the browser fetch on the web, and Tauri's http module when running in Tauri). I'm attempting to build a hybrid application that I can deploy both to Tauri as well as a web browser (similar to how Notion's app supports both). I was surprised to learn that Tauri's fetch implementation is not compatible with the function signature of the browser fetch, resulting in me having to write a compatibility layer over it:

export async function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
	const { fetch } = await import('@tauri-apps/api/http');

	const res = await fetch(input.toString(), {
		...init,
		method: init.method as HttpVerb,
		body: {
			type: 'Text',
			payload: init.body
		}
	});

	return new Response(JSON.stringify(res.data) as BodyInit, res);
}

Furthermore, I have to wrap calls to this fetch implementation in conditional statements that check if the app is running in Tauri. My app is in SvelteKit, and having to juggle calls to different fetch implementations from SvelteKit's load function, the browser client, and Tauri is all fairly cumbersome.

Describe the solution you'd like

As most of the issues I see on here regarding CORS and whatnot have the simple response of "just use Tauri's HTTP module", I think it could be beneficial to make the fetch API fully compatible with that of browsers, and automatically swap out the browser's fetch with Tauri's fetch when the app is built with the HTTP module enabled.

I'm not sure if there's been a previous discussion around this, or if there were specific reasons the HTTP module has gone in the direction it did.

Alternatives considered

At the very least, I think it could be good to make the fetch API compatible with that of browsers, even if a polyfill is not added automatically. This would prevent myself and other users from having to write shims to make TypeScript not complain.

Additional context

No response

Nickersoft avatar Jun 27 '22 19:06 Nickersoft

@Nickersoft your snippet was very helpful, thanks!

gasser707 avatar Aug 28 '22 20:08 gasser707

Adding automatic poly fills is outside the scope of Tauri, also something we cannot really do since we do not control the frontend framework. But adding a fetch compatible function to the http module is something we have considered before yes. If you're interested in this, you might consider opening a PR for it, as there are currently many things that have to take higher priority for the team.

JonasKruckenberg avatar Aug 29 '22 08:08 JonasKruckenberg

I believe #1237 closes this.

Uzaaft avatar Jan 22 '23 12:01 Uzaaft

Nope #1237 is way older than this issue. But #5136 will likely close it.

FabianLars avatar Jan 22 '23 13:01 FabianLars

Agree with that for hybrid applications it should simply be fetch that always works as expected. With the current impl it's even not possible to set credentials: true for cookies. I didn't know that, using just fetch, and for some reason my proxy gets a GET request although I'm using POST

await http.fetch(`${PREFIX}/api/login`, {
  method: 'POST',
  // ..      
})

// => GET /api/login

I'm using tauri = { version = "2.0.0-alpha.6"}

DaAitch avatar Apr 04 '23 18:04 DaAitch

closing, this will be part of tauri-plugin-http, see https://github.com/tauri-apps/plugins-workspace/pull/428

amrbashir avatar Jun 15 '23 18:06 amrbashir