8
8
PatchExchange ,
9
9
PostExchange ,
10
10
PutExchange ,
11
+ GraphQLExchange ,
12
+ ResponseBody ,
11
13
} from '../../decorators' ;
12
- import { GraphQLExchange } from '../../decorators/graphql-exchange.decorator' ;
13
- import { ResponseBody } from '../../decorators/response-body.decorator' ;
14
14
15
15
export class HttpInterfaceVisitor {
16
16
HTTP_INTERFACE_DECORATOR = HttpInterface . name ;
@@ -29,17 +29,23 @@ export class HttpInterfaceVisitor {
29
29
libModuleAlias = 'eager_module_1' ;
30
30
libName = '@r2don/nest-http-interface' ;
31
31
importSet = new Set < string > ( ) ;
32
+ typeFormatFlag =
33
+ ts . TypeFormatFlags . UseTypeOfFunction |
34
+ ts . TypeFormatFlags . NoTruncation |
35
+ ts . TypeFormatFlags . UseFullyQualifiedType |
36
+ ts . TypeFormatFlags . WriteTypeArgumentsOfSignature ;
32
37
33
38
visit (
34
39
sourceFile : ts . SourceFile ,
35
40
ctx : ts . TransformationContext ,
36
- _program : ts . Program ,
41
+ program : ts . Program ,
37
42
) : ts . Node {
38
43
this . importSet . clear ( ) ;
39
44
const factory = ctx . factory ;
45
+ const typeChecker = program . getTypeChecker ( ) ;
40
46
const visitNode = ( node : ts . Node ) : ts . Node => {
41
47
if ( this . isHttpInterfaceClass ( node ) ) {
42
- return this . visitMethods ( node , factory ) ;
48
+ return this . visitMethods ( node , factory , typeChecker ) ;
43
49
}
44
50
45
51
if ( ts . isSourceFile ( node ) ) {
@@ -84,13 +90,14 @@ export class HttpInterfaceVisitor {
84
90
private visitMethods (
85
91
node : ts . ClassDeclaration ,
86
92
factory : ts . NodeFactory ,
93
+ typeChecker : ts . TypeChecker ,
87
94
) : ts . ClassDeclaration {
88
95
const updatedMembers = node . members . map ( ( member ) => {
89
96
if ( ! this . isExchangeMethod ( member ) ) {
90
97
return member ;
91
98
}
92
99
93
- return this . appendResponseBodyDecorator ( member , factory ) ;
100
+ return this . appendResponseBodyDecorator ( member , factory , typeChecker ) ;
94
101
} ) ;
95
102
96
103
return factory . updateClassDeclaration (
@@ -113,8 +120,9 @@ export class HttpInterfaceVisitor {
113
120
private appendResponseBodyDecorator (
114
121
node : ts . MethodDeclaration ,
115
122
factory : ts . NodeFactory ,
123
+ typeChecker : ts . TypeChecker ,
116
124
) : ts . MethodDeclaration {
117
- const returnType = this . getReturnTypeAsText ( node ) ;
125
+ const returnType = this . getReturnTypeAsText ( node , typeChecker ) ;
118
126
119
127
if ( returnType == null ) {
120
128
return node ;
@@ -146,7 +154,10 @@ export class HttpInterfaceVisitor {
146
154
) ;
147
155
}
148
156
149
- private getReturnTypeAsText ( node : ts . MethodDeclaration ) : string | undefined {
157
+ private getReturnTypeAsText (
158
+ node : ts . MethodDeclaration ,
159
+ typeChecker : ts . TypeChecker ,
160
+ ) : string | undefined {
150
161
if (
151
162
node . type == null ||
152
163
! ts . isTypeReferenceNode ( node . type ) ||
@@ -165,7 +176,13 @@ export class HttpInterfaceVisitor {
165
176
return undefined ;
166
177
}
167
178
168
- return innerType . typeName . text ;
179
+ return typeChecker
180
+ . typeToString (
181
+ typeChecker . getTypeAtLocation ( innerType ) ,
182
+ undefined ,
183
+ this . typeFormatFlag ,
184
+ )
185
+ . replace ( 'import' , 'require' ) ;
169
186
}
170
187
171
188
private getInnerType ( node ?: ts . TypeNode ) : ts . TypeNode | undefined {
0 commit comments