Skip to content

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

Open
@YoavHortman

Description

@YoavHortman

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions