Skip to content

Commit

Permalink
QueryKeys: default query keys to include any params
Browse files Browse the repository at this point in the history
  • Loading branch information
seriouslag committed Mar 26, 2023
1 parent 35394ed commit 2223a58
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/createUseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export const createUseQuery = (
}

const customHookName = `use${className}${capitalizeFirstLetter(methodName)}`;
const queryKey = `${customHookName}Key`
const queryKey = `${customHookName}Key`;

const queryKeyGenericType = ts.factory.createTypeReferenceNode('TQueryKey', undefined);
const queryKeyConstraint = ts.factory.createTypeReferenceNode('Array', [ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)]);

return [
ts.factory.createVariableStatement(
Expand All @@ -66,19 +69,22 @@ export const createUseQuery = (
undefined,
ts.factory.createArrowFunction(
undefined,
undefined,
ts.factory.createNodeArray([
ts.factory.createTypeParameterDeclaration(
undefined,
'TQueryKey',
queryKeyConstraint,
ts.factory.createArrayTypeNode(ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)),
),
]),
[
...requestParam,
ts.factory.createParameterDeclaration(
undefined,
undefined,
ts.factory.createIdentifier("queryKey"),
undefined,
ts.factory.createArrayTypeNode(ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)),
ts.factory.createArrayLiteralExpression(
[],
false
)
ts.factory.createToken(ts.SyntaxKind.QuestionToken),
queryKeyGenericType,
),
ts.factory.createParameterDeclaration(
undefined,
Expand Down Expand Up @@ -130,7 +136,7 @@ export const createUseQuery = (
),
ts.factory.createArrayTypeNode(
ts.factory.createKeywordTypeNode(
ts.SyntaxKind.StringKeyword
ts.SyntaxKind.UnknownKeyword,
)
),
]
Expand Down Expand Up @@ -159,7 +165,19 @@ export const createUseQuery = (
ts.factory.createArrayLiteralExpression(
[
ts.factory.createIdentifier(queryKey),
ts.factory.createSpreadElement(ts.factory.createIdentifier("queryKey"))
ts.factory.createSpreadElement(ts.factory.createParenthesizedExpression(ts.factory.createBinaryExpression(
ts.factory.createIdentifier("queryKey"),
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
method.parameters.length ? ts.factory.createArrayLiteralExpression([
ts.factory.createObjectLiteralExpression(
method.parameters.map((param) =>
ts.factory.createShorthandPropertyAssignment(
ts.factory.createIdentifier(param.name.getText(node)),
),
),
),
]) : ts.factory.createArrayLiteralExpression([]),
))),
],
false
),
Expand Down

0 comments on commit 2223a58

Please sign in to comment.