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
1515export 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