-
Notifications
You must be signed in to change notification settings - Fork 621
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
fix(http): less restrictive arguments for accepts*()
functions
#5850
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #5850 +/- ##
=======================================
Coverage 96.26% 96.26%
=======================================
Files 479 479
Lines 38751 38738 -13
Branches 5620 5621 +1
=======================================
- Hits 37302 37291 -11
+ Misses 1406 1404 -2
Partials 43 43 ☔ View full report in Codecov by Sentry. |
Great point, but shouldn't it be |
Yeah, it'd make much more sense if these functions simply accepted |
That is a breaking API change with little ergonomic upside. Prior to 1.0 it accepted an object with a headers property, now it is restrictive. The least disruptive change would be to revert to the behaviour prior to 1.0. |
http/negotiation.ts
Outdated
* which allows for the working with objects which may not be strictly `Request` | ||
* objects. | ||
*/ | ||
export type Headered = { headers: { get(name: string): string | null } }; |
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.
@kitsonk
A question about some details. Can this be { headers: Headers }
in your use case?
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.
I'd prefer this be Headers
too.
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.
It feels a bit undesirable to expose this type as a public API. If it's ok to be { headers: Headers }
, maybe we can use Pick<Request, "headers">
type instead of Headered
for function parameter types
accepts*()
functions
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.
While I think these functions should've accepted headers: Headers
from the start, it's true that we can't make such changes post-1.0, for the sake of stability. I've changed this to go with Yoshiya's suggestion of using Pick<Request, "headers">
.
Either way, thank you, Kitson.
I think we also could do export function acceptsLanguages(headers: Request | Headers, ...langs: string[]): string | string[] | undefined {
headers = headers instanceof Request ? headers.headers : headers
...
} This avoids object wrapping but also doesn't break the code. |
I think the path of least action is what this PR currently does. It's ok how it is now. |
Currently the http negotiation methods are arbitrarily restrictive on what objects can be passed to the methods. It requires a Fetch API request object, when in fact it only requires a very specific type in the code. Setting overly restrictive types reduces compatibility and should be avoided.
Prior to 1.0 of std/http this was a case, but was changed for some reason causing downstream regressions in dependent code. Please consider expressing minimally restrictive types that actually reflect what the code is dependent upon.