Skip to content

Commit 0ae3d77

Browse files
authored
fix: Use optional query keys also without parameters (#116)
1 parent 375c283 commit 0ae3d77

File tree

3 files changed

+8
-16
lines changed

3 files changed

+8
-16
lines changed

src/createUseQuery.mts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ export function createQueryHook({
347347
),
348348
ts.factory.createIdentifier("queryKey"),
349349
]
350-
: [],
350+
: [ts.factory.createIdentifier("queryKey")],
351351
),
352352
),
353353
ts.factory.createPropertyAssignment(
@@ -490,7 +490,7 @@ function createQueryKeyFnExport(queryKey: string, method: MethodDeclaration) {
490490
ts.factory.createArrowFunction(
491491
undefined,
492492
undefined,
493-
params ? [params, overrideKey] : [],
493+
params ? [params, overrideKey] : [overrideKey],
494494
undefined,
495495
EqualsOrGreaterThanToken,
496496
queryKeyFn(queryKey, method),
@@ -506,14 +506,6 @@ function queryKeyFn(
506506
queryKey: string,
507507
method: MethodDeclaration,
508508
): ts.Expression {
509-
const params = getRequestParamFromMethod(method);
510-
511-
if (!params) {
512-
return ts.factory.createArrayLiteralExpression([
513-
ts.factory.createIdentifier(queryKey),
514-
]);
515-
}
516-
517509
return ts.factory.createArrayLiteralExpression(
518510
[
519511
ts.factory.createIdentifier(queryKey),

tests/__snapshots__/createSource.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const UseDefaultServiceFindPetsKeyFn = ({ limit, tags }: {
2424
export type DefaultServiceGetNotDefinedDefaultResponse = Awaited<ReturnType<typeof DefaultService.getNotDefined>>;
2525
export type DefaultServiceGetNotDefinedQueryResult<TData = DefaultServiceGetNotDefinedDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
2626
export const useDefaultServiceGetNotDefinedKey = "DefaultServiceGetNotDefined";
27-
export const UseDefaultServiceGetNotDefinedKeyFn = () => [useDefaultServiceGetNotDefinedKey];
27+
export const UseDefaultServiceGetNotDefinedKeyFn = (queryKey?: Array<unknown>) => [useDefaultServiceGetNotDefinedKey, ...(queryKey ?? [])];
2828
export type DefaultServiceFindPetByIdDefaultResponse = Awaited<ReturnType<typeof DefaultService.findPetById>>;
2929
export type DefaultServiceFindPetByIdQueryResult<TData = DefaultServiceFindPetByIdDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
3030
export const useDefaultServiceFindPetByIdKey = "DefaultServiceFindPetById";
@@ -67,7 +67,7 @@ export const useDefaultServiceFindPets = <TData = Common.DefaultServiceFindPetsD
6767
* @returns unknown unexpected error
6868
* @throws ApiError
6969
*/
70-
export const useDefaultServiceGetNotDefined = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
70+
export const useDefaultServiceGetNotDefined = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(queryKey), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
7171
/**
7272
* Returns a user based on a single ID, if the user does not have access to the pet
7373
* @param data The data for the request.
@@ -145,7 +145,7 @@ export const useDefaultServiceFindPetsSuspense = <TData = Common.DefaultServiceF
145145
* @returns unknown unexpected error
146146
* @throws ApiError
147147
*/
148-
export const useDefaultServiceGetNotDefinedSuspense = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
148+
export const useDefaultServiceGetNotDefinedSuspense = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(queryKey), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
149149
/**
150150
* Returns a user based on a single ID, if the user does not have access to the pet
151151
* @param data The data for the request.

tests/__snapshots__/generate.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const UseDefaultServiceFindPetsKeyFn = ({ limit, tags }: {
1515
export type DefaultServiceGetNotDefinedDefaultResponse = Awaited<ReturnType<typeof DefaultService.getNotDefined>>;
1616
export type DefaultServiceGetNotDefinedQueryResult<TData = DefaultServiceGetNotDefinedDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
1717
export const useDefaultServiceGetNotDefinedKey = "DefaultServiceGetNotDefined";
18-
export const UseDefaultServiceGetNotDefinedKeyFn = () => [useDefaultServiceGetNotDefinedKey];
18+
export const UseDefaultServiceGetNotDefinedKeyFn = (queryKey?: Array<unknown>) => [useDefaultServiceGetNotDefinedKey, ...(queryKey ?? [])];
1919
export type DefaultServiceFindPetByIdDefaultResponse = Awaited<ReturnType<typeof DefaultService.findPetById>>;
2020
export type DefaultServiceFindPetByIdQueryResult<TData = DefaultServiceFindPetByIdDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
2121
export const useDefaultServiceFindPetByIdKey = "DefaultServiceFindPetById";
@@ -110,7 +110,7 @@ export const useDefaultServiceFindPets = <TData = Common.DefaultServiceFindPetsD
110110
* @returns unknown unexpected error
111111
* @throws ApiError
112112
*/
113-
export const useDefaultServiceGetNotDefined = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
113+
export const useDefaultServiceGetNotDefined = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(queryKey), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
114114
/**
115115
* Returns a user based on a single ID, if the user does not have access to the pet
116116
* @param data The data for the request.
@@ -187,7 +187,7 @@ export const useDefaultServiceFindPetsSuspense = <TData = Common.DefaultServiceF
187187
* @returns unknown unexpected error
188188
* @throws ApiError
189189
*/
190-
export const useDefaultServiceGetNotDefinedSuspense = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
190+
export const useDefaultServiceGetNotDefinedSuspense = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(queryKey), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
191191
/**
192192
* Returns a user based on a single ID, if the user does not have access to the pet
193193
* @param data The data for the request.

0 commit comments

Comments
 (0)