Skip to content

Commit f62cb23

Browse files
authored
QueryKeys: default query keys to include any params (#9)
1 parent 35394ed commit f62cb23

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/createUseQuery.ts

+28-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ export const createUseQuery = (
3939
}
4040

4141
const customHookName = `use${className}${capitalizeFirstLetter(methodName)}`;
42-
const queryKey = `${customHookName}Key`
42+
const queryKey = `${customHookName}Key`;
43+
44+
const queryKeyGenericType = ts.factory.createTypeReferenceNode('TQueryKey', undefined);
45+
const queryKeyConstraint = ts.factory.createTypeReferenceNode('Array', [ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)]);
4346

4447
return [
4548
ts.factory.createVariableStatement(
@@ -66,19 +69,22 @@ export const createUseQuery = (
6669
undefined,
6770
ts.factory.createArrowFunction(
6871
undefined,
69-
undefined,
72+
ts.factory.createNodeArray([
73+
ts.factory.createTypeParameterDeclaration(
74+
undefined,
75+
'TQueryKey',
76+
queryKeyConstraint,
77+
ts.factory.createArrayTypeNode(ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)),
78+
),
79+
]),
7080
[
7181
...requestParam,
7282
ts.factory.createParameterDeclaration(
7383
undefined,
7484
undefined,
7585
ts.factory.createIdentifier("queryKey"),
76-
undefined,
77-
ts.factory.createArrayTypeNode(ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)),
78-
ts.factory.createArrayLiteralExpression(
79-
[],
80-
false
81-
)
86+
ts.factory.createToken(ts.SyntaxKind.QuestionToken),
87+
queryKeyGenericType,
8288
),
8389
ts.factory.createParameterDeclaration(
8490
undefined,
@@ -130,7 +136,7 @@ export const createUseQuery = (
130136
),
131137
ts.factory.createArrayTypeNode(
132138
ts.factory.createKeywordTypeNode(
133-
ts.SyntaxKind.StringKeyword
139+
ts.SyntaxKind.UnknownKeyword,
134140
)
135141
),
136142
]
@@ -159,7 +165,19 @@ export const createUseQuery = (
159165
ts.factory.createArrayLiteralExpression(
160166
[
161167
ts.factory.createIdentifier(queryKey),
162-
ts.factory.createSpreadElement(ts.factory.createIdentifier("queryKey"))
168+
ts.factory.createSpreadElement(ts.factory.createParenthesizedExpression(ts.factory.createBinaryExpression(
169+
ts.factory.createIdentifier("queryKey"),
170+
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
171+
method.parameters.length ? ts.factory.createArrayLiteralExpression([
172+
ts.factory.createObjectLiteralExpression(
173+
method.parameters.map((param) =>
174+
ts.factory.createShorthandPropertyAssignment(
175+
ts.factory.createIdentifier(param.name.getText(node)),
176+
),
177+
),
178+
),
179+
]) : ts.factory.createArrayLiteralExpression([]),
180+
))),
163181
],
164182
false
165183
),

0 commit comments

Comments
 (0)