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

Change stringifying options behavior #297

Merged
merged 13 commits into from
May 6, 2017

Conversation

alextes
Copy link
Contributor

@alextes alextes commented May 4, 2017

Changes the behavior of the stringifying options.

  • don't accept plain object unless form or json is true
  • stringify if json is true
  • stringify if form is true
  • stringify as querystring and parse res as JSON if form and json are true

Things I changed because I found the code hard to deal with, and confusing without.

  • clean up input validation using Sindre's suggestion from Request body can't be querystring.parse(str) #212.
  • simpler use of 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.
  • 'throws on bad input' test is maybe overkill

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:

  • make 'body is string' case explicit instead of implicit fallback
  • use early returns to limit code paths and sharpen / reduce redundant input checking
  • some tests use destructuring others don't for the exact same response property, could use more consistency.

Fixes #174

alextes and others added 10 commits May 4, 2017 01:47
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.
Copy link
Owner

@sindresorhus sindresorhus left a 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 🌈🥑

@sindresorhus
Copy link
Owner

@floatdrop What do you think of these changes? See my proposal in #174 (comment)

@sindresorhus
Copy link
Owner

sindresorhus commented May 5, 2017

I'm considering a second PR. Specifically what I feel could be better:

👍 Wait until this lands first though.

@alextes
Copy link
Contributor Author

alextes commented May 5, 2017

I got an avocado rainbow 😲 ✊ ❤️!
(I'll wait 😁)

index.js Outdated
}

opts.method = opts.method || 'POST';
if ((opts.form || opts.json) && !(isObj(body) && typeof body !== 'function')) {
Copy link
Contributor

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.

Copy link
Contributor Author

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');
Copy link
Contributor

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';
Copy link
Contributor

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.

@sindresorhus sindresorhus merged commit 51a3eaf into sindresorhus:master May 6, 2017
@sindresorhus
Copy link
Owner

Thank you for yet another excellent pull request @alextes. I'd like to invite you to join the project if you're interested? 🙏

@alextes
Copy link
Contributor Author

alextes commented May 6, 2017

@sindresorhus 🙇 🙇 . You just made my day. And it's only 10 AM!
Here we come v7!

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.

None yet

3 participants