oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
71.83k stars 2.55k forks source link

Bun does not implement `fetch()` API correctly #11381

Open renhyl opened 1 month ago

renhyl commented 1 month ago

What version of Bun is running?

1.1.8

What platform is your computer?

Linux 5.15.146.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

Bun malforms request when using overload version of fetch()

bun run --bun dev --port 5174

ref: https://developer.mozilla.org/en-US/docs/Web/API/fetch

when calling fetch('url', options) the request works as expected

const myRequest = new Request('http://localhost:3000/graphql', {
        method: 'POST',
        body: JSON.stringify({
            query: '{onlineUsers{ email }}'
        }),
        headers: {
            'Content-Type': 'application/json'
        }
    });

// note url is specified in Request and here as a first parameter
const response = await fetch('http://localhost:3000/graphql', myRequest);

calling same with const response = await fetch(myRequest); does not work.

as compared to node behaviour, or bun without --bun flag, both variations should work the same.

What is the expected behavior?

calling fetch in either variations should have same result

const response = await fetch('http://localhost:3000/graphql', myRequest); should be same as const response = await fetch(myRequest); for a slong as myRequest contains url.

What do you see instead?

No response

Additional information

No response

renhyl commented 3 weeks ago

It would be awesome to see this resolved sooner rather than later, for one it's fetch, this affects SvelteKit, I specifically experienced this bug while using houdinigraphql in SvelteKit where houdinigraphql uses SvelteKit's server_fetch: @sveltejs/kit/src/runtime/server/fetch.js https://github.com/sveltejs/kit/blob/f67898d25ee32e9377221979d2a9e6b792786f4e/packages/kit/src/runtime/server/fetch.js#L20

npupko commented 2 weeks ago

Recently faced with the same error

export const apiRequest = async <T>(
  fetch: typeof window.fetch,
  url: string,
  params: Record<string, FormDataEntryValue | null>
) => {
  const response = await fetch(`${PUBLIC_SERVER_HOST_URL}${url}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(params)
  });

  return (await response.json()) as T;
};

Code above is sending GET request instead of POST with bun (sveltekit/houdini server fetch)