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

TS: Ability to add expected type for fetch body. #392

Open
YoavHortman opened this issue Dec 27, 2023 · 1 comment
Open

TS: Ability to add expected type for fetch body. #392

YoavHortman opened this issue Dec 27, 2023 · 1 comment

Comments

@YoavHortman
Copy link

Currently useFetch only allows for one generic argument - the response body.

I suggest adding an optional second argument for the request body.

When using libraries like zod it's very useful to be able to strongly type the request body so that when it changes you don't have to go hunting, the issues will immediately pop up.

Below is an example of how I currently get around this constraint:

export const useTypedBodyFetch = <T, B extends BodyInit | object>(HOST: string) => {
  const all = useFetch<T>(HOST);

  const typedPost = (route: string, body: B) => {
    return all.post(route, body);
  };

  return { ...all, post: typedPost };
};

I think it would make to include this in the library.

Thanks.

@alex-cory
Copy link
Collaborator

Feel free to submit a PR! :)

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

2 participants