Skip to content
Merged
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
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