diff --git a/src/createPrefetch.mts b/src/createPrefetch.mts index 70729ee..d098289 100644 --- a/src/createPrefetch.mts +++ b/src/createPrefetch.mts @@ -8,6 +8,7 @@ import { import type { MethodDescription } from "./common.mjs"; import { createQueryKeyFromMethod, + getQueryKeyFnName, getRequestParamFromMethod, hookNameFromMethod, } from "./createUseQuery.mjs"; @@ -66,27 +67,26 @@ function createPrefetchHook({ ts.factory.createObjectLiteralExpression([ ts.factory.createPropertyAssignment( ts.factory.createIdentifier("queryKey"), - ts.factory.createArrayLiteralExpression( - [ - BuildCommonTypeName(queryKey), - method.getParameters().length - ? ts.factory.createArrayLiteralExpression([ - ts.factory.createObjectLiteralExpression( - method - .getParameters() - .flatMap((param) => - extractPropertiesFromObjectParam(param).map( - (p) => - ts.factory.createShorthandPropertyAssignment( - ts.factory.createIdentifier(p.name), - ), - ), + ts.factory.createCallExpression( + BuildCommonTypeName(getQueryKeyFnName(queryKey)), + undefined, + + method.getParameters().length + ? [ + ts.factory.createObjectLiteralExpression( + method + .getParameters() + .flatMap((param) => + extractPropertiesFromObjectParam(param).map( + (p) => + ts.factory.createShorthandPropertyAssignment( + ts.factory.createIdentifier(p.name), + ), ), - ), - ]) - : ts.factory.createArrayLiteralExpression([]), - ], - false, + ), + ), + ] + : [], ), ), ts.factory.createPropertyAssignment( diff --git a/src/createUseQuery.mts b/src/createUseQuery.mts index 8eb919e..5378004 100644 --- a/src/createUseQuery.mts +++ b/src/createUseQuery.mts @@ -463,7 +463,7 @@ export const createUseQuery = ({ }; }; -function getQueryKeyFnName(queryKey: string) { +export function getQueryKeyFnName(queryKey: string) { return `${capitalizeFirstLetter(queryKey)}Fn`; } diff --git a/tests/__snapshots__/createSource.test.ts.snap b/tests/__snapshots__/createSource.test.ts.snap index dd528b0..165b3b4 100644 --- a/tests/__snapshots__/createSource.test.ts.snap +++ b/tests/__snapshots__/createSource.test.ts.snap @@ -183,14 +183,14 @@ import { Pet, NewPet, Error, FindPetsData, FindPetsResponse, AddPetData, AddPetR export const prefetchUseDefaultServiceFindPets = (queryClient: QueryClient, { limit, tags }: { limit?: number; tags?: string[]; -} = {}) => queryClient.prefetchQuery({ queryKey: [Common.useDefaultServiceFindPetsKey, [{ limit, tags }]], queryFn: () => DefaultService.findPets({ limit, tags }) }); +} = {}) => queryClient.prefetchQuery({ queryKey: Common.UseDefaultServiceFindPetsKeyFn({ limit, tags }), queryFn: () => DefaultService.findPets({ limit, tags }) }); /** * @deprecated * This path is not fully defined. * @returns unknown unexpected error * @throws ApiError */ -export const prefetchUseDefaultServiceGetNotDefined = (queryClient: QueryClient) => queryClient.prefetchQuery({ queryKey: [Common.useDefaultServiceGetNotDefinedKey, []], queryFn: () => DefaultService.getNotDefined() }); +export const prefetchUseDefaultServiceGetNotDefined = (queryClient: QueryClient) => queryClient.prefetchQuery({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() }); /** * 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. @@ -201,6 +201,6 @@ export const prefetchUseDefaultServiceGetNotDefined = (queryClient: QueryClient) */ export const prefetchUseDefaultServiceFindPetById = (queryClient: QueryClient, { id }: { id: number; -}) => queryClient.prefetchQuery({ queryKey: [Common.useDefaultServiceFindPetByIdKey, [{ id }]], queryFn: () => DefaultService.findPetById({ id }) }); +}) => queryClient.prefetchQuery({ queryKey: Common.UseDefaultServiceFindPetByIdKeyFn({ id }), queryFn: () => DefaultService.findPetById({ id }) }); " `; diff --git a/tests/__snapshots__/generate.test.ts.snap b/tests/__snapshots__/generate.test.ts.snap index 4aed6c0..53ace37 100644 --- a/tests/__snapshots__/generate.test.ts.snap +++ b/tests/__snapshots__/generate.test.ts.snap @@ -58,14 +58,14 @@ import * as Common from "./common"; export const prefetchUseDefaultServiceFindPets = (queryClient: QueryClient, { limit, tags }: { limit?: number; tags?: string[]; -} = {}) => queryClient.prefetchQuery({ queryKey: [Common.useDefaultServiceFindPetsKey, [{ limit, tags }]], queryFn: () => DefaultService.findPets({ limit, tags }) }); +} = {}) => queryClient.prefetchQuery({ queryKey: Common.UseDefaultServiceFindPetsKeyFn({ limit, tags }), queryFn: () => DefaultService.findPets({ limit, tags }) }); /** * @deprecated * This path is not fully defined. * @returns unknown unexpected error * @throws ApiError */ -export const prefetchUseDefaultServiceGetNotDefined = (queryClient: QueryClient) => queryClient.prefetchQuery({ queryKey: [Common.useDefaultServiceGetNotDefinedKey, []], queryFn: () => DefaultService.getNotDefined() }); +export const prefetchUseDefaultServiceGetNotDefined = (queryClient: QueryClient) => queryClient.prefetchQuery({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() }); /** * 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. @@ -76,7 +76,7 @@ export const prefetchUseDefaultServiceGetNotDefined = (queryClient: QueryClient) */ export const prefetchUseDefaultServiceFindPetById = (queryClient: QueryClient, { id }: { id: number; -}) => queryClient.prefetchQuery({ queryKey: [Common.useDefaultServiceFindPetByIdKey, [{ id }]], queryFn: () => DefaultService.findPetById({ id }) }); +}) => queryClient.prefetchQuery({ queryKey: Common.UseDefaultServiceFindPetByIdKeyFn({ id }), queryFn: () => DefaultService.findPetById({ id }) }); " `;