Skip to content
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

fetch with Request instance cause TypeError: url.startsWith is not a function #582

Closed
KoichiKiyokawa opened this issue Sep 22, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@KoichiKiyokawa
Copy link

KoichiKiyokawa commented Sep 22, 2022

cf) https://developer.mozilla.org/en-US/docs/Web/API/Request

await fetch(new Request('https://jsonplaceholder.typicode.com/users')).then((r) => r.json())

This code causes an error of TypeError: url.startsWith is not a function.
The followings are error details.

 ❯ RelativeURL.getAbsoluteURL node_modules/happy-dom/lib/location/RelativeURL.js:14:17
 ❯ eval node_modules/happy-dom/lib/fetch/FetchHandler.js:26:61
 ❯ FetchHandler.fetch node_modules/happy-dom/lib/fetch/FetchHandler.js:24:16
 ❯ GlobalWindow.fetch node_modules/happy-dom/lib/window/Window.js:473:45

Thanks.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-vhbz3s?file=test%2Findex.test.ts&initialPath=__vitest__

@stefanhoelzl
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch happy-dom@7.5.10 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/happy-dom/lib/window/Window.js b/node_modules/happy-dom/lib/window/Window.js
index 6b14a75..08f4034 100644
--- a/node_modules/happy-dom/lib/window/Window.js
+++ b/node_modules/happy-dom/lib/window/Window.js
@@ -513,6 +513,14 @@ class Window extends EventTarget_1.default {
      * @returns Promise.
      */
     async fetch(url, init) {
+        if (url.method !== undefined) {
+            init = {
+                method: url.method,
+                headers: Object.fromEntries(url.headers.entries()),
+                body: await url.arrayBuffer(),
+            };
+            url = url.url;
+        }
         return await FetchHandler_1.default.fetch(this.document, url, init);
     }
     /**

This issue body was partially generated by patch-package.

This is not a general solution but might be a starting point for a workaround until this issue is fixed!

@capricorn86 capricorn86 added the bug Something isn't working label Oct 14, 2022
stefanhoelzl added a commit to stefanhoelzl/happy-dom that referenced this issue Oct 16, 2022
@capricorn86
Copy link
Owner

Thanks for reporting @KoichiKiyokawa! 🙂

Thanks a lot for contributing @stefanhoelzl! ⭐

@stefanhoelzl fix was added as part of this release:
https://github.com/capricorn86/happy-dom/releases/tag/v7.8.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants