-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prefetch): add prefetch query hook generation
closes: #91
- Loading branch information
1 parent
125c5b1
commit e6c8645
Showing
11 changed files
with
333 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import ts from "typescript"; | ||
import { MethodDeclaration } from "ts-morph"; | ||
import { | ||
BuildCommonTypeName, | ||
extractPropertiesFromObjectParam, | ||
getNameFromMethod, | ||
} from "./common.mjs"; | ||
import { type MethodDescription } from "./common.mjs"; | ||
import { | ||
createQueryKeyFromMethod, | ||
getRequestParamFromMethod, | ||
hookNameFromMethod, | ||
} from "./createUseQuery.mjs"; | ||
import { addJSDocToNode } from "./util.mjs"; | ||
|
||
/** | ||
* Creates a prefetch function for a query | ||
*/ | ||
function createPrefetchHook({ | ||
requestParams, | ||
method, | ||
className, | ||
}: { | ||
requestParams: ts.ParameterDeclaration[]; | ||
method: MethodDeclaration; | ||
className: string; | ||
}) { | ||
const methodName = getNameFromMethod(method); | ||
const queryName = hookNameFromMethod({ method, className }); | ||
const customHookName = `prefetch${queryName.charAt(0).toUpperCase() + queryName.slice(1)}`; | ||
const queryKey = createQueryKeyFromMethod({ method, className }); | ||
|
||
// const | ||
const hookExport = ts.factory.createVariableStatement( | ||
// export | ||
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], | ||
ts.factory.createVariableDeclarationList( | ||
[ | ||
ts.factory.createVariableDeclaration( | ||
ts.factory.createIdentifier(customHookName), | ||
undefined, | ||
undefined, | ||
ts.factory.createArrowFunction( | ||
undefined, | ||
undefined, | ||
[ | ||
ts.factory.createParameterDeclaration( | ||
undefined, | ||
undefined, | ||
"queryClient", | ||
undefined, | ||
ts.factory.createTypeReferenceNode( | ||
ts.factory.createIdentifier("QueryClient") | ||
) | ||
), | ||
...requestParams, | ||
], | ||
undefined, | ||
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), | ||
ts.factory.createCallExpression( | ||
ts.factory.createIdentifier("queryClient.prefetchQuery"), | ||
undefined, | ||
[ | ||
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) | ||
) | ||
) | ||
) | ||
.flat() | ||
), | ||
]) | ||
: ts.factory.createArrayLiteralExpression([]), | ||
], | ||
false | ||
) | ||
), | ||
ts.factory.createPropertyAssignment( | ||
ts.factory.createIdentifier("queryFn"), | ||
ts.factory.createArrowFunction( | ||
undefined, | ||
undefined, | ||
[], | ||
undefined, | ||
ts.factory.createToken( | ||
ts.SyntaxKind.EqualsGreaterThanToken | ||
), | ||
ts.factory.createCallExpression( | ||
ts.factory.createPropertyAccessExpression( | ||
ts.factory.createIdentifier(className), | ||
ts.factory.createIdentifier(methodName) | ||
), | ||
undefined, | ||
method.getParameters().length | ||
? [ | ||
ts.factory.createObjectLiteralExpression( | ||
method | ||
.getParameters() | ||
.map((param) => | ||
extractPropertiesFromObjectParam(param).map( | ||
(p) => | ||
ts.factory.createShorthandPropertyAssignment( | ||
ts.factory.createIdentifier(p.name) | ||
) | ||
) | ||
) | ||
.flat() | ||
), | ||
] | ||
: undefined | ||
) | ||
) | ||
), | ||
]), | ||
] | ||
) | ||
) | ||
), | ||
], | ||
ts.NodeFlags.Const | ||
) | ||
); | ||
return hookExport; | ||
} | ||
|
||
export const createPrefetch = ({ | ||
className, | ||
method, | ||
jsDoc, | ||
}: MethodDescription) => { | ||
const requestParam = getRequestParamFromMethod(method); | ||
|
||
const requestParams = requestParam ? [requestParam] : []; | ||
|
||
const prefetchHook = createPrefetchHook({ | ||
requestParams, | ||
method, | ||
className, | ||
}); | ||
|
||
const hookWithJsDoc = addJSDocToNode(prefetchHook, jsDoc); | ||
|
||
return { | ||
prefetchHook: hookWithJsDoc, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.