Skip to content

Commit

Permalink
refactor: deprecate getMethod to prefer event.method
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 1, 2023
1 parent 7585861 commit bc202c0
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 21 deletions.
5 changes: 2 additions & 3 deletions src/utils/cors/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defu } from "defu";
import { appendHeaders } from "../response";
import { getMethod, getRequestHeaders, getRequestHeader } from "../request";
import { getRequestHeaders, getRequestHeader } from "../request";
import type { H3Event } from "../../event";
import type {
H3CorsOptions,
Expand Down Expand Up @@ -32,14 +32,13 @@ export function resolveCorsOptions(
}

export function isPreflightRequest(event: H3Event): boolean {
const method = getMethod(event);
const origin = getRequestHeader(event, "origin");
const accessControlRequestMethod = getRequestHeader(
event,
"access-control-request-method"
);

return method === "OPTIONS" && !!origin && !!accessControlRequestMethod;
return event.method === "OPTIONS" && !!origin && !!accessControlRequestMethod;
}

export function isCorsOriginAllowed(
Expand Down
11 changes: 6 additions & 5 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export function getRouterParam(
return params[name];
}

/**
* @deprecated Directly use `event.method` instead.
*/
export function getMethod(
event: H3Event,
defaultMethod: HTTPMethod = "GET"
Expand All @@ -49,17 +52,15 @@ export function isMethod(
expected: HTTPMethod | HTTPMethod[],
allowHead?: boolean
) {
const method = getMethod(event);

if (allowHead && method === "HEAD") {
if (allowHead && event.method === "HEAD") {
return true;
}

if (typeof expected === "string") {
if (method === expected) {
if (event.method === expected) {
return true;
}
} else if (expected.includes(method)) {
} else if (expected.includes(event.method)) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion test/cors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ describe("resolveCorsOptions", () => {
describe("isPreflightRequest", () => {
it("can detect preflight request", () => {
const eventMock = {
method: "OPTIONS",
node: {
req: {
method: "OPTIONS",
headers: {
origin: "http://example.com",
"access-control-request-method": "GET",
Expand Down
10 changes: 2 additions & 8 deletions test/event.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import supertest, { SuperTest, Test } from "supertest";
import { describe, it, expect, beforeEach } from "vitest";
import {
createApp,
App,
toNodeListener,
eventHandler,
getMethod,
} from "../src";
import { createApp, App, toNodeListener, eventHandler } from "../src";

describe("Event", () => {
let app: App;
Expand All @@ -21,7 +15,7 @@ describe("Event", () => {
app.use(
"/",
eventHandler((event) => {
expect(event.method).toBe(getMethod(event));
expect(event.method).toBe(event.method);
expect(event.method).toBe("POST");
return "200";
})
Expand Down
3 changes: 1 addition & 2 deletions test/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
App,
eventHandler,
getHeaders,
getMethod,
setHeader,
readRawBody,
setCookie,
Expand Down Expand Up @@ -77,7 +76,7 @@ describe("", () => {
body = await readRawBody(event);
} catch {}
return {
method: getMethod(event),
method: event.method,
headers,
body,
};
Expand Down
3 changes: 1 addition & 2 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
assertMethod,
toNodeListener,
eventHandler,
getMethod,
getQuery,
getRequestURL,
readFormData,
Expand Down Expand Up @@ -88,7 +87,7 @@ describe("", () => {
it("can get method", async () => {
app.use(
"/",
eventHandler((event) => getMethod(event))
eventHandler((event) => event.method)
);
expect((await request.get("/api")).text).toBe("GET");
expect((await request.post("/api")).text).toBe("POST");
Expand Down

0 comments on commit bc202c0

Please sign in to comment.