Thanks to visit codestin.com
Credit goes to github.com

Skip to content

useFetch | Support for generics in payload, and add more specific type for payloadType in methods functions #5120

@hcdo7

Description

@hcdo7

Clear and concise description of the problem

I believe that when dealing with calls with a more complex body, it can be easy to make a mistake, so I think there could be a possibility to pass a pre-existing generic type when calling method functions.
In addition, there are Content-Type aliases that are specific to useFetch but are not typed (json and text).

Suggested solution

Allowing a generic type in functions similar to what already occurs in the json method, and the creation of a type for accepted types:

type PayloadType = 'json' | 'text' | (string & {})

export interface UseFetchReturn<T> {
  // methods
  get: () => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
  post: <P = unknown>(payload?: MaybeRefOrGetter<P>, type?: PayloadType) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
  put: <P = unknown>(payload?: MaybeRefOrGetter<P>, type?: PayloadType) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
  delete: <P = unknown>(payload?: MaybeRefOrGetter<P>, type?: PayloadType) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
  patch: <P = unknown>(payload?: MaybeRefOrGetter<P>, type?: PayloadType) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
  head: <P = unknown>(payload?: MaybeRefOrGetter<P>, type?: PayloadType) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
  options: <P = unknown>(payload?: MaybeRefOrGetter<P>, type?: PayloadType) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
}

This allows you to indicate the type of payload, if necessary, and there is an auto-fill for the type (while maintaining the possibility of any string).

Alternative

No response

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions