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

fix(http): less restrictive arguments for accepts*() functions #5850

Merged
merged 2 commits into from
Aug 29, 2024

Conversation

kitsonk
Copy link
Contributor

@kitsonk kitsonk commented Aug 28, 2024

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.

@kitsonk kitsonk requested a review from kt3k as a code owner August 28, 2024 06:22
@github-actions github-actions bot added the http label Aug 28, 2024
Copy link

codecov bot commented Aug 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.26%. Comparing base (2e209c0) to head (0bdffcc).
Report is 7 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

@timreichen
Copy link
Contributor

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.

Great point, but shouldn't it be Headers then that should be passed instead of wrapping them in an object?

@iuioiua
Copy link
Contributor

iuioiua commented Aug 28, 2024

Yeah, it'd make much more sense if these functions simply accepted Headers.

@kitsonk
Copy link
Contributor Author

kitsonk commented Aug 28, 2024

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.

* which allows for the working with objects which may not be strictly `Request`
* objects.
*/
export type Headered = { headers: { get(name: string): string | null } };
Copy link
Member

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?

Copy link
Contributor

@iuioiua iuioiua Aug 28, 2024

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.

Copy link
Member

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

@iuioiua iuioiua changed the title fix(http): less restrictive types fix(http): less restrictive arguments for accepts*() functions Aug 28, 2024
Copy link
Contributor

@iuioiua iuioiua left a 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.

@iuioiua iuioiua requested a review from kt3k August 28, 2024 23:04
@timreichen
Copy link
Contributor

timreichen commented Aug 28, 2024

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.

@iuioiua
Copy link
Contributor

iuioiua commented Aug 28, 2024

I think we also could do

export function acceptsLanguages(headers: Request | Headers, ...langs: string[]): string | string[] | undefined {
  headers = headers instanceof Request ? headers.headers : headers
  ...
}

to allow Headers but not break the code.

I think the path of least action is what this PR currently does. It's ok how it is now.

@iuioiua iuioiua merged commit b8df2b3 into denoland:main Aug 29, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants