11using Mono . Cecil ;
2+ using System ;
23using System . Collections . Generic ;
4+ using System . Linq ;
5+ using WebApiToTypeScript . Config ;
6+ using WebApiToTypeScript . Enums ;
7+ using WebApiToTypeScript . Interfaces ;
8+ using WebApiToTypeScript . Types ;
39
410namespace WebApiToTypeScript . WebApi
511{
6- public class WebApiRoutePart
12+ public class WebApiRoutePart : ServiceAware
713 {
814 public string Name { get ; set ; }
915 public string ParameterName { get ; set ; }
@@ -14,5 +20,110 @@ public class WebApiRoutePart
1420
1521 public List < string > CustomAttributes { get ; set ; }
1622 = new List < string > ( ) ;
23+
24+ public TypeScriptType GetTypeScriptType ( )
25+ {
26+ var result = new TypeScriptType ( ) ;
27+
28+ var parameter = Parameter ;
29+ var type = parameter . ParameterType ;
30+ var typeName = type . FullName ;
31+
32+ var typeMapping = GetTypeMapping ( ) ;
33+
34+ if ( typeMapping != null )
35+ {
36+ var tsTypeName = typeMapping . TypeScriptTypeName ;
37+ result . TypeName = tsTypeName ;
38+ result . IsPrimitive = TypeService . IsPrimitiveTypeScriptType ( result . TypeName ) ;
39+ result . IsEnum = tsTypeName . StartsWith ( $ "{ Config . EnumsNamespace } ")
40+ || result . IsPrimitive ;
41+
42+ return result ;
43+ }
44+
45+ typeName = TypeService . StripNullable ( type ) ?? typeName ;
46+
47+ var collectionType = TypeService . StripCollection ( type ) ;
48+ result . IsCollection = collectionType != null ;
49+ typeName = collectionType ?? typeName ;
50+
51+ var typeDefinition = TypeService . GetTypeDefinition ( typeName ) ;
52+
53+ if ( typeDefinition ? . IsEnum ?? false )
54+ {
55+ if ( ! Config . GenerateEnums )
56+ {
57+ result . TypeName = "number" ;
58+ result . IsPrimitive = true ;
59+ }
60+ else
61+ {
62+ EnumsService . AddEnum ( typeDefinition ) ;
63+
64+ result . TypeName = $ "{ Config . EnumsNamespace } .{ typeDefinition . Name } ";
65+ result . IsPrimitive = false ;
66+ }
67+
68+ result . IsEnum = true ;
69+ return result ;
70+ }
71+
72+ var primitiveType = TypeService . GetPrimitiveTypeScriptType ( typeName ) ;
73+
74+ if ( ! string . IsNullOrEmpty ( primitiveType ) )
75+ {
76+ result . TypeName = primitiveType ;
77+ result . IsPrimitive = true ;
78+
79+ return result ;
80+ }
81+
82+ if ( ! typeDefinition ? . IsValueType ?? false )
83+ {
84+ if ( ! Config . GenerateInterfaces )
85+ {
86+ result . TypeName = $ "{ WebApiToTypeScript . IHaveQueryParams } ";
87+ }
88+ else
89+ {
90+ InterfaceService . AddInterfaceNode ( typeDefinition ) ;
91+
92+ result . TypeName = $ "{ Config . InterfacesNamespace } .{ typeDefinition . Name } ";
93+ }
94+
95+ return result ;
96+ }
97+
98+ throw new NotSupportedException ( "Maybe it is a generic class, or a yet unsupported collection, or chain thereof?" ) ;
99+ }
100+
101+ public string GetParameterString ( bool withOptionals = true )
102+ {
103+ var parameter = Parameter ;
104+ var isOptional = withOptionals && TypeService . IsParameterOptional ( parameter ) ;
105+ var typeScriptType = GetTypeScriptType ( ) ;
106+
107+ var collectionString = typeScriptType . IsCollection ? "[]" : string . Empty ;
108+
109+ return $ "{ parameter . Name } { ( isOptional ? "?" : "" ) } : { typeScriptType . TypeName } { collectionString } ";
110+ }
111+
112+ public TypeMapping GetTypeMapping ( )
113+ {
114+ if ( Parameter == null )
115+ return null ;
116+
117+ var typeName = Parameter . ParameterType . FullName ;
118+
119+ var typeMapping = Config . TypeMappings
120+ . SingleOrDefault ( t => typeName . StartsWith ( t . WebApiTypeName )
121+ || ( t . TreatAsAttribute
122+ && ( Helpers . HasCustomAttribute ( Parameter , $ "{ t . WebApiTypeName } Attribute") )
123+ || ( t . TreatAsConstraint
124+ && Constraints . Any ( c => c == Helpers . ToCamelCase ( t . WebApiTypeName ) ) ) ) ) ;
125+
126+ return typeMapping ;
127+ }
17128 }
18129}
0 commit comments