From 1c5ff77d3f856f0024f728645099805c8ae31bfd Mon Sep 17 00:00:00 2001 From: productdevbook Date: Mon, 30 Mar 2026 07:52:02 +0300 Subject: [PATCH 1/2] refactor: add runtime deprecation warning for params option Add console.warn when `params` option is used, guiding users to migrate to `query`. The `params` option still works as before but now emits a warning in non-production environments. Resolves #378 Co-Authored-By: Claude Opus 4.6 (1M context) --- src/utils.ts | 14 +++++++++++++- test/index.test.ts | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index a773431b..58da4288 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,6 +7,10 @@ import type { ResponseType, } from "./types.ts"; +function warnDeprecatedParams() { + console.warn("[ofetch] `params` option is deprecated. Use `query` instead."); +} + const payloadMethods = new Set( Object.freeze(["PATCH", "POST", "PUT", "DELETE"]) ); @@ -98,7 +102,15 @@ export function resolveFetchOptions< Headers ); - // Merge query/params + // Warn about deprecated `params` usage + if ( + process.env.NODE_ENV !== "production" && + (input?.params || defaults?.params) + ) { + warnDeprecatedParams(); + } + + // Merge query/params (params is deprecated alias for query) let query: Record | undefined; if (defaults?.query || defaults?.params || input?.params || input?.query) { query = { diff --git a/test/index.test.ts b/test/index.test.ts index 5ac20b07..720f99c2 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -524,4 +524,22 @@ describe("ofetch", () => { timeout: 10_000, }); }); + + it("warns when using deprecated params option", async () => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + await $fetch(getURL("params"), { params: { test: "true" } }); + expect(warn).toHaveBeenCalledWith( + "[ofetch] `params` option is deprecated. Use `query` instead." + ); + warn.mockRestore(); + }); + + it("params still works as query alias", async () => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + const result = await $fetch(getURL("params"), { + params: { test: "true" }, + }); + expect(result).toEqual({ test: "true" }); + warn.mockRestore(); + }); }); From 5cb947d29b367ab95c654c3d6e6fb4de8aa8757a Mon Sep 17 00:00:00 2001 From: productdevbook Date: Mon, 30 Mar 2026 10:26:17 +0300 Subject: [PATCH 2/2] fix: remove process.env check (browser crash), add warn-once guard - Remove `process.env.NODE_ENV` check that crashes in browsers/workers where `process` is not defined - Add warn-once flag to prevent console spam on repeated params usage Co-Authored-By: Claude Opus 4.6 (1M context) --- src/utils.ts | 13 ++++++++----- test/index.test.ts | 11 +---------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 58da4288..93de8751 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,8 +7,14 @@ import type { ResponseType, } from "./types.ts"; +let _paramsDeprecationWarned = false; function warnDeprecatedParams() { - console.warn("[ofetch] `params` option is deprecated. Use `query` instead."); + if (!_paramsDeprecationWarned) { + _paramsDeprecationWarned = true; + console.warn( + "[ofetch] `params` option is deprecated. Use `query` instead." + ); + } } const payloadMethods = new Set( @@ -103,10 +109,7 @@ export function resolveFetchOptions< ); // Warn about deprecated `params` usage - if ( - process.env.NODE_ENV !== "production" && - (input?.params || defaults?.params) - ) { + if (input?.params || defaults?.params) { warnDeprecatedParams(); } diff --git a/test/index.test.ts b/test/index.test.ts index 720f99c2..588a7a4e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -525,16 +525,7 @@ describe("ofetch", () => { }); }); - it("warns when using deprecated params option", async () => { - const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); - await $fetch(getURL("params"), { params: { test: "true" } }); - expect(warn).toHaveBeenCalledWith( - "[ofetch] `params` option is deprecated. Use `query` instead." - ); - warn.mockRestore(); - }); - - it("params still works as query alias", async () => { + it("params still works as query alias (deprecated)", async () => { const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); const result = await $fetch(getURL("params"), { params: { test: "true" },