Skip to content

Default empty arguments #37

@seriouslag

Description

@seriouslag

It would be nice if the arguments for the generated hooks had a default value if all the properties of the argument can be undefined.

Example of generated hook as is:

export const useCatsServiceGetCats = <
  TData = CatServiceGetCatsDefaultResponse,
  TError = unknown,
  TQueryKey extends Array<unknown> = unknown[]
>(
  {
    color,
    age,
  }: {
    color?: string;
    age?: number;
  },
  queryKey?: TQueryKey,
  options?: Omit<
    UseQueryOptions<TData, TError>,
    "queryKey" | "queryFn" | "initialData"
  >
) =>
  useQuery<TData, TError>({
    queryKey: [
      useCatsServiceGetCatssKey,
      ...(queryKey ?? [{ color, age }]),
    ],
    queryFn: () =>
      CatsService.getCats(color, age) as TData,
    ...options,
  });
  
 // usage - need to pass empty object at least
 const {data} = useCatsServiceGetCats({});

Example of proposed generated code:

export const useCatsServiceGetCats = <
  TData = CatServiceGetCatsDefaultResponse,
  TError = unknown,
  TQueryKey extends Array<unknown> = unknown[]
>(
  {
    color,
    age,
  }: {
    color?: string;
    age?: number;
  } = {}, // this is defaulted since all properties can be undefined
  queryKey?: TQueryKey,
  options?: Omit<
    UseQueryOptions<TData, TError>,
    "queryKey" | "queryFn" | "initialData"
  >
) =>
  useQuery<TData, TError>({
    queryKey: [
      useCatsServiceGetCatssKey,
      ...(queryKey ?? [{ color, age }]),
    ],
    queryFn: () =>
      CatsService.getCats(color, age) as TData,
    ...options,
  });
 
 // usage - no need to pass empty object
const {data} = useCatsServiceGetCats();

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions