Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ayrton
Copy link
Contributor

@ayrton ayrton commented Dec 18, 2023

Stronger type safety through strict checking

src/index.ts Outdated
if (typeof fetch !== 'undefined') {
this.config.fetch ||= fetch
if (!config.fetch && typeof fetch === 'undefined') {
throw new Error('No `fetch` implementation available')
Copy link
Member

Choose a reason for hiding this comment

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

To me, this is the only thing that seems like change in behavior.

So i understand, before if there was no fetch passed into the config and no global fetch available, we'd have not failed in the constructor, but failed later inside of postJSON when we tried to execute the undefined thing. Correct?

But our change here catches this in the constructor and yells before trying to use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. These days it's unlikely there's no fetch available but rather than have an internal Node exception bubble up. I wanted to fail fast with a clear exception message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed this a little in ecae723.

this.fetch = config.fetch || fetch!

This will raise a Uncaught ReferenceError: fetch is not define - which is easily googlable

@ayrton ayrton merged commit 07a2329 into main Jan 26, 2024
@ayrton ayrton deleted the strict-check branch January 26, 2024 16:47
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.

3 participants