hono-openapi expects `method` parameter to be of a specific type
After upgrading to v0.14.0, all of my routes suddenly exploded with errors related to method needing to be of type Method.
Types of property 'method' are incompatible.
Type 'string' is not assignable to type 'Method'.ts(2345)
AFAIK, the Method type is not exported from hono-openapi, so there isn't any way to satisfy this type constraint. Previously, simply using a string worked fine.
The method type is fairly straight-forward, so I'm not sure why this error is occuring now:
type Method = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options' | 'trace';
Strangely, Hono exports the METHODS array, which can be used and it satisfies the Typescript analysis:
However, this is suboptimal because now the actual method is hidden within the array value.
Happened to me too. I have not been able to create a minimum reproducible state, but I will share what I know.
- It seems that this does not occur with code in honojs/middleware repository, but occurs when the package is used externally.
- Fixing the openapi3-ts version to 4.3.1 solves the problem.
- https://github.com/metadevpro/openapi3-ts/issues/134
- This change seems to have affected
-
method: 'get' as constalso solves the problem.
@akineko That works. Overriding the openapi3-ts version to 4.3.1 in package.json got the errors to disappear.
"overrides": {
"@hono/zod-openapi": {
"@asteasolutions/zod-to-openapi": {
"openapi3-ts": "4.3.1"
}
}
}
Hi @bruceharrison1984 @akineko
I can't reproduce it, but if we bump up the openapi3-ts to the latest version, will it be solved? Here:
https://github.com/honojs/middleware/blob/1b641bee14a3f9294640834c3ce6de7298e2f255/packages/zod-openapi/package.json#L47
Hi, @yusukebe I tried troubleshooting to see if I could reproduce this issue with minimal code, and I found the cause. Setting nodeResolution to node in tsconfig.json triggers this error.
1.setup
pnpm create hono my-app
# select nodejs
cd my-app
pnpm add zod @hono/zod-openapi
pnpm add -D typescript
- modify the
src/indext.ts
import { serve } from "@hono/node-server";
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
const app = new OpenAPIHono();
const route = createRoute({
method: "get",
path: "/users/{id}",
tags: ["users"],
description: "Get a user by id",
security: [{ Bearer: [] }],
request: {
params: z.object({ id: z.string() }),
},
responses: {
200: {
content: {
"application/json": {
schema: z.object({
id: z.string(),
name: z.string(),
age: z.number(),
}),
},
},
description: "Retrieve the user",
},
},
});
app.openapi(route, (c) => {
const { id } = c.req.valid("param");
return c.json(
{
id,
name: "Ultra-man",
age: 20,
},
200,
);
});
const port = 3000;
console.log(`Server is running on port ${port}`);
serve({
fetch: app.fetch,
port,
});
- modify the
tsconfig.json
- "moduleResolution": "Bundler",
+ "moduleResolution": "Node", # or "Node10",
- check types
pnpm tsc --noEmit
# error occurred
I'm aware that setting Node16 or Bundler in tsconfig.json's nodeResolution is preferable, but due to issues with libraries that don't work with these settings, I'm currently using Node.
Hi @akineko
Thanks. You are right. It does not work with setting "moduleResolution": "Node", # or "Node10" .
but due to issues with libraries that don't work with these settings, I'm currently using Node.
Hmm. I don't know how the both work correctly now 😞