Skip to content
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

Default empty arguments #37

Closed
seriouslag opened this issue Jan 4, 2024 · 2 comments
Closed

Default empty arguments #37

seriouslag opened this issue Jan 4, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@seriouslag
Copy link
Collaborator

seriouslag commented Jan 4, 2024

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();
@seriouslag seriouslag added enhancement New feature or request good first issue Good for newcomers labels Jan 4, 2024
@7nohe
Copy link
Owner

7nohe commented Jan 5, 2024

I also prefer that one 👍🏼

seriouslag added a commit that referenced this issue Apr 10, 2024
This PR adds a default parameter initializer if all the properties of the object are optional.

Fixes: Default empty arguments #37
@seriouslag seriouslag self-assigned this Apr 10, 2024
@seriouslag
Copy link
Collaborator Author

Closed by #60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants