The Wayback Machine - https://web.archive.org/web/20210725151257/https://github.com/node-fetch/node-fetch/issues/1000
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

Hint that using data is wrong #1000

Open
jimmywarting opened this issue Oct 28, 2020 · 7 comments
Open

Hint that using data is wrong #1000

jimmywarting opened this issue Oct 28, 2020 · 7 comments

Comments

@jimmywarting
Copy link
Collaborator

@jimmywarting jimmywarting commented Oct 28, 2020

From time to time i see ppl using data instead of body which is wrong.
happened just recently in #481 (comment)

how do you feel about adding a little warning message that can only appear once. (kinda like node reports that you are using experimental apis)

if ('data' in bodyInit && notWarned) {
  console.warn('use body instead')
}
class Response {
  get data () {
    notWarned && console.warn('you want to use .json(), .text(), .arrayBuffer() or just .body')
  }
}

I don't think it is uncommon to see those mistake happening when switching from different request libraries axios use data instead of body for example. I can admit that i have used data as well and wasted minutes trying to figure out what's wrong

@jimmywarting
Copy link
Collaborator Author

@jimmywarting jimmywarting commented Oct 28, 2020

Yay, i claimed issue 1000 🎉

@dork1
Copy link

@dork1 dork1 commented Oct 28, 2020

I am still learning all the ins and outs of various libraries and indeed my code was adapted from an axios example so an error like stated above would help loads in pinpointing the problem. Thank you! @jimmywarting

@tekwiz
Copy link
Member

@tekwiz tekwiz commented Dec 20, 2020

@jimmywarting I think this makes sense, but I would throw an Error because they're probably going to get one anyway when they try to use the undefined return value

@jimmywarting
Copy link
Collaborator Author

@jimmywarting jimmywarting commented Dec 21, 2020

I don't think an error should be thrown... it's not what is happening in (mostly) any method where you are allowed to pass in a object, other options are just ignored. it has the risk to break things as it wouldn't be backwards compatible.

You can get a request option from an arbitrary place that could include both body and data option and dosen't fully looks like a Request init option.

We try to align us self with how the whatwg fetch works in the browser and the spec. we never know the true intention if the developer really meant to post the data option or if it's just a extra arbitrary option that comes from somewhere else

edit ups, i was talking about the Request constructor... totally forgot that it was about the Response.data

But yea, a warning on the request constructor would also be helpful :P

when i think about it response.data could potentially throw a TypeError but new Request(x, { method: 'post', data }) should warn

@tekwiz
Copy link
Member

@tekwiz tekwiz commented Dec 21, 2020

@jimmywarting And oops for me, too... I was only thinking about Response.data. I agree with this, and I also agree now with what you were saying about warning only once:

when i think about it response.data could potentially throw a TypeError but new Request(x, { method: 'post', data }) should warn

@Richienb
Copy link
Member

@Richienb Richienb commented Dec 27, 2020

It's a good idea, but I'm not sure how far we should deviate from the original fetch spec.

@jimmywarting
Copy link
Collaborator Author

@jimmywarting jimmywarting commented Dec 27, 2020

It's a good idea, but I'm not sure how far we should deviate from the original fetch spec.

it's not like we are adding any new feature/method into fetch, it's just a way of helping/notifying that what you might be doing is wrong. such notification could easily be removed if we ever decide to ditch it again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants