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(context): Custom header name for c.header #3289

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

akku1139
Copy link

@akku1139 akku1139 commented Aug 18, 2024

Power UP #3221 .
You can define your own headers and the editor (e.g. VSCode) can complete them.

import { Hono } from "hono"

const app = new Hono<{
  Headers: "X-Hono-Test" | "Hello-Hono"
}>()

app.get("/", (c) => {
  c.header("Hello-Hono") // Defined header will be completed
  return c.text("Hello Hono!", 100, {
  })
})

image

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@yusukebe
Copy link
Member

Hi @akku1139

Supporting adding extra header names is good. But I don't agree with using the generics for Hono. The generics for Hono have two types, Bindings and Variables. These are the most important types in the Hono type system. To keep the generics type definition simple, I think we don't have to add the Headers type for the generics. It should have only two important types.

I think it would be good to extend types in the following way:

https://hono.dev/docs/guides/jsx#override-type-definitions

@yusukebe
Copy link
Member

Regarding adding custom header types, I think it's not good to pass generics to new Hono because of the above reason. Instead of it, we can extend the types with the following method if we can implement it:

declare module 'hono/context' {
  interface CustomResponseHeader {
    'X-Custom-Foo': 'foo' | 'foo2' | 'foo3'
    'X-Custom-Bar': 'bar' | 'bar2' | 'bar3'
  }
}

@nakasyou What do you think of it?

@nakasyou
Copy link
Contributor

I think it is better than creating a generic.
Also it's more convenient if there are multi instances of Hono. I recommend the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants