Skip to content

Add URL to RequestInfo typedef #1269

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

Merged
merged 4 commits into from
Mar 2, 2022

Conversation

LiviaMedeiros
Copy link
Contributor

Fixes: #1016

In most methods urlstring params are declared as string | URL, which ensures perfect balance between safe code and implicit casts allowed for USVString.

This PR aligns RequestInfo with them.
In particular, it allows to pass URL objects in fetch.

@ghost
Copy link

ghost commented Feb 24, 2022

CLA assistant check
All CLA requirements met.

@github-actions
Copy link
Contributor

Thanks for the PR!

This section of the codebase is owned by @saschanaz - if they write a comment saying "LGTM" then it will be merged.

@saschanaz
Copy link
Contributor

This sounds fair, but I wonder this could be breaking as some other libraries may be using RequestInfo but not the implicit URL stringification behavior. @orta, thoughts?

@orta
Copy link
Contributor

orta commented Feb 25, 2022

Yeah, I trust past me to have done enough checking to make the call that we shouldn't do this last year, #1016 (comment) (likely because of ecosystem breakage) and I don't think anything's changed sinced then

@LiviaMedeiros
Copy link
Contributor Author

Please clarify: you're against allowing any random objects like { toString(): string } from the issue (or even anything stringable, as USVString-related specs says), which is obviously not typesafe; or against URL instances in particular, which are allowed everywhere in TypeScript if param name in docs matches /url/i?

@saschanaz
Copy link
Contributor

Accepting URL for Web APIs is okay, what's not okay is to change the typedef and thus affect third-parties. I think changing the signatures of the APIs using RequestInfo is better.

if (p.name.toLowerCase().includes("url") && p.type === "USVString") {
if (
(p.name.toLowerCase().includes("url") && p.type === "USVString") ||
p.type === "RequestInfo"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now a little bit long to be in a if clause, could you add a function for this and call it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Contributor

@saschanaz saschanaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Orta, wdyt?

(I'd prefer something like acceptsUrl btw)

Copy link
Contributor

@orta orta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree, this is a good solution - good creative thinking folks

@@ -3232,13 +3232,13 @@ declare var CSSTransition: {
* Available only in secure contexts.
*/
interface Cache {
add(request: RequestInfo): Promise<void>;
add(request: RequestInfo | URL): Promise<void>;
addAll(requests: RequestInfo[]): Promise<void>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to treat this one 🤔 but okay for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to properly implement this in generator either.
But in my opinion, even something like RequestInfo[] | URL[] would be an okay trade-off here: mixing those in userland code should be rarely needed.

Another option is to refactor this later by introducing some urlstring and RequestInfoOrUrl typedefs. Dunno about downsides.

These should be affected as well:

interface Cache {
addAll(requests: Iterable<RequestInfo>): Promise<void>;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmmmm. Let's merge this and tackle this in a separate patch.

@saschanaz
Copy link
Contributor

lgtm

@github-actions github-actions bot merged commit a4e6873 into microsoft:main Mar 2, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2022

Merging because @saschanaz is a code-owner of all the changes - thanks!

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.

type RequestInfo should include URL
3 participants