generated from stijnvanhulle/template
-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
Labels
Comments
Proposal: import client from '@kubb/plugin-client/clients/axios'
import useClient from './path'
import type { AddPetMutationRequest, AddPetMutationResponse, AddPet405 } from '../../models/AddPet.ts'
import type { RequestConfig, ResponseErrorConfig } from '@kubb/plugin-client/clients/axios'
import type { UseMutationOptions } from '@tanstack/react-query'
import { useMutation } from '@tanstack/react-query'
export const addPetMutationKey = () => [{ url: '/pet' }] as const
export type AddPetMutationKey = ReturnType<typeof addPetMutationKey>
/**
* @description Add a new pet to the store
* @summary Add a new pet to the store
* {@link /pet}
*/
export async function addPetHook(data: AddPetMutationRequest, config: Partial<RequestConfig<AddPetMutationRequest>> = {}) {
const res = await client<AddPetMutationResponse, ResponseErrorConfig<AddPet405>, AddPetMutationRequest>({ method: 'POST', url: `/pet`, data, ...config })
return res.data
}
async function addPetHookFactory(_client: typeof client) {
return async function addPetHook(data: AddPetMutationRequest, config: Partial<RequestConfig<AddPetMutationRequest>> = {}) {
const res = await _client<AddPetMutationResponse, ResponseErrorConfig<AddPet405>, AddPetMutationRequest>({ method: 'POST', url: `/pet`, data, ...config })
return res.data
}
}
/**
* @description Add a new pet to the store
* @summary Add a new pet to the store
* {@link /pet}
*/
export function useAddPetHook(
options: {
mutation?: UseMutationOptions<AddPetMutationResponse, ResponseErrorConfig<AddPet405>, { data: AddPetMutationRequest }>
client?: Partial<RequestConfig<AddPetMutationRequest>>
} = {},
) {
const { mutation: mutationOptions, client: config = {} } = options ?? {}
const mutationKey = mutationOptions?.mutationKey ?? addPetMutationKey()
const client = useClient(config)
return useMutation<AddPetMutationResponse, ResponseErrorConfig<AddPet405>, { data: AddPetMutationRequest }>({
mutationFn: async ({ data }) => {
return addPetHookFactory(client)(data, config)
},
mutationKey,
...mutationOptions,
})
} |
@luixo Version For example
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
I want it be changed to:
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.
The text was updated successfully, but these errors were encountered: