Skip to content

Commit fd22090

Browse files
Merge pull request #103 from stainless-api/yolken-fix-hono-head
Fix handling of HEAD requests when using hono plugin
2 parents 00726c2 + cc6d615 commit fd22090

5 files changed

Lines changed: 55 additions & 13 deletions

File tree

packages/hono/jest.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
transform: {
4+
"^.+\\.tsx?$": [
5+
"ts-jest",
6+
{
7+
diagnostics: false,
8+
isolatedModules: true,
9+
},
10+
],
11+
},
12+
testEnvironment: "node",
13+
modulePathIgnorePatterns: ["<rootDir>/node_modules/", "/dist/"],
14+
testPathIgnorePatterns: ["<rootDir>/node_modules/", "/dist/"],
15+
testMatch: ["**/__tests__/**/*.test.[jt]s?(x)"],
16+
setupFilesAfterEnv: [],
17+
watchPathIgnorePatterns: ["<rootDir>/node_modules/"],
18+
watchPlugins: [
19+
"jest-watch-typeahead/filename",
20+
"jest-watch-typeahead/testname",
21+
],
22+
verbose: false,
23+
};

packages/hono/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
"main": "dist/honoPlugin.js",
2323
"types": "dist/honoPlugin.d.ts",
2424
"scripts": {
25-
"clean": "rimraf dist *.tsbuildinfo"
25+
"clean": "rimraf dist *.tsbuildinfo",
26+
"test": "jest"
2627
},
2728
"devDependencies": {
29+
"@types/jest": "^29.5.10",
2830
"@types/node": "^20.10.3",
2931
"@types/qs": "^6.9.10",
30-
"typescript": "^5.3.2",
31-
"vitest": "^1.3.1"
32+
"jest": "^29.7.0",
33+
"ts-jest": "^29.1.1",
34+
"typescript": "^5.3.2"
3235
},
3336
"dependencies": {
3437
"hono": "^4.0.0",

packages/hono/src/honoPlugin.test.ts renamed to packages/hono/src/__tests__/honoPlugin.test.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Hono } from "hono";
22
import { Stl, UnauthorizedError, z } from "stainless";
3-
import { describe, expect, test } from "vitest";
4-
import { stlApi } from "./honoPlugin";
3+
import { stlApi } from "../honoPlugin";
54

65
const stl = new Stl({ plugins: {} });
76

@@ -63,9 +62,7 @@ describe("basic routing", () => {
6362
test("list posts", async () => {
6463
const response = await app.request("/api/posts");
6564
expect(response).toHaveProperty("status", 200);
66-
expect(await response.json()).toMatchInlineSnapshot(`
67-
[]
68-
`);
65+
expect(await response.json()).toMatchInlineSnapshot("[]");
6966
});
7067

7168
test("retrieve posts", async () => {
@@ -85,11 +82,19 @@ describe("basic routing", () => {
8582
expect(response).toHaveProperty("status", 405);
8683
expect(await response.json()).toMatchInlineSnapshot(`
8784
{
88-
"message": "No handler for PUT; only GET, POST.",
85+
"message": "No handler for PUT; only GET, HEAD, POST.",
8986
}
9087
`);
9188
});
9289

90+
test("head posts", async () => {
91+
const response = await app.request("/api/posts/5", {
92+
method: "HEAD",
93+
});
94+
expect(response).toHaveProperty("status", 200);
95+
expect(await response.text()).toBe("");
96+
});
97+
9398
test("update posts", async () => {
9499
const response = await app.request("/api/posts/5", {
95100
method: "POST",
@@ -159,7 +164,7 @@ describe("basic routing", () => {
159164
"received": "undefined",
160165
},
161166
],
162-
"message": "Validation error: Required at "<body>.content"",
167+
"message": "Required at "<body>.content"",
163168
}
164169
`);
165170
});

packages/hono/src/routeMatcher.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export function makeRouteMatcher(endpoints: AnyEndpoint[]) {
3434
for (const endpoint of endpoints) {
3535
const [method, path] = endpointToHono(endpoint.endpoint);
3636
routeMatcher.add(method, path, endpoint);
37+
if (method === "GET") {
38+
// Hono route matching is method-specific, so also add a
39+
// HEAD route for GET endpoints
40+
routeMatcher.add("HEAD", path, endpoint);
41+
}
3742
}
3843

3944
return routeMatcher;

pnpm-lock.yaml

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)