orval icon indicating copy to clipboard operation
orval copied to clipboard

Generating more than one `useShowPetById` function, bugs or feature?

Open efriandika opened this issue 6 months ago • 5 comments

Hello,

I am new in this awesome library.

I just follow the config from this docs with the same api specs.

It's generating 4 useShowPetById function. 1 the correct function (in my opinion) and another 3 are just signature without body. See the generated code below.

Is this something expected, if yes why? or just a bug?

/**
 * @summary Info for a specific pet
 */
export const showPetById = (petId: string, options?: AxiosRequestConfig): Promise<AxiosResponse<Pet>> => {
  return axios.default.get(`/pets/${petId}`, options);
};

export const getShowPetByIdQueryKey = (petId: string) => {
  return [`/pets/${petId}`] as const;
};

export const getShowPetByIdQueryOptions = <TData = Awaited<ReturnType<typeof showPetById>>, TError = AxiosError<Error>>(
  petId: string,
  options?: { query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof showPetById>>, TError, TData>>; axios?: AxiosRequestConfig },
) => {
  const { query: queryOptions, axios: axiosOptions } = options ?? {};

  const queryKey = queryOptions?.queryKey ?? getShowPetByIdQueryKey(petId);

  const queryFn: QueryFunction<Awaited<ReturnType<typeof showPetById>>> = ({ signal }) => showPetById(petId, { signal, ...axiosOptions });

  return { queryKey, queryFn, enabled: !!petId, ...queryOptions } as UseQueryOptions<Awaited<ReturnType<typeof showPetById>>, TError, TData> & {
    queryKey: DataTag<QueryKey, TData, TError>;
  };
};

export type ShowPetByIdQueryResult = NonNullable<Awaited<ReturnType<typeof showPetById>>>;
export type ShowPetByIdQueryError = AxiosError<Error>;

export function useShowPetById<TData = Awaited<ReturnType<typeof showPetById>>, TError = AxiosError<Error>>(
  petId: string,
  options: {
    query: Partial<UseQueryOptions<Awaited<ReturnType<typeof showPetById>>, TError, TData>> &
      Pick<DefinedInitialDataOptions<Awaited<ReturnType<typeof showPetById>>, TError, Awaited<ReturnType<typeof showPetById>>>, 'initialData'>;
    axios?: AxiosRequestConfig;
  },
  queryClient?: QueryClient,
): DefinedUseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> };
export function useShowPetById<TData = Awaited<ReturnType<typeof showPetById>>, TError = AxiosError<Error>>(
  petId: string,
  options?: {
    query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof showPetById>>, TError, TData>> &
      Pick<UndefinedInitialDataOptions<Awaited<ReturnType<typeof showPetById>>, TError, Awaited<ReturnType<typeof showPetById>>>, 'initialData'>;
    axios?: AxiosRequestConfig;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> };
export function useShowPetById<TData = Awaited<ReturnType<typeof showPetById>>, TError = AxiosError<Error>>(
  petId: string,
  options?: { query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof showPetById>>, TError, TData>>; axios?: AxiosRequestConfig },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> };
/**
 * @summary Info for a specific pet
 */

export function useShowPetById<TData = Awaited<ReturnType<typeof showPetById>>, TError = AxiosError<Error>>(
  petId: string,
  options?: { query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof showPetById>>, TError, TData>>; axios?: AxiosRequestConfig },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> } {
  const queryOptions = getShowPetByIdQueryOptions(petId, options);

  const query = useQuery(queryOptions, queryClient) as UseQueryResult<TData, TError> & { queryKey: DataTag<QueryKey, TData, TError> };

  query.queryKey = queryOptions.queryKey;

  return query;
}

efriandika avatar May 13 '25 14:05 efriandika

Is there an issue? I think all the code is correct

They are overloaded depending on your usage and use case

melloware avatar May 17 '25 01:05 melloware

I am just curious. in the documentation, with the same config, it has no overloading.

Is the example in the documentation outdated?

efriandika avatar May 17 '25 22:05 efriandika

@anymaniax @AllieJonsson @soartec-lab do any of you know the answer to this?

melloware avatar May 17 '25 22:05 melloware

I think the documentation is outdated

AllieJonsson avatar May 18 '25 11:05 AllieJonsson

Good first issue if you want to submit a PR @efriandika

melloware avatar May 18 '25 15:05 melloware