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

useClient in @kubb/plugin-tanstack-query #1226

Open
luixo opened this issue Sep 20, 2024 · 0 comments
Open

useClient in @kubb/plugin-tanstack-query #1226

luixo opened this issue Sep 20, 2024 · 0 comments
Labels
enhancement New feature or request @kubb/plugin-react-query v3 Kubb v3
Milestone

Comments

@luixo
Copy link

luixo commented Sep 20, 2024

What is the problem this feature would solve?

I need to use multiple clients for the same set of useQuery / useMutation generated hooks.

External documents/projects?

No response

What is the feature you are proposing to solve the problem?

Currently I have generated code:

export function useFoo(
  options: {
    mutation?: UseMutationOptions<...>;
    client?: Foo['client']['parameters'];
  } = {},
) {
  const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {};
  return useMutation({
    mutationFn: async (data) => {
      const res = await client<...>({
        method: 'post',
        url: `...`,
        data,
        ...clientOptions,
      });
      return res.data;
    },
    ...mutationOptions,
  });
}

I want it be changed to:

// This should be either `useClient` or `client` depending on option
import { useClient, client } from "client-path";

export function useFoo(
  options: {
    mutation?: UseMutationOptions<...>;
    client?: Foo['client'];
  } = {},
) {
  const { mutation: mutationOptions, client: clientWithParams = {} } = options;
  const client = useClient(clientWithParams.options);
  return useMutation({
    mutationFn: async (data) => {
      const res = await (clientWithParams.return || client)<...>({
        method: 'post',
        url: `...`,
        data,
        ...clientWithParams.parameters,
      });
      return res.data;
    },
    ...mutationOptions,
  });
}
// tanstack-query plugin options
type Options = {
  //...
  client?: {
    importPath?: string
    importHook?: boolean; // if true - import `useClient` instead of `client`
  }
};

What alternatives have you considered?

Replacing the template.
It seems like the template is a rendered react component which means it's basically non-modifiable, I have to rewrite it from scratch.
It's easier to replace lines by regex after generating.

@luixo luixo added the enhancement New feature or request label Sep 20, 2024
@stijnvanhulle stijnvanhulle added this to the 3.0.0 milestone Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request @kubb/plugin-react-query v3 Kubb v3
Projects
None yet
Development

No branches or pull requests

2 participants