Skip to content

fix: reuse helper to generate keys in prefetch hooks #103

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

Merged
merged 6 commits into from
Jun 7, 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
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
Loading