Skip to content

Add URL to RequestInfo typedef - #1269

Merged
github-actions[bot] merged 4 commits into
microsoft:mainfrom
LiviaMedeiros:fix-requestinfo
Mar 2, 2022
Merged

github-actions[bot] merged 4 commits into
microsoft:mainfrom
LiviaMedeiros:fix-requestinfo

Conversation

@LiviaMedeiros

Copy link
Copy Markdown
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

Deleted user (ghost) commented Feb 24, 2022 •

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the PR!

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

@saschanaz

Copy link
Copy Markdown
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 Therox (@orta), thoughts?

@orta

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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.

Comment thread src/build/emitter.ts Outdated
if (p.name.toLowerCase().includes("url") && p.type === "USVString") {
if (
(p.name.toLowerCase().includes("url") && p.type === "USVString") ||
p.type === "RequestInfo"

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

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)

@orta Orta Therox (orta) left a comment •

Copy link
Copy Markdown
Contributor

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

interface Cache {
add(request: RequestInfo): Promise<void>;
add(request: RequestInfo | URL): Promise<void>;
addAll(requests: RequestInfo[]): Promise<void>;

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

lgtm

@github-actions
github-actions Bot merged commit a4e6873 into microsoft:main Mar 2, 2022
@github-actions

github-actions Bot commented Mar 2, 2022

Copy link
Copy Markdown
Contributor

Merging because Kagami Sascha Rosylight (@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