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 generics to ky() and HTTPError #619

Merged
merged 5 commits into from
Aug 17, 2024

Conversation

sholladay
Copy link
Collaborator

@sholladay sholladay commented Aug 13, 2024

Closes #615
Closes #485
Relates to #312

This PR adds support for an optional TypeScript generic type parameter in all request methods, passes it through the request, response promise, and response, and into .json(), so that users have more flexibility in where and how they type their JSON objects.

All of the following forms are supported:

  • ky<T>()
  • ky.get<T>() (all methods)
  • ky().json<T>()
  • (await ky()).json<T>()

The HTTPError class has similarly been updated and passes its type parameter through to error.response.json(). See #615 for an example of how this could be used.

Relates to sindresorhus#312

All of the following forms are supported:
 - `ky<T>()`
 - `ky.get<T>()` (all methods)
 - `ky().json<T>()`
 - `(await ky()).json<T>()`
@sholladay sholladay changed the title Add JSON generics to ky() and ky.get(), etc. Add JSON generics to ky() and HTTPError Aug 13, 2024
Copy link

@nielsdB97 nielsdB97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks for your efforts on getting this done 😄

@sindresorhus sindresorhus merged commit f76c7cd into sindresorhus:main Aug 17, 2024
1 check passed
@mfulton26
Copy link
Contributor

mfulton26 commented Aug 17, 2024

This is neat. What do you think of adding a second generic type parameter for the expected Error payload too?

try {
  // user is a User
  const user = await ky<User, UserGetError>('/api/users/2').json();
} catch (exception) {
  if (exception instanceof HTTPError) {
    // error is UserGetError
    const error = await exception.response.json();
  }
}

@sholladay sholladay deleted the easy-generics branch August 17, 2024 17:24
@sholladay
Copy link
Collaborator Author

Seems like a good idea. 👍

@mfulton26
Copy link
Contributor

Oh, it might already be there, kind of. Plus, I don't think there's a way in TypeScript to propagate a generic type parameter from a method call through to an exception that is thrown and it show up in the catch() block.

I think this works as is though:

try {
    // user is a User
    const user = await ky<User>("/api/users/2").json();
} catch (exception) {
    if (exception instanceof HTTPError) {
        // exception is UserGetError
        const error = await exception.response.json<UserGetError>();
    }
}

In fact, I see that <T = unknown> was added to HTTPError. Is that usable though?

@mfulton26
Copy link
Contributor

I see that <T = unknown> was added to HTTPError. Is that usable though?

I suppose when creating instances manually it is.

@sholladay
Copy link
Collaborator Author

It was added because of #615. See that issue for how and why you might use the generic for HTTPError.

And if TypeScript ever does allow functions to type their exceptions, HTTPError will be ready for it.

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.

Add generic for HTTPError to type the response improve documentation for generic parameters in README
4 participants