@@ -41,15 +41,20 @@ const EdmTypeMap = new Map([
41
41
]
42
42
] ) ;
43
43
44
+ const Space = ' ' ;
45
+ const OpeningBracket = '{' ;
46
+ const ClosingBracket = '}' ;
47
+ const NewLine = '\n' ;
48
+ const Tab = '\t' ;
49
+
44
50
class TypeRenderer {
45
51
46
52
protected context : BasicDataContext ;
47
53
protected schema : EdmSchema ;
48
54
49
- /**
50
- * @param {string } host
51
- */
52
- constructor ( host ?: string ) {
55
+ constructor ( host ?: string , protected options ?: {
56
+ classes : boolean
57
+ } ) {
53
58
this . context = new BasicDataContext ( host ) ;
54
59
}
55
60
@@ -87,7 +92,6 @@ class TypeRenderer {
87
92
* @returns string
88
93
*/
89
94
protected renderType ( entityType : EdmEntityType ) {
90
- const extendsInterface = entityType . BaseType ? ` extends ${ entityType . BaseType } ` : '' ;
91
95
const properties = entityType . Property . map ( ( property ) => {
92
96
const { Name } = property ;
93
97
const Declaration = this . renderProperty ( property ) ;
@@ -104,15 +108,32 @@ class TypeRenderer {
104
108
Declaration
105
109
}
106
110
} ) ) ;
107
- const result = `
108
- export interface ${ entityType . Name } ${ extendsInterface } {
109
- ${ properties . sort (
110
- ( a , b ) => {
111
- if ( a . Name < b . Name ) return - 1 ;
112
- if ( a . Name > b . Name ) return 1 ;
113
- return 0 ;
114
- } ) . map ( ( property ) => `\t${ property . Declaration } ` ) . join ( '\n' ) }
115
- }`
111
+ let result = '' ;
112
+ if ( this . options && this . options . classes ) {
113
+ // find entity set and define annotation
114
+ const entitySet = this . schema . EntityContainer . EntitySet . find ( ( s ) => s . EntityType === entityType . Name ) ;
115
+ if ( entitySet ) {
116
+ result += `@EdmSchema.entitySet('${ entitySet . Name } ')` ;
117
+ result += NewLine ;
118
+ }
119
+ }
120
+ result += 'export' ;
121
+ result += Space ;
122
+ result += this . options && this . options . classes ? 'class' : 'interface' ;
123
+ result += Space ;
124
+ result += entityType . Name ;
125
+ result += Space ;
126
+ result += entityType . BaseType ? `extends ${ entityType . BaseType } ` : '' ;
127
+ result += Space ;
128
+ result += OpeningBracket ;
129
+ result += NewLine ;
130
+ result += properties . sort ( ( a , b ) => {
131
+ if ( a . Name < b . Name ) return - 1 ;
132
+ if ( a . Name > b . Name ) return 1 ;
133
+ return 0 ;
134
+ } ) . map ( ( property ) => Tab + `${ property . Declaration } ` ) . join ( NewLine ) ;
135
+ result += NewLine ;
136
+ result += ClosingBracket ;
116
137
return result . replace ( / ( \n + ) / g, '\n' ) ;
117
138
}
118
139
@@ -132,20 +153,63 @@ ${properties.sort(
132
153
if ( this . schema == null ) {
133
154
this . schema = await this . getSchema ( ) ;
134
155
}
135
- const typeDeclarations = this . schema . EntityType . sort (
136
- ( a , b ) => {
137
- if ( a . Name < b . Name ) return - 1 ;
138
- if ( a . Name > b . Name ) return 1 ;
139
- return 0 ;
156
+
157
+ const sort = ( a : string , b : string ) => {
158
+ if ( a < b ) return - 1 ;
159
+ if ( a > b ) return 1 ;
160
+ return 0 ;
161
+ }
162
+
163
+ // sort entity types
164
+ const withoutBaseType = this . schema . EntityType
165
+ . filter ( ( t ) => t . BaseType == null )
166
+ . map ( ( t ) => t . Name )
167
+ . sort ( ( a , b ) => sort ( a , b ) ) ;
168
+
169
+ const names = [ ] ;
170
+ names . push ( ...withoutBaseType ) ;
171
+ const withBaseType = this . schema . EntityType
172
+ . filter ( ( t ) => t . BaseType != null )
173
+ . map ( ( t ) => t . Name )
174
+ . sort ( ( a , b ) => sort ( a , b ) ) ;
175
+ // sort entity types with base type and find base type path e.g. Action/RequestAction/StudentRequestAction
176
+ const withTypes = withBaseType . map ( ( t ) => {
177
+ let entityType = this . schema . EntityType . find ( ( e ) => e . Name === t )
178
+ let baseType = entityType . BaseType ;
179
+ let types = [
180
+ t
181
+ ] ;
182
+ while ( baseType ) {
183
+ types . push ( baseType ) ;
184
+ entityType = this . schema . EntityType . find ( ( e ) => e . Name === baseType ) ;
185
+ baseType = entityType ? entityType . BaseType : null ;
186
+ }
187
+ return {
188
+ Name : t ,
189
+ Types : types . reverse ( ) . join ( '/' )
140
190
}
191
+ } ) . sort ( ( a , b ) => sort ( a . Types , b . Types ) ) . map ( ( t ) => t . Name ) ;
192
+ names . push ( ...withTypes ) ;
193
+ const typeDeclarations = names . map (
194
+ ( name : string ) => this . schema . EntityType . find ( ( t ) => t . Name === name )
141
195
) . map ( ( entityType ) => this . renderType ( entityType ) ) ;
142
- return typeDeclarations . join ( '\n' ) ;
196
+ let result = '' ;
197
+ if ( this . options && this . options . classes ) {
198
+ result += 'import { EdmSchema } from \'@themost/client\';' ;
199
+ result += NewLine ;
200
+ result += NewLine ;
201
+ }
202
+ result += typeDeclarations . join ( NewLine + NewLine ) ;
203
+ return result ;
143
204
}
144
205
}
145
206
146
207
class FileSchemaRenderer extends TypeRenderer {
147
- constructor ( private file : string ) {
208
+ constructor ( private file : string , options ?: {
209
+ classes : boolean
210
+ } ) {
148
211
super ( ) ;
212
+ this . options = options ;
149
213
}
150
214
151
215
protected getSchema ( ) : Promise < EdmSchema > {
0 commit comments