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

fix: Use optional query keys also without parameters #116

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/createUseQuery.mts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export function createQueryHook({
),
ts.factory.createIdentifier("queryKey"),
]
: [],
: [ts.factory.createIdentifier("queryKey")],
),
),
ts.factory.createPropertyAssignment(
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions tests/__snapshots__/createSource.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const UseDefaultServiceFindPetsKeyFn = ({ limit, tags }: {
export type DefaultServiceGetNotDefinedDefaultResponse = Awaited<ReturnType<typeof DefaultService.getNotDefined>>;
export type DefaultServiceGetNotDefinedQueryResult<TData = DefaultServiceGetNotDefinedDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
export const useDefaultServiceGetNotDefinedKey = "DefaultServiceGetNotDefined";
export const UseDefaultServiceGetNotDefinedKeyFn = () => [useDefaultServiceGetNotDefinedKey];
export const UseDefaultServiceGetNotDefinedKeyFn = (queryKey?: Array<unknown>) => [useDefaultServiceGetNotDefinedKey, ...(queryKey ?? [])];
export type DefaultServiceFindPetByIdDefaultResponse = Awaited<ReturnType<typeof DefaultService.findPetById>>;
export type DefaultServiceFindPetByIdQueryResult<TData = DefaultServiceFindPetByIdDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
export const useDefaultServiceFindPetByIdKey = "DefaultServiceFindPetById";
Expand Down Expand Up @@ -67,7 +67,7 @@ export const useDefaultServiceFindPets = <TData = Common.DefaultServiceFindPetsD
* @returns unknown unexpected error
* @throws ApiError
*/
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 });
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 });
/**
* 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.
Expand Down Expand Up @@ -145,7 +145,7 @@ export const useDefaultServiceFindPetsSuspense = <TData = Common.DefaultServiceF
* @returns unknown unexpected error
* @throws ApiError
*/
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 });
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 });
/**
* 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.
Expand Down
6 changes: 3 additions & 3 deletions tests/__snapshots__/generate.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const UseDefaultServiceFindPetsKeyFn = ({ limit, tags }: {
export type DefaultServiceGetNotDefinedDefaultResponse = Awaited<ReturnType<typeof DefaultService.getNotDefined>>;
export type DefaultServiceGetNotDefinedQueryResult<TData = DefaultServiceGetNotDefinedDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
export const useDefaultServiceGetNotDefinedKey = "DefaultServiceGetNotDefined";
export const UseDefaultServiceGetNotDefinedKeyFn = () => [useDefaultServiceGetNotDefinedKey];
export const UseDefaultServiceGetNotDefinedKeyFn = (queryKey?: Array<unknown>) => [useDefaultServiceGetNotDefinedKey, ...(queryKey ?? [])];
export type DefaultServiceFindPetByIdDefaultResponse = Awaited<ReturnType<typeof DefaultService.findPetById>>;
export type DefaultServiceFindPetByIdQueryResult<TData = DefaultServiceFindPetByIdDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
export const useDefaultServiceFindPetByIdKey = "DefaultServiceFindPetById";
Expand Down Expand Up @@ -110,7 +110,7 @@ export const useDefaultServiceFindPets = <TData = Common.DefaultServiceFindPetsD
* @returns unknown unexpected error
* @throws ApiError
*/
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 });
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 });
/**
* 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.
Expand Down Expand Up @@ -187,7 +187,7 @@ export const useDefaultServiceFindPetsSuspense = <TData = Common.DefaultServiceF
* @returns unknown unexpected error
* @throws ApiError
*/
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 });
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 });
/**
* 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.
Expand Down
Loading