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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solving the json option confusion by replacing it with a parse option #264

Closed
wants to merge 11 commits into from

Conversation

alextes
Copy link
Contributor

@alextes alextes commented Jan 22, 2017

Solving the JSON option confusion

Preamble

You're looking at my first significant PR hooray 馃帀! Got is awesome and inspired me to submit it. I鈥檝e put a lot of thought into this PR and its code; nevertheless, the project comes first. Feel free to be completely honest. Also quite excited after spending hours creating and rebasing. Most commits have an elaborate description, I hope they will be a joy to read.

On to the PR!

Got wants to support parsing of responses and sending of encoded bodies. The current JSON option achieves one of these but seems to confuse people. After a lot of comparing libraries and thinking here鈥檚 what I believe we can do!

Summarizing from #174 and my thoughts here鈥檚 why the JSON option currently confuses:

  • It鈥檚 the only option that helps parse although you can parse a response many ways.
  • Some popular libraries parse based on headers by default.
  • Some popular libraries use JSON as the body encoding by default or have a shortcut for it.

I think from #174 the easiest solution is to bundle all parsing under one parse option and drop the confusing JSON option. To my thinking, this would lead to better expected behaviour (and is a little more flexible going forward).

But that鈥檚 not where it ends!

I'm thinking of a next PR that would add some nice default parsing based on the Content-Type header (that's what that鈥檚 for after all). Seems like a nice thing to do 馃槃. Might also make Got too complex, let me know what you think!

I also created #265 because - as I hinted earlier - I think the body encoding default is confusing people. I have added a few suggestions to ease use there too.

Removed the implementation. Removed option use from readme.
All these tests fail as a result of removing the json specific parse
option. Each test will be reviewed and updated to work
with the - to be implemented - general parse option.
Changes the json option flag into the new parse option. Does not touch
tests that require other modifications or are in the to-be-replaced
json test file.
This test flagged setting a body to something other than ReadableStream,
string, Buffer or plain object. Since we removed the json shortcut
you'll have to always stringify yourself which means you can no longer
set the body to a plain object, and so we won't suggest it in our type
error.
This test mainly signals the wider intention of the parse option. The
other tests use the JSON variant. It also tests a common scenario that
should work.
Since we are no longer sure what method of parsing the user is wanting
to use we can no longer automagically set an Accept header for you.
Adds the suggested parse option. Adds a warning explaining the parse
option cannot be used in combination with got's stream mode. Sets all
tests to be expected to pass again.
@alextes
Copy link
Contributor Author

alextes commented Jan 23, 2017

Just noticed there was a misguided commit in there. Dropped it and rewrote history to improve the PR. I think we can improve the test that confused me. The details will be in the PR under this comment.

@sindresorhus
Copy link
Owner

Very good PR and reasoning @alextes. Sorry not getting to this sooner.

I'm not entirely sold on the change itself though. The reason we added a json option is that JSON is the 95% use-case and we wanted to optimize for that. I've honestly never used any other encoding than plain and JSON. I would rather want to fix any confusion with the JSON option than replacing it with something more flexible.

@alextes
Copy link
Contributor Author

alextes commented May 3, 2017

Fair enough @sindresorhus. It's 馃槩 to close my first PR but I'll get over it 馃槈. I knew this chance as there from the beginning when I chose not to discuss my solution first. It was a fun and good way to familiarize myself with got.
Thanks for looking over the long PR and helping me in merging many other PRs by now.

@alextes alextes closed this May 3, 2017
@alextes alextes deleted the parse-option branch May 3, 2017 16:08
@sindresorhus
Copy link
Owner

Yeah, always hard to close PRs that had a lot of work gone into them, but that's why opening an issue first is often beneficial.

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

2 participants