feat: add HTTP method aliases ($fetch.get, .post, .put, etc.)#552
feat: add HTTP method aliases ($fetch.get, .post, .put, etc.)#552productdevbook wants to merge 1 commit intounjs:mainfrom
Conversation
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>
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (3)
src/fetch.tssrc/types.tstest/index.test.ts
| 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"); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
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.
| 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.
Summary
$fetch.get(),$fetch.post(),$fetch.put(),$fetch.delete(),$fetch.patch(),$fetch.head(),$fetch.options()methodis omitted from options type for type safety$fetch.create()instancesUsage
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)$fetch.create()Resolves #282
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
get,post,put,delete,patch,head,options) to the fetch API for more concise request syntax.Tests