Skip to content

Commit

Permalink
fix: other cli fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dalechyn committed Nov 9, 2024
1 parent 7a1facd commit 0d7ce14
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Cli/Commands/Generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ export async function generate(options: Generate = {}) {
isTypeScript
? dedent`
import type { Actions } from 'fhub'
import { useMutation, type MutationOptions } from '@tanstack/react-query'
import { useMutation, type UseMutationOptions } from '@tanstack/react-query'
`
: "import { useMutation } from '@tanstack/react-query'"
}
export function ${outputHook}({mutation = {}}${
isTypeScript
? `: {
mutation?: MutationOptions<Actions.${namespaceName}.${functionName}.ReturnType, Actions.${namespaceName}.${functionName}.ErrorType, Actions.${namespaceName}.${functionName}.ParametersType> | undefined
mutation?: UseMutationOptions<Actions.${namespaceName}.${functionName}.ReturnType, Actions.${namespaceName}.${functionName}.ErrorType, Actions.${namespaceName}.${functionName}.ParametersType> | undefined
}`
: ''
}) {
Expand All @@ -157,7 +157,7 @@ export async function generate(options: Generate = {}) {
isTypeScript
? dedent`
import type { Actions } from 'fhub'
import { useQuery, type QueryOptions } from '@tanstack/react-query'
import { useQuery, type UseQueryOptions } from '@tanstack/react-query'
`
: "import { useQuery } from '@tanstack/react-query'"
}
Expand All @@ -171,13 +171,21 @@ export async function generate(options: Generate = {}) {
export function ${outputHook}({query = {}, args}${
isTypeScript
? `: {
query?: QueryOptions<Actions.${namespaceName}.${functionName}.ReturnType, Actions.${namespaceName}.${functionName}.ErrorType, Actions.${namespaceName}.${functionName}.ReturnType, QueryKey> | undefined
query?: Omit<UseQueryOptions<Actions.${namespaceName}.${functionName}.ReturnType, Actions.${namespaceName}.${functionName}.ErrorType, Actions.${namespaceName}.${functionName}.ReturnType, QueryKey>, 'queryKey'> | undefined
args?: Actions.${namespaceName}.${functionName}.ParametersType | undefined
}`
: ''
}) {
const enabled = Boolean(args && (query.enabled ?? true))
return useQuery({ ...query, queryKey: queryKey(args), queryFn: ({queryKey:[_, args]})=> action(args), enabled })
return useQuery({
...query,
queryKey: queryKey(args),
queryFn: ({queryKey:[_, args]}) => {
if (args === undefined) throw new Error('Missing args')
return action(args)
},
enabled
})
}
`)
})()
Expand Down

0 comments on commit 0d7ce14

Please sign in to comment.