Skip to content

Commit

Permalink
fix: reuse helper to generate keys in prefetch hooks (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
nopitown authored Jun 7, 2024
1 parent 1f95fbf commit 0529b6f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
40 changes: 20 additions & 20 deletions src/createPrefetch.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import type { MethodDescription } from "./common.mjs";
import {
createQueryKeyFromMethod,
getQueryKeyFnName,
getRequestParamFromMethod,
hookNameFromMethod,
} from "./createUseQuery.mjs";
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/createUseQuery.mts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export const createUseQuery = ({
};
};

function getQueryKeyFnName(queryKey: string) {
export function getQueryKeyFnName(queryKey: string) {
return `${capitalizeFirstLetter(queryKey)}Fn`;
}

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 @@ -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.
Expand All @@ -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 }) });
"
`;
6 changes: 3 additions & 3 deletions tests/__snapshots__/generate.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 }) });
"
`;
Expand Down

0 comments on commit 0529b6f

Please sign in to comment.