Skip to content

Upgrading to 0.10.0 results in Failed to execute 'fetch' on 'Window' #1715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task
cmcnicholas opened this issue Jun 20, 2024 · 11 comments · Fixed by #1719
Closed
1 task

Upgrading to 0.10.0 results in Failed to execute 'fetch' on 'Window' #1715

cmcnicholas opened this issue Jun 20, 2024 · 11 comments · Fixed by #1719
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library question Further information is requested

Comments

@cmcnicholas
Copy link

Description

Upgraded to 0.10.0 (and 7.0.0 of openapi-typescript). Perform .POST(..) method and receive:

Failed to execute 'fetch' on 'Window': The `duplex` member must be specified for a request with a streaming body

at coreFetch (openapi-fetch.js?v=c62ec3c6:100:26)

Reproduction

As above, previously working POST request on 0.9.0, upgrade to latest 0.10.0. Execute same POST request

Expected result

To not throw.

Checklist

@cmcnicholas cmcnicholas added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Jun 20, 2024
@drwpow
Copy link
Contributor

drwpow commented Jun 20, 2024

Edit: sorry; replied to wrong issue.

@drwpow
Copy link
Contributor

drwpow commented Jun 20, 2024

Could you provide a reproduction of this and give more information about what environment you’re fetching in (browser? node? deno?)? The tests are currently passing, and I’m not able to replicate it myself. I believe you’re having the issue, but not sure what hole our test suite has that needs to be filled.

@drwpow drwpow added the question Further information is requested label Jun 20, 2024
@cmcnicholas
Copy link
Author

Could you provide a reproduction of this and give more information about what environment you’re fetching in (browser? node? deno?)? The tests are currently passing, and I’m not able to replicate it myself. I believe you’re having the issue, but not sure what hole our test suite has that needs to be filled.

Hi, I don't have a repro currently but can look to put something together.

This is an NX monorepo with apps in a browser environment.

The only piece of middleware I have is:

{
    onRequest: (req) => {
      // add authentication
      req.headers.set('Authorization', `Bearer ${accessToken}`);

      // incorrectly sets headers for GET requests
      // https://github.com/drwpow/openapi-typescript/issues/1410
      if (req.method === 'GET' || req.method === 'DELETE') {
        req.headers.delete('Content-Type');
      }

      return req;
    },
  }

which after the update I have altered to this inline with the docs.

{
    onRequest: ({ request }) => {
      // add authentication
      request.headers.set('Authorization', `Bearer ${accessToken}`);

      // incorrectly sets headers for GET requests
      // https://github.com/drwpow/openapi-typescript/issues/1410
      if (request.method === 'GET' || request.method === 'DELETE') {
        request.headers.delete('Content-Type');
      }

      return request;
    },
  }

The rest is all pretty standard stuff:

const client = createClient<paths>({
  baseUrl,
});

await client.POST('/api/organisations/', { body: { name }, });

It never invokes a request and fails in the openapi-fetch library.

@cmcnicholas
Copy link
Author

I've since put a breakpoint in the openapi-fetch.js implementation of coreFetch and traced this through to the invocation of fetch.

If I supplement the request variable with request.duplex = 'half' then it progresses and works as before. Has the creation of Request changed?

@lukasbash
Copy link

lukasbash commented Jun 20, 2024

Same issue here. The weird part is that (at least in my case) it only happens when executing E2E tests with playwright and happy-dom. In the console trace of the now failing tests I find the same error:

TypeError: Failed to execute 'fetch' on 'Window': The `duplex` member must be specified for a request with a streaming body

@yethee
Copy link

yethee commented Jun 20, 2024

I think the reason for the error is that an instance of Request is passed to fetch() as the second argument, instead of a plain object.

@hauk88
Copy link

hauk88 commented Jun 21, 2024

Hi. I ran into this while doing some testing of this lib. I have a small repo that reproduces the error:
https://github.com/hauk88/e2e-typesafe/tree/openapi-fetch-bug. The related code is in open-api/fe/src/App.tsx

It is not really refined and a bit hacked toghether with conda environments etc, so not the perfect repo to look at maybe, but I though I would leave it here anyway.

If you manage to run the backend (open-api/be/) and the frontend (open-api/fe/) it should reproduce the error in the console if you press the send button in the UI.
image

@benyaminbeyzaie
Copy link

I'm also facing this issue

@drwpow
Copy link
Contributor

drwpow commented Jun 21, 2024

Thank you all! Will push a fix to this later today. This also gives me enough info to see what needs to be added to the test suites to prevent this from happening again.

@drwpow
Copy link
Contributor

drwpow commented Jun 21, 2024

I think the reason for the error is that an instance of Request is passed to fetch() as the second argument, instead of a plain object.

Ah that’s it exactly. This change was to improve support for third-party custom fetchers, such as #1691, but it’s a flawed implementation and this currently doesn’t match the fetch spec. Will improve this handling so that it both matches the official fetch spec, and is also friendly to third-party fetch clients that aren’t technically spec-compliant but are close.

@drwpow
Copy link
Contributor

drwpow commented Jun 21, 2024

Also digging through other instances of this error, this seems to be an issue specific to Chrome/Chromium which made it difficult to catch 😅 (technically our tests run in Node.js, not Chromium). Safari has a different-but-related error, and in Firefox this Just Works™.

Anyway, wanted to dig further into the root issue before I understood the best fix to take (and I believe actual headless Playwright tests are the best thing to add here, rather than try and hack together Chrome and Safari tests in Node.js which are moving targets to say the least)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants