-
-
Notifications
You must be signed in to change notification settings - Fork 957
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
Change stringifying options behavior #297
Conversation
Soon the json option will also stringify. All tests that currently rely on the 'parse JSON res option' but don't shouldn't stringify their body as JSON have to be updated.
Since it only deals with parsing. Stringifying tests are in test/post.js
Removes falling back to stringifying plain objects as querystrings. Cleans up general input validation. Adds form and json option input validation. Limits the use of `body` shorthand to reading only. Moves method setting and fallback closer together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks super to me 🌈🥑
@floatdrop What do you think of these changes? See my proposal in #174 (comment) |
👍 Wait until this lands first though. |
I got an avocado rainbow 😲 ✊ ❤️! |
index.js
Outdated
} | ||
|
||
opts.method = opts.method || 'POST'; | ||
if ((opts.form || opts.json) && !(isObj(body) && typeof body !== 'function')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is typeof body !== 'funciton'
is necessary? Looks like isPlainObj(function() {})
returns false
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch. I was confusing is-obj
and is-plain-obj
.
index.js
Outdated
} | ||
|
||
opts.method = opts.method || 'POST'; | ||
if ((opts.form || opts.json) && !(isObj(body) && typeof body !== 'function')) { | ||
throw new TypeError('options.body must be a plain Object'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
options.body must be a plain Object when options.form or options.json is used
index.js
Outdated
|
||
opts.method = (opts.method || 'GET').toUpperCase(); | ||
opts.method = opts.method || 'POST'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opts.method
should be uppercased in this if-branch too.
Thank you for yet another excellent pull request @alextes. I'd like to invite you to join the project if you're interested? 🙏 |
@sindresorhus 🙇 🙇 . You just made my day. And it's only 10 AM! |
Changes the behavior of the stringifying options.
form
orjson
is truejson
is trueform
is trueres
as JSON ifform
andjson
are trueThings I changed because I found the code hard to deal with, and confusing without.
body
variable. It's a shorthand for the body passed by the user.opts.body
is what we mutate into the body we want to send.As always details are in the commit messages.
Sidenotes: I went for minimal change based on the proposal from #174. I think this can be quite a bit cleaner still. I'm considering a second PR. Specifically what I feel could be better:
Fixes #174