Skip to content

feat: add HTTP method aliases ($fetch.get, .post, .put, etc.)#552

Open
productdevbook wants to merge 1 commit intounjs:mainfrom
productdevbook:feat/method-aliases
Open

feat: add HTTP method aliases ($fetch.get, .post, .put, etc.)#552
productdevbook wants to merge 1 commit intounjs:mainfrom
productdevbook:feat/method-aliases

Conversation

@productdevbook
Copy link
Copy Markdown

@productdevbook productdevbook commented Mar 30, 2026

Summary

  • Add shorthand methods: $fetch.get(), $fetch.post(), $fetch.put(), $fetch.delete(), $fetch.patch(), $fetch.head(), $fetch.options()
  • method is omitted from options type for type safety
  • Works with $fetch.create() instances

Usage

// Before
await $fetch('/api/users', { method: 'GET' })
await $fetch('/api/users', { method: 'POST', body: { name: 'John' } })

// After
await $fetch.get('/api/users')
await $fetch.post('/api/users', { body: { name: 'John' } })
await $fetch.delete('/api/users/1')
await $fetch.patch('/api/users/1', { body: { name: 'Jane' } })

// Works with create()
const api = $fetch.create({ baseURL: 'https://api.example.com' })
await api.get('/users')
await api.post('/users', { body: { name: 'John' } })

Test plan

  • $fetch.get() — GET request
  • $fetch.post() — POST with JSON body
  • $fetch.put() — PUT with JSON body
  • $fetch.delete() — DELETE request
  • $fetch.patch() — PATCH with JSON body
  • $fetch.head() — HEAD request (no body)
  • Method aliases work with $fetch.create()
  • All existing tests pass (35 tests)
  • Lint, typecheck, build pass

Resolves #282

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added HTTP method shortcuts (get, post, put, delete, patch, head, options) to the fetch API for more concise request syntax.
  • Tests

    • Added comprehensive test coverage for the new HTTP method shortcuts, including response validation and path composition.

Add shorthand methods for common HTTP verbs: get, post, put, delete,
patch, head, options. Method parameter is omitted from options type
for type safety.

Resolves unjs#282

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 30, 2026

📝 Walkthrough

Walkthrough

The createFetch factory now provides HTTP verb convenience methods (get, post, put, delete, patch, head, options) directly on the returned $fetch function. Each method delegates to the underlying $fetch call while injecting the appropriate method value into the options object. No control flow or error-handling logic was altered.

Changes

Cohort / File(s) Summary
HTTP Method Aliases
src/fetch.ts, src/types.ts
Implements HTTP verb convenience methods on the $fetch function. Adds implementation in fetch.ts that injects the HTTP method into options for each verb, and introduces HttpMethodFn type in types.ts to define the signature for all method aliases on the $Fetch interface.
Tests
test/index.test.ts
Adds comprehensive test suite covering get, post, put, delete, patch, and head method aliases, verifying correct request behavior, response parsing, header normalization, and that methods work on $fetch.create(...) instances with baseURL composition.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A rabbit hops with method so neat,
Fetch shortcuts make HTTP sweet,
GET, POST, PUT in a single bound,
No spreading options all around!
Axios envy? Now we compete!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding HTTP method aliases to $fetch, with clear examples of the new functionality.
Linked Issues check ✅ Passed The PR fully implements the requested feature from issue #282: HTTP method aliases (get, post, put, delete, patch, head, options) matching axios-style ergonomics.
Out of Scope Changes check ✅ Passed All changes are directly in scope: type definitions for the new methods, implementation of aliases, and comprehensive test coverage—no unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@test/index.test.ts`:
- Around line 508-551: Add a test for the missing $fetch.options() method inside
the "method aliases" describe block: call await $fetch.options(getURL("echo"))
(mirroring the $fetch.get() and $fetch.delete() tests) and assert the response
path is "/echo"; also add a similar check for the create() alias by calling
api.options("echo") on the client returned by $fetch.create({ baseURL:
getURL("") }) and asserting the path, so the options alias is covered like the
other HTTP methods referenced by $fetch and api.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 825a9fc5-eedf-4c77-bb65-789c22a2a94e

📥 Commits

Reviewing files that changed from the base of the PR and between dfbe3ca and 7945c7b.

📒 Files selected for processing (3)
  • src/fetch.ts
  • src/types.ts
  • test/index.test.ts

Comment on lines +508 to +551
describe("method aliases", () => {
it("$fetch.get()", async () => {
const result = await $fetch.get(getURL("echo"));
expect(result.path).toBe("/echo");
});

it("$fetch.post()", async () => {
const { body, headers } = await $fetch.post(getURL("post"), {
body: { foo: "bar" },
});
expect(body).toEqual({ foo: "bar" });
expect(headers["content-type"]).toBe("application/json");
});

it("$fetch.put()", async () => {
const { body } = await $fetch.put(getURL("post"), {
body: { updated: true },
});
expect(body).toEqual({ updated: true });
});

it("$fetch.delete()", async () => {
const result = await $fetch.delete(getURL("echo"));
expect(result.path).toBe("/echo");
});

it("$fetch.patch()", async () => {
const { body } = await $fetch.patch(getURL("post"), {
body: { patched: true },
});
expect(body).toEqual({ patched: true });
});

it("$fetch.head()", async () => {
const result = await $fetch.head(getURL("ok"));
expect(result).toBeUndefined();
});

it("method aliases work with create()", async () => {
const api = $fetch.create({ baseURL: getURL("") });
const result = await api.get("echo");
expect(result.path).toBe("/echo");
});
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Missing test for $fetch.options() method.

The implementation in src/fetch.ts adds seven HTTP method aliases including options, but only six are tested here. Consider adding a test for $fetch.options() for complete coverage.

💚 Proposed test to add
     it("$fetch.head()", async () => {
       const result = await $fetch.head(getURL("ok"));
       expect(result).toBeUndefined();
     });
+
+    it("$fetch.options()", async () => {
+      const result = await $fetch.options(getURL("echo"));
+      expect(result.path).toBe("/echo");
+    });

     it("method aliases work with create()", async () => {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
describe("method aliases", () => {
it("$fetch.get()", async () => {
const result = await $fetch.get(getURL("echo"));
expect(result.path).toBe("/echo");
});
it("$fetch.post()", async () => {
const { body, headers } = await $fetch.post(getURL("post"), {
body: { foo: "bar" },
});
expect(body).toEqual({ foo: "bar" });
expect(headers["content-type"]).toBe("application/json");
});
it("$fetch.put()", async () => {
const { body } = await $fetch.put(getURL("post"), {
body: { updated: true },
});
expect(body).toEqual({ updated: true });
});
it("$fetch.delete()", async () => {
const result = await $fetch.delete(getURL("echo"));
expect(result.path).toBe("/echo");
});
it("$fetch.patch()", async () => {
const { body } = await $fetch.patch(getURL("post"), {
body: { patched: true },
});
expect(body).toEqual({ patched: true });
});
it("$fetch.head()", async () => {
const result = await $fetch.head(getURL("ok"));
expect(result).toBeUndefined();
});
it("method aliases work with create()", async () => {
const api = $fetch.create({ baseURL: getURL("") });
const result = await api.get("echo");
expect(result.path).toBe("/echo");
});
});
describe("method aliases", () => {
it("$fetch.get()", async () => {
const result = await $fetch.get(getURL("echo"));
expect(result.path).toBe("/echo");
});
it("$fetch.post()", async () => {
const { body, headers } = await $fetch.post(getURL("post"), {
body: { foo: "bar" },
});
expect(body).toEqual({ foo: "bar" });
expect(headers["content-type"]).toBe("application/json");
});
it("$fetch.put()", async () => {
const { body } = await $fetch.put(getURL("post"), {
body: { updated: true },
});
expect(body).toEqual({ updated: true });
});
it("$fetch.delete()", async () => {
const result = await $fetch.delete(getURL("echo"));
expect(result.path).toBe("/echo");
});
it("$fetch.patch()", async () => {
const { body } = await $fetch.patch(getURL("post"), {
body: { patched: true },
});
expect(body).toEqual({ patched: true });
});
it("$fetch.head()", async () => {
const result = await $fetch.head(getURL("ok"));
expect(result).toBeUndefined();
});
it("$fetch.options()", async () => {
const result = await $fetch.options(getURL("echo"));
expect(result.path).toBe("/echo");
});
it("method aliases work with create()", async () => {
const api = $fetch.create({ baseURL: getURL("") });
const result = await api.get("echo");
expect(result.path).toBe("/echo");
});
});
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/index.test.ts` around lines 508 - 551, Add a test for the missing
$fetch.options() method inside the "method aliases" describe block: call await
$fetch.options(getURL("echo")) (mirroring the $fetch.get() and $fetch.delete()
tests) and assert the response path is "/echo"; also add a similar check for the
create() alias by calling api.options("echo") on the client returned by
$fetch.create({ baseURL: getURL("") }) and asserting the path, so the options
alias is covered like the other HTTP methods referenced by $fetch and api.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request method to function alias

1 participant