Skip to content

Commit 86cae73

Browse files
committed
feat: include require syntax
1 parent 0849f85 commit 86cae73

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class UserHttpService {
5555
@GetExchange('/users/{id}') // path
5656
@ResponseBody(UserResponse) // response dto
5757
async request(@PathVariable() id: number): Promise<UserResponse> {
58-
return imitation(); // this is a mock function to prevent the type error
58+
return imitation(id); // this is a mock function to prevent the type error
5959
}
6060
}
6161
```

lib/plugin/visitors/http-interface.visitor.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
PatchExchange,
99
PostExchange,
1010
PutExchange,
11+
GraphQLExchange,
12+
ResponseBody,
1113
} from '../../decorators';
12-
import { GraphQLExchange } from '../../decorators/graphql-exchange.decorator';
13-
import { ResponseBody } from '../../decorators/response-body.decorator';
1414

1515
export class HttpInterfaceVisitor {
1616
HTTP_INTERFACE_DECORATOR = HttpInterface.name;
@@ -29,17 +29,23 @@ export class HttpInterfaceVisitor {
2929
libModuleAlias = 'eager_module_1';
3030
libName = '@r2don/nest-http-interface';
3131
importSet = new Set<string>();
32+
typeFormatFlag =
33+
ts.TypeFormatFlags.UseTypeOfFunction |
34+
ts.TypeFormatFlags.NoTruncation |
35+
ts.TypeFormatFlags.UseFullyQualifiedType |
36+
ts.TypeFormatFlags.WriteTypeArgumentsOfSignature;
3237

3338
visit(
3439
sourceFile: ts.SourceFile,
3540
ctx: ts.TransformationContext,
36-
_program: ts.Program,
41+
program: ts.Program,
3742
): ts.Node {
3843
this.importSet.clear();
3944
const factory = ctx.factory;
45+
const typeChecker = program.getTypeChecker();
4046
const visitNode = (node: ts.Node): ts.Node => {
4147
if (this.isHttpInterfaceClass(node)) {
42-
return this.visitMethods(node, factory);
48+
return this.visitMethods(node, factory, typeChecker);
4349
}
4450

4551
if (ts.isSourceFile(node)) {
@@ -84,13 +90,14 @@ export class HttpInterfaceVisitor {
8490
private visitMethods(
8591
node: ts.ClassDeclaration,
8692
factory: ts.NodeFactory,
93+
typeChecker: ts.TypeChecker,
8794
): ts.ClassDeclaration {
8895
const updatedMembers = node.members.map((member) => {
8996
if (!this.isExchangeMethod(member)) {
9097
return member;
9198
}
9299

93-
return this.appendResponseBodyDecorator(member, factory);
100+
return this.appendResponseBodyDecorator(member, factory, typeChecker);
94101
});
95102

96103
return factory.updateClassDeclaration(
@@ -113,8 +120,9 @@ export class HttpInterfaceVisitor {
113120
private appendResponseBodyDecorator(
114121
node: ts.MethodDeclaration,
115122
factory: ts.NodeFactory,
123+
typeChecker: ts.TypeChecker,
116124
): ts.MethodDeclaration {
117-
const returnType = this.getReturnTypeAsText(node);
125+
const returnType = this.getReturnTypeAsText(node, typeChecker);
118126

119127
if (returnType == null) {
120128
return node;
@@ -146,7 +154,10 @@ export class HttpInterfaceVisitor {
146154
);
147155
}
148156

149-
private getReturnTypeAsText(node: ts.MethodDeclaration): string | undefined {
157+
private getReturnTypeAsText(
158+
node: ts.MethodDeclaration,
159+
typeChecker: ts.TypeChecker,
160+
): string | undefined {
150161
if (
151162
node.type == null ||
152163
!ts.isTypeReferenceNode(node.type) ||
@@ -165,7 +176,13 @@ export class HttpInterfaceVisitor {
165176
return undefined;
166177
}
167178

168-
return innerType.typeName.text;
179+
return typeChecker
180+
.typeToString(
181+
typeChecker.getTypeAtLocation(innerType),
182+
undefined,
183+
this.typeFormatFlag,
184+
)
185+
.replace('import', 'require');
169186
}
170187

171188
private getInnerType(node?: ts.TypeNode): ts.TypeNode | undefined {

0 commit comments

Comments
 (0)