From 30211fb218aa1c571f8f2cb8a9781f82a2abcffd Mon Sep 17 00:00:00 2001 From: Mikko Murto Date: Thu, 16 May 2024 12:48:23 +0300 Subject: [PATCH] fix: Use optional query keys also without parameters The query key functions for queries that don't take in additional request parameters don't allow for using additional query key input, but query hooks always take in a query key parameter. This leads to the query key parameter in hooks without additional input being unused variable, leading to TypeScript erroring. Fix this by always taking query key parameter in query key functions, and always using it in query hooks. Fixes #112. Signed-off-by: Mikko Murto --- src/createUseQuery.mts | 12 ++---------- tests/__snapshots__/createSource.test.ts.snap | 6 +++--- tests/__snapshots__/generate.test.ts.snap | 6 +++--- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/createUseQuery.mts b/src/createUseQuery.mts index ea91907..8eb919e 100644 --- a/src/createUseQuery.mts +++ b/src/createUseQuery.mts @@ -347,7 +347,7 @@ export function createQueryHook({ ), ts.factory.createIdentifier("queryKey"), ] - : [], + : [ts.factory.createIdentifier("queryKey")], ), ), ts.factory.createPropertyAssignment( @@ -490,7 +490,7 @@ function createQueryKeyFnExport(queryKey: string, method: MethodDeclaration) { ts.factory.createArrowFunction( undefined, undefined, - params ? [params, overrideKey] : [], + params ? [params, overrideKey] : [overrideKey], undefined, EqualsOrGreaterThanToken, queryKeyFn(queryKey, method), @@ -506,14 +506,6 @@ function queryKeyFn( queryKey: string, method: MethodDeclaration, ): ts.Expression { - const params = getRequestParamFromMethod(method); - - if (!params) { - return ts.factory.createArrayLiteralExpression([ - ts.factory.createIdentifier(queryKey), - ]); - } - return ts.factory.createArrayLiteralExpression( [ ts.factory.createIdentifier(queryKey), diff --git a/tests/__snapshots__/createSource.test.ts.snap b/tests/__snapshots__/createSource.test.ts.snap index ff7385b..dd528b0 100644 --- a/tests/__snapshots__/createSource.test.ts.snap +++ b/tests/__snapshots__/createSource.test.ts.snap @@ -24,7 +24,7 @@ export const UseDefaultServiceFindPetsKeyFn = ({ limit, tags }: { export type DefaultServiceGetNotDefinedDefaultResponse = Awaited>; export type DefaultServiceGetNotDefinedQueryResult = UseQueryResult; export const useDefaultServiceGetNotDefinedKey = "DefaultServiceGetNotDefined"; -export const UseDefaultServiceGetNotDefinedKeyFn = () => [useDefaultServiceGetNotDefinedKey]; +export const UseDefaultServiceGetNotDefinedKeyFn = (queryKey?: Array) => [useDefaultServiceGetNotDefinedKey, ...(queryKey ?? [])]; export type DefaultServiceFindPetByIdDefaultResponse = Awaited>; export type DefaultServiceFindPetByIdQueryResult = UseQueryResult; export const useDefaultServiceFindPetByIdKey = "DefaultServiceFindPetById"; @@ -67,7 +67,7 @@ export const useDefaultServiceFindPets = = unknown[]>(queryKey?: TQueryKey, options?: Omit, "queryKey" | "queryFn">) => useQuery({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options }); +export const useDefaultServiceGetNotDefined = = unknown[]>(queryKey?: TQueryKey, options?: Omit, "queryKey" | "queryFn">) => useQuery({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(queryKey), queryFn: () => DefaultService.getNotDefined() as TData, ...options }); /** * Returns a user based on a single ID, if the user does not have access to the pet * @param data The data for the request. @@ -145,7 +145,7 @@ export const useDefaultServiceFindPetsSuspense = = unknown[]>(queryKey?: TQueryKey, options?: Omit, "queryKey" | "queryFn">) => useSuspenseQuery({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options }); +export const useDefaultServiceGetNotDefinedSuspense = = unknown[]>(queryKey?: TQueryKey, options?: Omit, "queryKey" | "queryFn">) => useSuspenseQuery({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(queryKey), queryFn: () => DefaultService.getNotDefined() as TData, ...options }); /** * Returns a user based on a single ID, if the user does not have access to the pet * @param data The data for the request. diff --git a/tests/__snapshots__/generate.test.ts.snap b/tests/__snapshots__/generate.test.ts.snap index 7b7c14d..4aed6c0 100644 --- a/tests/__snapshots__/generate.test.ts.snap +++ b/tests/__snapshots__/generate.test.ts.snap @@ -15,7 +15,7 @@ export const UseDefaultServiceFindPetsKeyFn = ({ limit, tags }: { export type DefaultServiceGetNotDefinedDefaultResponse = Awaited>; export type DefaultServiceGetNotDefinedQueryResult = UseQueryResult; export const useDefaultServiceGetNotDefinedKey = "DefaultServiceGetNotDefined"; -export const UseDefaultServiceGetNotDefinedKeyFn = () => [useDefaultServiceGetNotDefinedKey]; +export const UseDefaultServiceGetNotDefinedKeyFn = (queryKey?: Array) => [useDefaultServiceGetNotDefinedKey, ...(queryKey ?? [])]; export type DefaultServiceFindPetByIdDefaultResponse = Awaited>; export type DefaultServiceFindPetByIdQueryResult = UseQueryResult; export const useDefaultServiceFindPetByIdKey = "DefaultServiceFindPetById"; @@ -110,7 +110,7 @@ export const useDefaultServiceFindPets = = unknown[]>(queryKey?: TQueryKey, options?: Omit, "queryKey" | "queryFn">) => useQuery({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options }); +export const useDefaultServiceGetNotDefined = = unknown[]>(queryKey?: TQueryKey, options?: Omit, "queryKey" | "queryFn">) => useQuery({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(queryKey), queryFn: () => DefaultService.getNotDefined() as TData, ...options }); /** * Returns a user based on a single ID, if the user does not have access to the pet * @param data The data for the request. @@ -187,7 +187,7 @@ export const useDefaultServiceFindPetsSuspense = = unknown[]>(queryKey?: TQueryKey, options?: Omit, "queryKey" | "queryFn">) => useSuspenseQuery({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options }); +export const useDefaultServiceGetNotDefinedSuspense = = unknown[]>(queryKey?: TQueryKey, options?: Omit, "queryKey" | "queryFn">) => useSuspenseQuery({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(queryKey), queryFn: () => DefaultService.getNotDefined() as TData, ...options }); /** * Returns a user based on a single ID, if the user does not have access to the pet * @param data The data for the request.