From f86d417775ffab1aad4a024991e4a5b50507a5ac Mon Sep 17 00:00:00 2001 From: Kian Salout Date: Sun, 29 Sep 2024 23:36:12 +0200 Subject: [PATCH] refactor(fetch): Add method aliases for HTTP verbs --- src/fetch.ts | 13 +++++++++++-- src/types.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ test/index.test.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index 3c2b4408..5728cc80 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -15,6 +15,7 @@ import type { ResponseType, FetchContext, $Fetch, + FetchWithAliases, FetchRequest, FetchOptions, } from "./types"; @@ -246,10 +247,10 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { return context.response; }; - const $fetch = async function $fetch(request, options) { + const $fetch: FetchWithAliases = async function $fetch(request, options) { const r = await $fetchRaw(request, options); return r._data; - } as $Fetch; + } as FetchWithAliases; $fetch.raw = $fetchRaw; @@ -266,5 +267,13 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { }, }); + const methods = ["get", "post", "put", "patch", "delete", "head", "options"]; + + for (const method of methods) { + $fetch[method] = (request, options = {}) => { + return $fetch(request, { ...options, method: method.toUpperCase() }); + }; + } + return $fetch; } diff --git a/src/types.ts b/src/types.ts index 5f2e8187..1c0cf281 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,8 +13,50 @@ export interface $Fetch { ): Promise>>; native: Fetch; create(defaults: FetchOptions, globalOptions?: CreateFetchOptions): $Fetch; + // Method Aliases + get( + request: FetchRequest, + options?: FetchOptions + ): Promise>; + + post( + request: FetchRequest, + options?: FetchOptions + ): Promise>; + + put( + request: FetchRequest, + options?: FetchOptions + ): Promise>; + + delete( + request: FetchRequest, + options?: FetchOptions + ): Promise>; + + patch( + request: FetchRequest, + options?: FetchOptions + ): Promise>; + + head( + request: FetchRequest, + options?: FetchOptions + ): Promise>; + + options( + request: FetchRequest, + options?: FetchOptions + ): Promise>; } +export type FetchWithAliases = $Fetch & { + [method: string]: ( + request: FetchRequest, + options?: FetchOptions + ) => Promise>; +}; + // -------------------------- // Options // -------------------------- diff --git a/test/index.test.ts b/test/index.test.ts index ead9a8f2..8a02f9c6 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -510,4 +510,31 @@ describe("ofetch", () => { expect(onResponse).toHaveBeenCalledTimes(2); expect(onResponseError).toHaveBeenCalledTimes(2); }); + + it("should support method aliases like get, post, put, etc.", async () => { + // Test the GET alias + expect(await $fetch.get(getURL("ok"))).to.equal("ok"); + + // Test the POST alias + const { body: postBody } = await $fetch.post(getURL("post"), { + body: { test: "post" }, + }); + expect(postBody).to.deep.eq({ test: "post" }); + + // Test the PUT alias + const { body: putBody } = await $fetch.put(getURL("post"), { + body: { test: "put" }, + }); + expect(putBody).to.deep.eq({ test: "put" }); + + // Test the DELETE alias + const deleteResponse = await $fetch.delete(getURL("ok")); + expect(deleteResponse).to.equal("ok"); + + // Test the PATCH alias + const { body: patchBody } = await $fetch.patch(getURL("post"), { + body: { test: "patch" }, + }); + expect(patchBody).to.deep.eq({ test: "patch" }); + }); });