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

Add JSON convenience methods #35

Closed
jodiedunlop opened this issue Sep 24, 2018 · 2 comments
Closed

Add JSON convenience methods #35

jodiedunlop opened this issue Sep 24, 2018 · 2 comments

Comments

@jodiedunlop
Copy link

To simplify json calls, which is probably the major use-case of this library it might be nice to provide some wrapper methods which simplify the api calls even further for the common get, put, patch, delete http verbs.

Eg.

const json = await ky.getJson('https://some-api.com');
const json = await ky.postJson('https://some-api.com', {foo: true});

These would obviously just be some syntatic sugar around the existing json examples, but imo it would make the api even nicer to use.

@szmarczak
Copy link
Collaborator

The readme says:

Returns a Response object with Body methods added for convenience. So you can, for example, call ky.json() directly on the Response without having to await it first

@sindresorhus
Copy link
Owner

To make it clear, you won't save many characters with your proposal. 1 character to be exact. You can't really pass a plain object into the second one as we need to support options too. So would be:

const json = await ky.getJson('https://some-api.com');
const json = await ky.postJson('https://some-api.com', {json: {foo: true}});

vs. Ky now:

const json = await ky('https://some-api.com').json();
const json = await ky.post('https://some-api.com', {json: {foo: true}}).json();

The current Ky syntax is almost the same size, but is much more flexible. You can decide to send JSON, but receive something else, all without having to learn a different API.

@sindresorhus sindresorhus changed the title Adding json methods Add JSON convenience methods Oct 5, 2018
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

No branches or pull requests

3 participants