2
2
using System . Collections . Generic ;
3
3
using System . Globalization ;
4
4
using System . Linq ;
5
- using System . Net ;
6
5
using Facility . Definition ;
7
6
using Facility . Definition . CodeGen ;
8
7
using Facility . Definition . Http ;
@@ -51,7 +50,7 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)
51
50
code . WriteLine ( $ "// DO NOT EDIT: generated by { GeneratorName } ") ;
52
51
53
52
code . WriteLine ( ) ;
54
- code . WriteLine ( "import { IServiceResult, IServiceError } from ' facility-core'; ") ;
53
+ WriteImports ( code , new [ ] { " IServiceResult" , " IServiceError" } , " facility-core") ;
55
54
56
55
code . WriteLine ( ) ;
57
56
WriteJSDoc ( code , service ) ;
@@ -96,7 +95,6 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)
96
95
} ) ) ;
97
96
}
98
97
99
-
100
98
namedTexts . Add ( CreateNamedText ( clientFileName , code =>
101
99
{
102
100
code . WriteLine ( $ "// DO NOT EDIT: generated by { GeneratorName } ") ;
@@ -105,10 +103,14 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)
105
103
code . WriteLine ( "'use strict';" ) ;
106
104
107
105
code . WriteLine ( ) ;
108
- code . WriteLine ( "import { HttpClientUtility" + IfTypeScript ( ", IServiceResult, IServiceError, IHttpClientOptions" ) + " } from 'facility-core';" ) ;
106
+ var facilityImports = new List < string > { "HttpClientUtility" } ;
107
+ if ( TypeScript )
108
+ facilityImports . AddRange ( new [ ] { "IServiceResult" , "IServiceError" , "IHttpClientOptions" } ) ;
109
+ WriteImports ( code , facilityImports , "facility-core" ) ;
110
+
109
111
if ( TypeScript )
110
112
{
111
- code . WriteLine ( $ "import {{ { string . Join ( ", " , typeNames ) } }} from ' ./{ Uncapitalize ( moduleName ) } Types'; ") ;
113
+ WriteImports ( code , typeNames , $ " ./{ Uncapitalize ( moduleName ) } Types") ;
112
114
code . WriteLine ( $ "export * from './{ Uncapitalize ( moduleName ) } Types';") ;
113
115
}
114
116
@@ -303,10 +305,13 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)
303
305
code . WriteLine ( ) ;
304
306
code . WriteLine ( "import * as bodyParser from 'body-parser';" ) ;
305
307
code . WriteLine ( "import * as express from 'express';" ) ;
306
- code . WriteLine ( "import {" + IfTypeScript ( " IServiceResult, IServiceError" ) + " } from 'facility-core';" ) ;
308
+ var facilityImports = new List < string > ( ) ;
309
+ if ( TypeScript )
310
+ facilityImports . AddRange ( new [ ] { "IServiceResult" , "IServiceError" } ) ;
311
+ WriteImports ( code , facilityImports , "facility-core" ) ;
307
312
if ( TypeScript )
308
313
{
309
- code . WriteLine ( $ "import {{ { string . Join ( ", " , typeNames ) } }} from ' ./{ Uncapitalize ( moduleName ) } Types'; ") ;
314
+ WriteImports ( code , typeNames , $ " ./{ Uncapitalize ( moduleName ) } Types") ;
310
315
code . WriteLine ( $ "export * from './{ Uncapitalize ( moduleName ) } Types';") ;
311
316
}
312
317
@@ -423,12 +428,12 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)
423
428
var bodyFieldType = service . GetFieldType ( bodyField . ServiceField ) ;
424
429
if ( bodyFieldType . Kind == ServiceTypeKind . Boolean )
425
430
{
426
- code . WriteLine ( $ "res.sendStatus({ ( int ) validResponse . StatusCode } );") ;
431
+ code . WriteLine ( $ "res.sendStatus({ ( int ) validResponse . StatusCode } );") ;
427
432
code . WriteLine ( "return;" ) ;
428
433
}
429
434
else
430
435
{
431
- code . WriteLine ( $ "res.status({ ( int ) validResponse . StatusCode } ).send(result.value.{ responseBodyFieldName } );") ;
436
+ code . WriteLine ( $ "res.status({ ( int ) validResponse . StatusCode } ).send(result.value.{ responseBodyFieldName } );") ;
432
437
code . WriteLine ( "return;" ) ;
433
438
}
434
439
}
@@ -437,15 +442,15 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)
437
442
{
438
443
if ( validResponse . NormalFields . Count == 0 )
439
444
{
440
- code . WriteLine ( $ "res.sendStatus({ ( int ) validResponse . StatusCode } );") ;
445
+ code . WriteLine ( $ "res.sendStatus({ ( int ) validResponse . StatusCode } );") ;
441
446
code . WriteLine ( "return;" ) ;
442
447
}
443
448
}
444
449
}
445
450
446
451
foreach ( var validResponse in httpMethodInfo . ValidResponses . Where ( x => x . NormalFields != null && x . NormalFields . Count != 0 ) )
447
452
{
448
- code . WriteLine ( $ "res.status({ ( int ) validResponse . StatusCode } ).send({{") ;
453
+ code . WriteLine ( $ "res.status({ ( int ) validResponse . StatusCode } ).send({{") ;
449
454
using ( code . Indent ( ) )
450
455
{
451
456
foreach ( var field in validResponse . NormalFields )
@@ -557,7 +562,6 @@ private string RenderUriComponent(ServiceFieldInfo field, ServiceInfo service)
557
562
private string RenderJsConversion ( ServiceFieldInfo field , ServiceInfo service , string value )
558
563
{
559
564
var fieldTypeKind = service . GetFieldType ( field ) . Kind ;
560
- var fieldName = field . Name ;
561
565
562
566
switch ( fieldTypeKind )
563
567
{
@@ -612,6 +616,12 @@ private static void WriteJSDoc(CodeWriter code, IReadOnlyList<string> lines)
612
616
}
613
617
}
614
618
619
+ private static void WriteImports ( CodeWriter code , IReadOnlyList < string > imports , string from )
620
+ {
621
+ if ( imports . Count != 0 )
622
+ code . WriteLine ( $ "import {{ { string . Join ( ", " , imports ) } }} from '{ from } ';") ;
623
+ }
624
+
615
625
private static string Uncapitalize ( string value )
616
626
{
617
627
return value . Length == 0 ? "" : value . Substring ( 0 , 1 ) . ToLowerInvariant ( ) + value . Substring ( 1 ) ;
0 commit comments