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

Add helper function for headers #147

Closed
tobiasdiez opened this issue Jul 20, 2022 · 4 comments · Fixed by #157
Closed

Add helper function for headers #147

tobiasdiez opened this issue Jul 20, 2022 · 4 comments · Fixed by #157

Comments

@tobiasdiez
Copy link
Contributor

Since its quite common to read the header strings, it would be nice if h3 could provide a helper function for this, which parses the headers and provides a structured way to access various common headers. For example, inspired by https://hapi.dev/module/accept/api/?v=5.0.2#parseallheaders one could have

parseHeaders(event).accept.mediaTypes
//  ["text/plain", "text/html", "application/json", "*/*"]
@NozomuIkuta
Copy link
Member

NozomuIkuta commented Aug 2, 2022

@tobiasdiez @pi0

If you have not yet started working on this feature, I can submit a PR. How?


By the way, I'm imagining composables like below so that H3 methods can be aligned:

// Request phase
const headers = useHeaders(event) // all keys are lower-cased as `req.getHeaders()`
const value = useHeader(event, name) // `name` is case-insensitive as `req.getHeader(name)`
headers[name.toLowerCase()] === value // true
const body = useBody(event)

// Response phase
appendHeader(event, name, value)
send(event, data, type)
sendStream(event, data)

We might want to make useHeaders and useHeader available in response phase (i.e. on req) as well, by either making them kinda generic or providing dedicated composables like useReqHeader(s) and useResHeader(s) for easy typing.

@NozomuIkuta
Copy link
Member

It seems that useBody has been deprecated in the code base.

h3/src/utils/body.ts

Lines 75 to 76 in 56bfe0a

/** @deprecated Use `h3.readBody` */
export const useBody = readBody

So, readHeader(s) or parseHeader(s) would be more suitable.

@pi0
Copy link
Member

pi0 commented Aug 2, 2022

Sure thing @NozomuIkuta. I think we can introduce getHeaders(event), getHeader(event, name) and setHeader(event, name, value) and maybe appendHeader(event, name, value) utils.

@tobiasdiez
Copy link
Contributor Author

Thanks @NozomuIkuta! These helper methods are good to have.

Strictly speaking, my proposal/idea went a bit further and included a structured access to the headers, i.e. parse them:

// Instead of
getHeader(event, "Content-Type")
// returns string: text/html; charset=UTF-8

// One has
getHeader(event, "Content-Type")
// returns object: {mediaTypes: ["text/html"], charsets: ["UTF-8"]}

Don't you think it makes sense and be helpful for devs to have this structured access?

This issue was closed.
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 a pull request may close this issue.

3 participants