-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Approach 1: rewrite the output to add signal
- Loading branch information
1 parent
ae97745
commit 565af38
Showing
3 changed files
with
146 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import ts from "typescript"; | ||
import tsConfig from "../../../tsconfig.json"; | ||
import orvalConfig from "../../orval.config"; | ||
|
||
const Operations: Record< | ||
string, | ||
{ | ||
query: { | ||
useQuery: boolean; | ||
signal: boolean; | ||
}; | ||
} | ||
> = (orvalConfig as any).api.output.override.operations; | ||
|
||
function transformFile(fileName: string) { | ||
const program = ts.createProgram([fileName], { | ||
moduleResolution: ts.ModuleResolutionKind.Node10, | ||
baseUrl: ".", | ||
paths: tsConfig.compilerOptions.paths, | ||
}); | ||
|
||
const sourceFile = program.getSourceFile(fileName); | ||
if (!sourceFile) return; | ||
|
||
const transformationResult = ts.transform(sourceFile, [addSignalTransformer]); | ||
|
||
const transformedSourceFile = transformationResult.transformed[0]; | ||
const printer = ts.createPrinter(); | ||
|
||
const result = printer.printNode( | ||
ts.EmitHint.Unspecified, | ||
transformedSourceFile, | ||
sourceFile, | ||
); | ||
|
||
console.log(result); | ||
} | ||
|
||
function addSignalTransformer(context: ts.TransformationContext) { | ||
return (rootNode: ts.Node) => { | ||
function visit(node: ts.Node): ts.Node { | ||
node = ts.visitEachChild(node, visit, context); | ||
|
||
if ( | ||
// ignore non variable declarations | ||
!ts.isVariableDeclaration(node) || | ||
// ignore non identifier names | ||
!ts.isIdentifier(node.name) || | ||
// ignore methods that are not the query function | ||
!node.name.escapedText.toString().startsWith("queryService") | ||
) { | ||
return node; | ||
} | ||
|
||
const queryName = node.name.escapedText.toString(); | ||
if (!isOverriddenPostQuery(queryName)) { | ||
return node; | ||
} | ||
|
||
const init = node.initializer; | ||
// safeguard to make sure initializer is defined and is an arrow function | ||
if (!init || !ts.isArrowFunction(init) || !ts.isBlock(init.body)) { | ||
return node; | ||
} | ||
|
||
const callStatement = init.body.statements[0]; | ||
if ( | ||
!callStatement || | ||
!ts.isReturnStatement(callStatement) || | ||
!callStatement.expression || | ||
!ts.isCallExpression(callStatement.expression) || | ||
!callStatement.expression.arguments[0] || | ||
!ts.isObjectLiteralExpression(callStatement.expression.arguments[0]) | ||
) { | ||
return node; | ||
} | ||
|
||
const callArg = callStatement.expression.arguments[0]; | ||
const lastProp = callArg.properties[callArg.properties.length - 1]; | ||
if (lastProp?.name && (lastProp.name as any).escapedText === "signal") { | ||
return node; | ||
} | ||
|
||
console.log("Replacing", queryName); | ||
return context.factory.createVariableDeclaration( | ||
queryName, | ||
node.exclamationToken, | ||
node.type, | ||
context.factory.createArrowFunction( | ||
init.modifiers, | ||
init.typeParameters, | ||
[ | ||
...init.parameters, | ||
context.factory.createParameterDeclaration( | ||
undefined, | ||
undefined, | ||
"signal", | ||
context.factory.createToken(ts.SyntaxKind.QuestionToken), | ||
context.factory.createTypeReferenceNode("AbortSignal"), | ||
), | ||
], | ||
init.type, | ||
init.equalsGreaterThanToken, | ||
context.factory.createBlock([ | ||
context.factory.createReturnStatement( | ||
context.factory.createCallExpression( | ||
callStatement.expression.expression, | ||
callStatement.expression.typeArguments, | ||
[ | ||
context.factory.createObjectLiteralExpression( | ||
[ | ||
...callArg.properties, | ||
context.factory.createShorthandPropertyAssignment( | ||
"signal", | ||
), | ||
], | ||
true, | ||
), | ||
], | ||
), | ||
), | ||
]), | ||
), | ||
); | ||
} | ||
|
||
return ts.visitNode(rootNode, visit); | ||
}; | ||
} | ||
|
||
function isOverriddenPostQuery(name: string) { | ||
const operationName = | ||
"QueryService_" + | ||
name.replace(/queryService(.)/, (_, c: string) => c.toUpperCase()); | ||
return Operations[operationName]?.query?.signal; | ||
} | ||
|
||
transformFile(process.argv[2]); |