Skip to content

Add data type to request and axios optional param for both custom mutator and axios output httpClient #2041

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

Open
matheus-oliveira-unity opened this issue Apr 17, 2025 · 0 comments
Labels
axios Axios related issue tanstack-query TanStack Query related issue

Comments

@matheus-oliveira-unity
Copy link

matheus-oliveira-unity commented Apr 17, 2025

docs for reference.

When creating a custom mutator functions with a generic data param (e.g. const customInstance = <T>) the dynamic type is not defined on any param of the generated code.

for instance, the function that fetches data has the following param:

 options?: SecondParameter<typeof customInstance>,

but ideally, it should look like this:

 options?: SecondParameter<typeof customInstance<TodoResponse>>,

this has a cascading effect, which the query and query option functions also need to have the data type.

for a more detailed example, I have this custom mutator:

export interface RequestOptions<T = any> { 
   notify: (data: T) => void 
}
export const customInstance = <T>(config: AxiosRequestConfig<T>,  options?: RequestOptions<T>): Promise<T> => { ... }

the ideal result should look something like this:

export const getTodoById = (
  todoId: string,
  params?: GetTodoByIdParams,
    // include the data type
  options?: SecondParameter<typeof customInstance<TodoResponse>>,
  signal?: AbortSignal
) => {
    return customInstance<TodoResponse>({
      url: `/todos/${encodeURIComponent(String(todoId))}`,
       method: 'GET', 
       params, 
       signal
    }, options);
  }
  
export const getGetTodoByIdQueryOptions = <TData = Awaited<ReturnType<typeof getTodoById>>, TError = ErrorType<GetTodoById500>>(
  todoId: string,
  params?: GetTodoByIdParams, 
  options?: { 
    query?:UseQueryOptions<Awaited<ReturnType<typeof getTodoById>>, TError, TData>, 
    // include the returned data type
    request?:  SecondParameter<typeof customInstance<<Awaited<ReturnType<typeof getTodoById>>>>>
  }
) => { ... }

export function useGetTodoById<TData = Awaited<ReturnType<typeof getTodoById>>, TError = ErrorType<GetTodoById401>>>(
  todoId: string,
  params?: GetTodoByIdIdParams, 
  options?: { 
    query?:UseQueryOptions<Awaited<ReturnType<typeof getTodoById>>, TError, TData>, 
    // include the returned data type
    request?: SecondParameter<typeof customInstance<<Awaited<ReturnType<typeof getTodoById>>>>>
  }

the same goes when output httpClient is set to axios
it create the config without the data type:

axios?: AxiosRequestConfig

but ideally it should also include the data type:

axios?: AxiosRequestConfig<TodoResponse>

this could potentially break existing code, so maybe there should be a configuration options for it as well?

Thanks

@melloware melloware added tanstack-query TanStack Query related issue axios Axios related issue labels Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
axios Axios related issue tanstack-query TanStack Query related issue
Projects
None yet
Development

No branches or pull requests

2 participants