|
| 1 | +import ts from "typescript"; |
| 2 | +import { MethodDeclaration } from "ts-morph"; |
| 3 | +import { |
| 4 | + BuildCommonTypeName, |
| 5 | + extractPropertiesFromObjectParam, |
| 6 | + getNameFromMethod, |
| 7 | +} from "./common.mjs"; |
| 8 | +import { type MethodDescription } from "./common.mjs"; |
| 9 | +import { |
| 10 | + createQueryKeyFromMethod, |
| 11 | + getRequestParamFromMethod, |
| 12 | + hookNameFromMethod, |
| 13 | +} from "./createUseQuery.mjs"; |
| 14 | +import { addJSDocToNode } from "./util.mjs"; |
| 15 | + |
| 16 | +/** |
| 17 | + * Creates a prefetch function for a query |
| 18 | + */ |
| 19 | +function createPrefetchHook({ |
| 20 | + requestParams, |
| 21 | + method, |
| 22 | + className, |
| 23 | +}: { |
| 24 | + requestParams: ts.ParameterDeclaration[]; |
| 25 | + method: MethodDeclaration; |
| 26 | + className: string; |
| 27 | +}) { |
| 28 | + const methodName = getNameFromMethod(method); |
| 29 | + const queryName = hookNameFromMethod({ method, className }); |
| 30 | + const customHookName = `prefetch${queryName.charAt(0).toUpperCase() + queryName.slice(1)}`; |
| 31 | + const queryKey = createQueryKeyFromMethod({ method, className }); |
| 32 | + |
| 33 | + // const |
| 34 | + const hookExport = ts.factory.createVariableStatement( |
| 35 | + // export |
| 36 | + [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], |
| 37 | + ts.factory.createVariableDeclarationList( |
| 38 | + [ |
| 39 | + ts.factory.createVariableDeclaration( |
| 40 | + ts.factory.createIdentifier(customHookName), |
| 41 | + undefined, |
| 42 | + undefined, |
| 43 | + ts.factory.createArrowFunction( |
| 44 | + undefined, |
| 45 | + undefined, |
| 46 | + [ |
| 47 | + ts.factory.createParameterDeclaration( |
| 48 | + undefined, |
| 49 | + undefined, |
| 50 | + "queryClient", |
| 51 | + undefined, |
| 52 | + ts.factory.createTypeReferenceNode( |
| 53 | + ts.factory.createIdentifier("QueryClient") |
| 54 | + ) |
| 55 | + ), |
| 56 | + ...requestParams, |
| 57 | + ], |
| 58 | + undefined, |
| 59 | + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), |
| 60 | + ts.factory.createCallExpression( |
| 61 | + ts.factory.createIdentifier("queryClient.prefetchQuery"), |
| 62 | + undefined, |
| 63 | + [ |
| 64 | + ts.factory.createObjectLiteralExpression([ |
| 65 | + ts.factory.createPropertyAssignment( |
| 66 | + ts.factory.createIdentifier("queryKey"), |
| 67 | + ts.factory.createArrayLiteralExpression( |
| 68 | + [ |
| 69 | + BuildCommonTypeName(queryKey), |
| 70 | + method.getParameters().length |
| 71 | + ? ts.factory.createArrayLiteralExpression([ |
| 72 | + ts.factory.createObjectLiteralExpression( |
| 73 | + method |
| 74 | + .getParameters() |
| 75 | + .map((param) => |
| 76 | + extractPropertiesFromObjectParam(param).map( |
| 77 | + (p) => |
| 78 | + ts.factory.createShorthandPropertyAssignment( |
| 79 | + ts.factory.createIdentifier(p.name) |
| 80 | + ) |
| 81 | + ) |
| 82 | + ) |
| 83 | + .flat() |
| 84 | + ), |
| 85 | + ]) |
| 86 | + : ts.factory.createArrayLiteralExpression([]), |
| 87 | + ], |
| 88 | + false |
| 89 | + ) |
| 90 | + ), |
| 91 | + ts.factory.createPropertyAssignment( |
| 92 | + ts.factory.createIdentifier("queryFn"), |
| 93 | + ts.factory.createArrowFunction( |
| 94 | + undefined, |
| 95 | + undefined, |
| 96 | + [], |
| 97 | + undefined, |
| 98 | + ts.factory.createToken( |
| 99 | + ts.SyntaxKind.EqualsGreaterThanToken |
| 100 | + ), |
| 101 | + ts.factory.createCallExpression( |
| 102 | + ts.factory.createPropertyAccessExpression( |
| 103 | + ts.factory.createIdentifier(className), |
| 104 | + ts.factory.createIdentifier(methodName) |
| 105 | + ), |
| 106 | + undefined, |
| 107 | + method.getParameters().length |
| 108 | + ? [ |
| 109 | + ts.factory.createObjectLiteralExpression( |
| 110 | + method |
| 111 | + .getParameters() |
| 112 | + .map((param) => |
| 113 | + extractPropertiesFromObjectParam(param).map( |
| 114 | + (p) => |
| 115 | + ts.factory.createShorthandPropertyAssignment( |
| 116 | + ts.factory.createIdentifier(p.name) |
| 117 | + ) |
| 118 | + ) |
| 119 | + ) |
| 120 | + .flat() |
| 121 | + ), |
| 122 | + ] |
| 123 | + : undefined |
| 124 | + ) |
| 125 | + ) |
| 126 | + ), |
| 127 | + ]), |
| 128 | + ] |
| 129 | + ) |
| 130 | + ) |
| 131 | + ), |
| 132 | + ], |
| 133 | + ts.NodeFlags.Const |
| 134 | + ) |
| 135 | + ); |
| 136 | + return hookExport; |
| 137 | +} |
| 138 | + |
| 139 | +export const createPrefetch = ({ |
| 140 | + className, |
| 141 | + method, |
| 142 | + jsDoc, |
| 143 | +}: MethodDescription) => { |
| 144 | + const requestParam = getRequestParamFromMethod(method); |
| 145 | + |
| 146 | + const requestParams = requestParam ? [requestParam] : []; |
| 147 | + |
| 148 | + const prefetchHook = createPrefetchHook({ |
| 149 | + requestParams, |
| 150 | + method, |
| 151 | + className, |
| 152 | + }); |
| 153 | + |
| 154 | + const hookWithJsDoc = addJSDocToNode(prefetchHook, jsDoc); |
| 155 | + |
| 156 | + return { |
| 157 | + prefetchHook: hookWithJsDoc, |
| 158 | + }; |
| 159 | +}; |
0 commit comments