diff --git a/src/fetch.ts b/src/fetch.ts index 3c2b4408..c222466c 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; } } diff --git a/test/index.test.ts b/test/index.test.ts index df1d00ae..f432e2d7 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -9,7 +9,15 @@ 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 +26,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 +99,10 @@ describe("ofetch", () => { listener.close().catch(console.error); }); + beforeEach(() => { + fetch.mockClear(); + }); + it("ok", async () => { expect(await $fetch(getURL("ok"))).to.equal("ok"); }); @@ -510,4 +524,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), + }); + }); });