From eb46b837c67ba80ee1f614b19ceda4760eb87661 Mon Sep 17 00:00:00 2001 From: Miles Richardson Date: Mon, 29 Aug 2022 16:24:02 +0100 Subject: [PATCH] fix(fetch): correctly intercept requests with `URL` inputs If `input` (first argument to fetch) is not a string, then it can be a URL, which does not have a `.url` property but does have a `.href` property This fixes a type error and also fixes tests that were failing in Node 18 without this fix. --- src/interceptors/fetch/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/interceptors/fetch/index.ts b/src/interceptors/fetch/index.ts index 14de6d3f..0e1f4086 100644 --- a/src/interceptors/fetch/index.ts +++ b/src/interceptors/fetch/index.ts @@ -40,7 +40,13 @@ export class FetchInterceptor extends Interceptor { globalThis.fetch = async (input, init) => { const request = new Request(input, init) - const url = typeof input === 'string' ? input : input.url + const url = + typeof input === 'string' + ? input + : input instanceof URL + ? input.href + : input.url + const method = request.method this.log('[%s] %s', method, url)