From be03f69f2be7be4c56e1b3cf16dad11a66fb2c4c Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 8 Oct 2024 17:04:26 +0200 Subject: [PATCH 1/2] fix: remove undefined `method` and `query`/`params` options --- src/fetch.ts | 13 +++++++++++-- test/index.test.ts | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index 3c2b4408..d70ca3ec 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -108,7 +108,9 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { }; // Uppercase method name - context.options.method = context.options.method?.toUpperCase(); + if (context.options.method) { + context.options.method = context.options.method.toUpperCase() + } if (context.options.onRequest) { await callHooks(context, context.options.onRequest); @@ -120,6 +122,13 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { } if (context.options.query) { context.request = withQuery(context.request, context.options.query); + delete context.options.query; + } + if ('query' in context.options) { + delete context.options.query; + } + if ('params' in context.options) { + delete context.options.params; } } @@ -146,7 +155,7 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { // ReadableStream Body ("pipeTo" in (context.options.body as ReadableStream) && typeof (context.options.body as ReadableStream).pipeTo === - "function") || + "function") || // Node.js Stream Body typeof (context.options.body as Readable).pipe === "function" ) { diff --git a/test/index.test.ts b/test/index.test.ts index df1d00ae..08c0f6c4 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -9,7 +9,7 @@ import { readRawBody, toNodeListener, } from "h3"; -import { describe, beforeAll, afterAll, it, expect, vi } from "vitest"; +import { describe, beforeEach, beforeAll, afterAll, it, expect, vi } from "vitest"; import { Headers, FormData, Blob } from "node-fetch-native"; import { nodeMajorVersion } from "std-env"; import { $fetch } from "../src/node"; @@ -18,6 +18,8 @@ describe("ofetch", () => { let listener; const getURL = (url) => joinURL(listener.url, url); + const fetch = vi.spyOn(globalThis, "fetch") + beforeAll(async () => { const app = createApp() .use( @@ -89,6 +91,10 @@ describe("ofetch", () => { listener.close().catch(console.error); }); + beforeEach(() => { + fetch.mockClear() + }) + it("ok", async () => { expect(await $fetch(getURL("ok"))).to.equal("ok"); }); @@ -510,4 +516,13 @@ describe("ofetch", () => { expect(onResponse).toHaveBeenCalledTimes(2); expect(onResponseError).toHaveBeenCalledTimes(2); }); + + it('default fetch options', async () => { + await $fetch('https://jsonplaceholder.typicode.com/todos/1', {}) + expect(fetch).toHaveBeenCalledOnce() + const options = fetch.mock.calls[0][1] + expect(options).toStrictEqual({ + headers: expect.any(Headers), + }) + }) }); From 67276bafbee999291613ad280da8657a2a9c2a1b Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 15:07:00 +0000 Subject: [PATCH 2/2] chore: apply automated updates --- src/fetch.ts | 8 ++++---- test/index.test.ts | 28 ++++++++++++++++++---------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index d70ca3ec..c222466c 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -109,7 +109,7 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { // Uppercase method name if (context.options.method) { - context.options.method = context.options.method.toUpperCase() + context.options.method = context.options.method.toUpperCase(); } if (context.options.onRequest) { @@ -124,10 +124,10 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { context.request = withQuery(context.request, context.options.query); delete context.options.query; } - if ('query' in context.options) { + if ("query" in context.options) { delete context.options.query; } - if ('params' in context.options) { + if ("params" in context.options) { delete context.options.params; } } @@ -155,7 +155,7 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { // ReadableStream Body ("pipeTo" in (context.options.body as ReadableStream) && typeof (context.options.body as ReadableStream).pipeTo === - "function") || + "function") || // Node.js Stream Body typeof (context.options.body as Readable).pipe === "function" ) { diff --git a/test/index.test.ts b/test/index.test.ts index 08c0f6c4..f432e2d7 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -9,7 +9,15 @@ import { readRawBody, toNodeListener, } from "h3"; -import { describe, beforeEach, beforeAll, afterAll, it, expect, vi } from "vitest"; +import { + describe, + beforeEach, + beforeAll, + afterAll, + it, + expect, + vi, +} from "vitest"; import { Headers, FormData, Blob } from "node-fetch-native"; import { nodeMajorVersion } from "std-env"; import { $fetch } from "../src/node"; @@ -18,7 +26,7 @@ describe("ofetch", () => { let listener; const getURL = (url) => joinURL(listener.url, url); - const fetch = vi.spyOn(globalThis, "fetch") + const fetch = vi.spyOn(globalThis, "fetch"); beforeAll(async () => { const app = createApp() @@ -92,8 +100,8 @@ describe("ofetch", () => { }); beforeEach(() => { - fetch.mockClear() - }) + fetch.mockClear(); + }); it("ok", async () => { expect(await $fetch(getURL("ok"))).to.equal("ok"); @@ -517,12 +525,12 @@ describe("ofetch", () => { expect(onResponseError).toHaveBeenCalledTimes(2); }); - it('default fetch options', async () => { - await $fetch('https://jsonplaceholder.typicode.com/todos/1', {}) - expect(fetch).toHaveBeenCalledOnce() - const options = fetch.mock.calls[0][1] + it("default fetch options", async () => { + await $fetch("https://jsonplaceholder.typicode.com/todos/1", {}); + expect(fetch).toHaveBeenCalledOnce(); + const options = fetch.mock.calls[0][1]; expect(options).toStrictEqual({ headers: expect.any(Headers), - }) - }) + }); + }); });