honox icon indicating copy to clipboard operation
honox copied to clipboard

[suggestion] base-x routing path-parameter type is any

Open rehatch-yoshimori-otsuka opened this issue 1 year ago • 3 comments

[!NOTE] You can ignore if you can not get this issue clearly. I understand this honox project is alpha version.

What is the feature you are proposing?

I need type suggestion for path parameter(c.req.param()) in createRoute.

What version of HonoX are you using?

0.1.0

What steps can reproduce the bug?

create a new project via pnpm create@hono and x-basic add a directory structure with 2 dynamic path parameters (app/routes/companies/[companyId]/[postId]/index.ts)

What is the expected behavior?

When I type c.req.param(' then the VSCode(TS Server) suggest 'companyId' and 'postId'

What do you see instead?

When I type c.req.param(' then the VSCode(TS Server) suggest nothing

Additional information

example code

app/routes/companies/[companyId]/[postId]/index.ts

import { zValidator } from '@hono/zod-validator'
import { createRoute } from 'honox/factory'
import { object, string } from 'zod'

const paramsSchema = object({
  companyId: string(),
  postId: string(),
})

// before route.get('companies/:companyId/:postId',async (c) => {
// try const GET = createRoute<{ in: { param: ParamSchema } }>(async (c) => {
const GET = createRoute(zValidator('param', paramsSchema), async (c) => {
  // no suggestion for 'companyId' because param<any>(key: string) is any Generics
  const companyId = c.req.param('companyId')
  const postId = c.req.param('postId')

  return c.json({})
})

// biome-ignore lint/style/noDefaultExport: Router specification
export default GET
// type 
const GET: [H<Env, any, {
    in: {
        param: {
            companyId: string;
            postId: string;
        };
    };
    out: {
        param: {
            companyId: string;
            postId: string;
        };
    };
}, Promise<Response & TypedResponse<...>>>, H<...>]

in my case, I can find the solution for above case.

now still path parameter(c.req.param('companyId')) has no suggestion

import { zValidator } from '@hono/zod-validator'
import { createRoute } from 'honox/factory'
import { object, string } from 'zod'

const paramsSchema = object({
  companyId: string(),
  postId: string(),
})

// try const GET = createRoute<{ in: { param: ParamSchema } }>(async (c) => {
const GET = createRoute(zValidator('param', paramsSchema), async (c) => {
+  // works! suggestion for 'companyId'
+  const { companyId, postId } = c.req.valid('param')

  // not working. no suggestion for 'companyId' because param<any>(key: string) is any Generics
-  // const companyId = c.req.param('companyId')
-  // const postId = c.req.param('postId')

  return c.json({})
})

// biome-ignore lint/style/noDefaultExport: Router specification
export default GET

Hi @rehatch-yoshimori-otsuka

Yeah, this is a known issue and not a bug. But, it's too difficult or impossible to support inferring path params without changing APIs.

We can close this issue, but let it keep open for a while, we might have a good idea. Thanks!

yusukebe avatar Feb 14 '24 12:02 yusukebe

@yusukebe Thank you for your reading! I understand what you tell me.

Yeah, this is a known issue and not a bug. We can close this issue,

OK! you can close this issue without fixing this!