Skip to content

Commit

Permalink
feat: override return types
Browse files Browse the repository at this point in the history
  • Loading branch information
7nohe committed Oct 31, 2023
1 parent c3159e0 commit 586eeec
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 177 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"commander": "^11.1.0",
"glob": "^10.2.5",
"openapi-typescript-codegen": "^0.25.0",
"typescript": "^5.0.4"
"typescript": "^5.2.2"
},
"peerDependencies": {
"commander": ">= 11 < 12",
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/createExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const createExports = (generatedClientsPath: string) => {
) as ts.MethodDeclaration[];
return methods
.map((method) => {
const methodName = method.name?.getText(node)!;
const methodBlock = method
.getChildren(node)
.find((child) => child.kind === ts.SyntaxKind.Block) as ts.Block;
Expand Down
10 changes: 10 additions & 0 deletions src/createImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export const createImports = (generatedClientsPath: string) => {
undefined,
ts.factory.createIdentifier("useMutation")
),
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier("UseQueryResult")
),
ts.factory.createImportSpecifier(
false,
undefined,
Expand All @@ -34,6 +39,11 @@ export const createImports = (generatedClientsPath: string) => {
undefined,
ts.factory.createIdentifier("UseMutationOptions")
),
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier("UseMutationResult")
),
])
),
ts.factory.createStringLiteral("@tanstack/react-query"),
Expand Down
16 changes: 10 additions & 6 deletions src/createSource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ts from "typescript";
import { createImports } from "./createImports";
import { createExports } from "./createExports";
import { version } from "../package.json";

const createSourceFile = (outputPath: string) => {
return ts.factory.createSourceFile(
Expand All @@ -18,13 +19,16 @@ export const createSource = (outputPath: string) => {
false,
ts.ScriptKind.TS
);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const printer = ts.createPrinter({
newLine: ts.NewLineKind.LineFeed,
removeComments: false,
});

const result = printer.printNode(
ts.EmitHint.Unspecified,
createSourceFile(outputPath),
resultFile
);
const node = createSourceFile(outputPath);

const result = `// generated with openapi-generator@${version} \n` + printer.printNode(ts.EmitHint.Unspecified, node, resultFile);



return result;
};
198 changes: 127 additions & 71 deletions src/createUseMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,48 @@ export const createUseMutation = (
method: ts.MethodDeclaration
) => {
const methodName = method.name?.getText(node)!;
// Awaited<ReturnType<typeof myClass.myMethod>>
const awaitedResponseDataType = ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("Awaited"),
[
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("ReturnType"),
[
ts.factory.createTypeQueryNode(
ts.factory.createQualifiedName(
ts.factory.createIdentifier(className),
ts.factory.createIdentifier(methodName)
),
undefined
),
]
),
]
);

const responseDataType = ts.factory.createTypeParameterDeclaration(
undefined,
"ResponseData",
undefined,
awaitedResponseDataType
);

const methodParameters =
method.parameters.length !== 0
? ts.factory.createTypeLiteralNode(
method.parameters.map((param) => {
return ts.factory.createPropertySignature(
undefined,
ts.factory.createIdentifier(param.name.getText(node)),
param.questionToken ?? param.initializer
? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
: param.questionToken,
param.type
);
})
)
: ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword);

return ts.factory.createVariableStatement(
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
ts.factory.createVariableDeclarationList(
Expand All @@ -19,7 +61,7 @@ export const createUseMutation = (
undefined,
ts.factory.createArrowFunction(
undefined,
undefined,
ts.factory.createNodeArray([responseDataType]),
[
ts.factory.createParameterDeclaration(
undefined,
Expand All @@ -32,40 +74,11 @@ export const createUseMutation = (
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("UseMutationOptions"),
[
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("Awaited"),
[
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("ReturnType"),
[
ts.factory.createTypeQueryNode(
ts.factory.createQualifiedName(
ts.factory.createIdentifier(className),
ts.factory.createIdentifier(methodName)
),
undefined
),
]
),
]
),
awaitedResponseDataType,
ts.factory.createKeywordTypeNode(
ts.SyntaxKind.UnknownKeyword
),
method.parameters.length !== 0 ? ts.factory.createTypeLiteralNode(
method.parameters.map((param) => {
return ts.factory.createPropertySignature(
undefined,
ts.factory.createIdentifier(
param.name.getText(node)
),
param.questionToken ?? param.initializer ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : param.questionToken,
param.type
);
})
) : ts.factory.createKeywordTypeNode(
ts.SyntaxKind.VoidKeyword
),
methodParameters,
ts.factory.createKeywordTypeNode(
ts.SyntaxKind.UnknownKeyword
),
Expand All @@ -81,56 +94,99 @@ export const createUseMutation = (
],
undefined,
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
ts.factory.createCallExpression(
ts.factory.createIdentifier("useMutation"),
undefined,
[
ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("mutationFn"),
ts.factory.createArrowFunction(
undefined,
undefined,
method.parameters.length !== 0 ? [
ts.factory.createParameterDeclaration(
undefined,
undefined,
ts.factory.createObjectBindingPattern(
method.parameters.map((param) => {
return ts.factory.createBindingElement(
ts.factory.createAsExpression(
ts.factory.createCallExpression(
ts.factory.createIdentifier("useMutation"),
undefined,
[
ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("mutationFn"),
ts.factory.createArrowFunction(
undefined,
undefined,
method.parameters.length !== 0
? [
ts.factory.createParameterDeclaration(
undefined,
undefined,
ts.factory.createIdentifier(
param.name.getText(node)
ts.factory.createObjectBindingPattern(
method.parameters.map((param) => {
return ts.factory.createBindingElement(
undefined,
undefined,
ts.factory.createIdentifier(
param.name.getText(node)
),
undefined
);
})
),
undefined,
undefined,
undefined
);
})
),
]
: [],
undefined,
ts.factory.createToken(
ts.SyntaxKind.EqualsGreaterThanToken
),
ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier(className),
ts.factory.createIdentifier(methodName)
),
undefined,
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.parameters.map((params) =>
ts.factory.createIdentifier(params.name.getText(node))
method.parameters.map((params) =>
ts.factory.createIdentifier(
params.name.getText(node)
)
)
)
)
),
ts.factory.createSpreadAssignment(
ts.factory.createIdentifier("options")
),
]),
]
),
// Omit<UseMutationResult<ResponseData, unknown, { requestBody: NewPet; }, unknown>, 'data'> & { data: ResponseData };
ts.factory.createIntersectionTypeNode([
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("Omit"),
[
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("UseMutationResult"),
[
awaitedResponseDataType,
ts.factory.createKeywordTypeNode(
ts.SyntaxKind.UnknownKeyword
),
methodParameters,
ts.factory.createKeywordTypeNode(
ts.SyntaxKind.UnknownKeyword
),
]
),
ts.factory.createLiteralTypeNode(
ts.factory.createStringLiteral("data")
),
]
),
ts.factory.createTypeLiteralNode([
ts.factory.createPropertySignature(
undefined,
ts.factory.createIdentifier("data"),
undefined,
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("ResponseData"),
undefined
)
),
ts.factory.createSpreadAssignment(
ts.factory.createIdentifier("options")
),
]),
]
])
)
)
),
Expand Down
Loading

0 comments on commit 586eeec

Please sign in to comment.