-
-
Notifications
You must be signed in to change notification settings - Fork 36.6k
doc: improve http.setHeader and getHeader typeinfo #19902
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
Changes from 2 commits
3699198
32685cf
1d5c3e2
5dc2a5d
3ab5dc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -587,13 +587,21 @@ added: v1.6.0 | |
| --> | ||
|
|
||
| * `name` {string} | ||
| * Returns: {string} | ||
| * Returns: {string|string[]} | ||
|
|
||
| Reads out a header on the request. Note that the name is case insensitive. | ||
| The type of the return value depends on the arguments provided to | ||
| [`response.setHeader()`][]. | ||
|
|
||
| Example: | ||
| ```js | ||
| const contentType = request.getHeader('Content-Type'); | ||
| request.setHeader('content-type', 'text/html'); | ||
| request.setHeader('Content-Length': Buffer.byteLength(body)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| request.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing semicolon. |
||
| const contentType = request.getHeader('Content-Type'); // contentType is 'text/html' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We lint code examples in docs, so these 3 lines will violate |
||
| const contentLength = request.getHeader('Content-Length'); // contentLength is of type number | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is true, should the parameter types be updated? Can we define the whole possible set there?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it's javascript so mostly everything is coerced to a string (even
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So maybe we can use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Works for me. Easy enough to update it to something else if we discover this confuses a lot of people. I don't think it will, though. |
||
| const setCookie = request.getHeader('set-cookie'); // setCookie is of type string[] | ||
|
|
||
| ``` | ||
|
|
||
| ### request.removeHeader(name) | ||
|
|
@@ -1210,7 +1218,8 @@ added: v0.4.0 | |
|
|
||
| Sets a single header value for implicit headers. If this header already exists | ||
| in the to-be-sent headers, its value will be replaced. Use an array of strings | ||
| here to send multiple headers with the same name. | ||
| here to send multiple headers with the same name. Non-string values will be | ||
| coerced to strings. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-string values will be coerced to strings in order to transmit them.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are coerced to strings already during set in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooof, I see the subtlety here now. Maybe be super explicit? Non-string values will be stored without modification. Therefore, `getHeader()`
may return non-string values. However, the non-string values will be converted
to strings for network transmission.Something like that maybe (but wrapped at 80 chars instead whatever I'm wrapping at above)? |
||
|
|
||
| Example: | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
* Returns: {any}? BecauseUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use
anyalso forsetHeaderthen?It is allowed and works as long as it translates to a
stringon the fly. It would be strange to saygetHeaderreturns what was provided tosetHeaderbut doc sayssetHeaderaccepts onlystring|string[]butgetHeaderreturnsany.Something like
stringifyablewould be best match but this is no type.