Skip to content

Commit

Permalink
fix: remove undefined method and query/params from fetch options (
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Oct 8, 2024
1 parent b4c9990 commit 80aa991
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}

Expand Down
25 changes: 24 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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(
Expand Down Expand Up @@ -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");
});
Expand Down Expand Up @@ -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),
});
});
});

0 comments on commit 80aa991

Please sign in to comment.