interceptors icon indicating copy to clipboard operation
interceptors copied to clipboard

`@mswjs/[email protected]` breaks `URL` as fetch argument

Open gerbyzation opened this issue 1 year ago • 0 comments

fetch supports being called with an instance of URL:

// working.mjs
const url = "https://www.google.com/";

const response = await fetch(new URL(url));
console.log(response.status);
❯ node working.mjs
200

However with [email protected] this breaks:

// not-working.mjs
import { setupServer } from "msw/node";

const server = setupServer();
server.listen();

const url = "https://www.google.com/";

const response = await fetch(new URL(url));
console.log(response.status);

server.close();
❯ node not-working.mjs
node:internal/errors:496
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:405:5)
    at new URL (node:internal/url:778:13)
    at FetchInterceptor.<anonymous> (/Users/gerbenneven/project/msw-fetch-URL-invalid/node_modules/.pnpm/@[email protected]/node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:116:38)
    at step (/Users/gerbenneven/projects/msw-fetch-URL-invalid/node_modules/.pnpm/@[email protected]/node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:59:23)
    at Object.next (/Users/gerbenneven/projects/msw-fetch-URL-invalid/node_modules/.pnpm/@[email protected]/node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:40:53)
    at fulfilled (/Users/gerbenneven/projects/msw-fetch-URL-invalid/node_modules/.pnpm/@[email protected]/node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:31:58) {
  input: 'undefined',
  code: 'ERR_INVALID_URL'
}

Node.js v20.6.1

Repro URL: https://github.com/gerbyzation/msw-fetch-url-invalid

From what I can tell this is the offending line: https://github.com/mswjs/interceptors/blob/0c2f79db35b13a2db2dd9c5614a23b808d623220/src/interceptors/fetch/index.ts#L43

When input is a URL instance it's not a string, so uses input.url which evaluates to undefined.

Note that this is from the source of 0.17.8 as 0.17.9 & 0.17.10 have no git tags associated, the code is practically the same.

gerbyzation avatar Jun 18 '24 12:06 gerbyzation