Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fetch[method](url) aliases #447

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
ResponseType,
FetchContext,
$Fetch,
FetchWithAliases,
FetchRequest,
FetchOptions,
} from "./types";
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
42 changes: 42 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,50 @@ export interface $Fetch {
): Promise<FetchResponse<MappedResponseType<R, T>>>;
native: Fetch;
create(defaults: FetchOptions, globalOptions?: CreateFetchOptions): $Fetch;
// Method Aliases
get<T = any, R extends ResponseType = "json">(
request: FetchRequest,
options?: FetchOptions<R>
): Promise<MappedResponseType<R, T>>;

post<T = any, R extends ResponseType = "json">(
request: FetchRequest,
options?: FetchOptions<R>
): Promise<MappedResponseType<R, T>>;

put<T = any, R extends ResponseType = "json">(
request: FetchRequest,
options?: FetchOptions<R>
): Promise<MappedResponseType<R, T>>;

delete<T = any, R extends ResponseType = "json">(
request: FetchRequest,
options?: FetchOptions<R>
): Promise<MappedResponseType<R, T>>;

patch<T = any, R extends ResponseType = "json">(
request: FetchRequest,
options?: FetchOptions<R>
): Promise<MappedResponseType<R, T>>;

head<T = any, R extends ResponseType = "json">(
request: FetchRequest,
options?: FetchOptions<R>
): Promise<MappedResponseType<R, T>>;

options<T = any, R extends ResponseType = "json">(
request: FetchRequest,
options?: FetchOptions<R>
): Promise<MappedResponseType<R, T>>;
}

export type FetchWithAliases = $Fetch & {
[method: string]: <T = any, R extends ResponseType = "json">(
request: FetchRequest,
options?: FetchOptions<R>
) => Promise<MappedResponseType<R, T>>;
};

// --------------------------
// Options
// --------------------------
Expand Down
27 changes: 27 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
});
});