Skip to content

Commit

Permalink
reuse function to generate keys
Browse files Browse the repository at this point in the history
  • Loading branch information
nopitown committed May 3, 2024
1 parent c5460df commit 3588856
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
63 changes: 41 additions & 22 deletions src/createPrefetch.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import {
BuildCommonTypeName,
extractPropertiesFromObjectParam,
getNameFromMethod,
queryKeyConstraint,
queryKeyGenericType,
} from "./common.mjs";
import { type MethodDescription } from "./common.mjs";
import {
createQueryKeyFromMethod,
getRequestParamFromMethod,
hookNameFromMethod,
getQueryKeyFnName,
} from "./createUseQuery.mjs";
import { addJSDocToNode } from "./util.mjs";

Expand Down Expand Up @@ -42,7 +45,16 @@ function createPrefetchHook({
undefined,
ts.factory.createArrowFunction(
undefined,
undefined,
ts.factory.createNodeArray([
ts.factory.createTypeParameterDeclaration(
undefined,
"TQueryKey",
queryKeyConstraint,
ts.factory.createArrayTypeNode(
ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)
)
),
]),
[
ts.factory.createParameterDeclaration(
undefined,
Expand All @@ -54,6 +66,13 @@ function createPrefetchHook({
)
),
...requestParams,
ts.factory.createParameterDeclaration(
undefined,
undefined,
ts.factory.createIdentifier("queryKey"),
ts.factory.createToken(ts.SyntaxKind.QuestionToken),
queryKeyGenericType
),
],
undefined,
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
Expand All @@ -64,28 +83,28 @@ 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()
.map((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()
.map((param) =>
extractPropertiesFromObjectParam(param).map(
(p) =>
ts.factory.createShorthandPropertyAssignment(
ts.factory.createIdentifier(p.name)
)
)
.flat()
),
])
: ts.factory.createArrayLiteralExpression([]),
],
false
)
.flat()
),
ts.factory.createIdentifier("queryKey"),
]
: []
)
),
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 @@ -466,7 +466,7 @@ export const createUseQuery = ({
};
};

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

Expand Down
10 changes: 5 additions & 5 deletions tests/__snapshots__/createSource.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,17 @@ import { Pet, NewPet, Error, $OpenApiTs } from "../requests/types.gen";
* @returns Error unexpected error
* @throws ApiError
*/
export const prefetchUseDefaultServiceFindPets = (queryClient: QueryClient, { limit, tags }: {
export const prefetchUseDefaultServiceFindPets = <TQueryKey extends Array<unknown> = unknown[]>(queryClient: QueryClient, { limit, tags }: {
limit?: number;
tags?: string[];
} = {}) => queryClient.prefetchQuery({ queryKey: [Common.useDefaultServiceFindPetsKey, [{ limit, tags }]], queryFn: () => DefaultService.findPets({ limit, tags }) });
} = {}, queryKey?: TQueryKey) => queryClient.prefetchQuery({ queryKey: Common.UseDefaultServiceFindPetsKeyFn({ limit, tags }, queryKey), 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 = <TQueryKey extends Array<unknown> = unknown[]>(queryClient: QueryClient, queryKey?: TQueryKey) => 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 @@ -199,8 +199,8 @@ export const prefetchUseDefaultServiceGetNotDefined = (queryClient: QueryClient)
* @returns Error unexpected error
* @throws ApiError
*/
export const prefetchUseDefaultServiceFindPetById = (queryClient: QueryClient, { id }: {
export const prefetchUseDefaultServiceFindPetById = <TQueryKey extends Array<unknown> = unknown[]>(queryClient: QueryClient, { id }: {
id: number;
}) => queryClient.prefetchQuery({ queryKey: [Common.useDefaultServiceFindPetByIdKey, [{ id }]], queryFn: () => DefaultService.findPetById({ id }) });
}, queryKey?: TQueryKey) => queryClient.prefetchQuery({ queryKey: Common.UseDefaultServiceFindPetByIdKeyFn({ id }, queryKey), queryFn: () => DefaultService.findPetById({ id }) });
"
`;
10 changes: 5 additions & 5 deletions tests/__snapshots__/generate.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ import * as Common from "./common";
* @returns Error unexpected error
* @throws ApiError
*/
export const prefetchUseDefaultServiceFindPets = (queryClient: QueryClient, { limit, tags }: {
export const prefetchUseDefaultServiceFindPets = <TQueryKey extends Array<unknown> = unknown[]>(queryClient: QueryClient, { limit, tags }: {
limit?: number;
tags?: string[];
} = {}) => queryClient.prefetchQuery({ queryKey: [Common.useDefaultServiceFindPetsKey, [{ limit, tags }]], queryFn: () => DefaultService.findPets({ limit, tags }) });
} = {}, queryKey?: TQueryKey) => queryClient.prefetchQuery({ queryKey: Common.UseDefaultServiceFindPetsKeyFn({ limit, tags }, queryKey), 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 = <TQueryKey extends Array<unknown> = unknown[]>(queryClient: QueryClient, queryKey?: TQueryKey) => 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 @@ -73,9 +73,9 @@ export const prefetchUseDefaultServiceGetNotDefined = (queryClient: QueryClient)
* @returns Error unexpected error
* @throws ApiError
*/
export const prefetchUseDefaultServiceFindPetById = (queryClient: QueryClient, { id }: {
export const prefetchUseDefaultServiceFindPetById = <TQueryKey extends Array<unknown> = unknown[]>(queryClient: QueryClient, { id }: {
id: number;
}) => queryClient.prefetchQuery({ queryKey: [Common.useDefaultServiceFindPetByIdKey, [{ id }]], queryFn: () => DefaultService.findPetById({ id }) });
}, queryKey?: TQueryKey) => queryClient.prefetchQuery({ queryKey: Common.UseDefaultServiceFindPetByIdKeyFn({ id }, queryKey), queryFn: () => DefaultService.findPetById({ id }) });
"
`;
Expand Down

0 comments on commit 3588856

Please sign in to comment.