@@ -33,6 +33,11 @@ public static int GenerateJavaScript(JavaScriptGeneratorSettings settings) =>
33
33
/// </summary>
34
34
public bool Express { get ; set ; }
35
35
36
+ /// <summary>
37
+ /// True to generate Fastify plugin.
38
+ /// </summary>
39
+ public bool Fastify { get ; set ; }
40
+
36
41
/// <summary>
37
42
/// True to disable ESLint via code comment.
38
43
/// </summary>
@@ -46,6 +51,7 @@ public override CodeGenOutput GenerateOutput(ServiceInfo service)
46
51
var httpServiceInfo = HttpServiceInfo . Create ( service ) ;
47
52
48
53
var moduleName = ModuleName ?? service . Name ;
54
+ var camelCaseModuleName = CodeGenUtility . ToCamelCase ( moduleName ) ;
49
55
var capModuleName = CodeGenUtility . Capitalize ( moduleName ) ;
50
56
var typesFileName = CodeGenUtility . Uncapitalize ( moduleName ) + "Types" + ( TypeScript ? ".ts" : ".js" ) ;
51
57
var clientFileName = CodeGenUtility . Uncapitalize ( moduleName ) + ( TypeScript ? ".ts" : ".js" ) ;
@@ -606,6 +612,90 @@ public override CodeGenOutput GenerateOutput(ServiceInfo service)
606
612
} ) ) ;
607
613
}
608
614
615
+ if ( Fastify )
616
+ {
617
+ var pluginFileName = CodeGenUtility . Uncapitalize ( moduleName ) + "Plugin" + ( TypeScript ? ".ts" : ".js" ) ;
618
+ namedTexts . Add ( CreateFile ( "fastify/" + pluginFileName , code =>
619
+ {
620
+ WriteFileHeader ( code ) ;
621
+
622
+ if ( ! TypeScript )
623
+ code . WriteLine ( "'use strict';" ) ;
624
+
625
+ code . WriteLine ( ) ;
626
+
627
+ var fastifyImports = new List < string > ( ) ;
628
+ if ( TypeScript )
629
+ fastifyImports . Add ( "FastifyPluginAsync" ) ;
630
+ WriteImports ( code , fastifyImports , "fastify" ) ;
631
+
632
+ var facilityImports = new List < string > ( ) ;
633
+ if ( TypeScript )
634
+ facilityImports . Add ( "IServiceResult" ) ;
635
+ WriteImports ( code , facilityImports , "facility-core" ) ;
636
+ if ( TypeScript )
637
+ {
638
+ WriteImports ( code , typeNames , $ "../{ CodeGenUtility . Uncapitalize ( moduleName ) } Types") ;
639
+ code . WriteLine ( $ "export * from '../{ CodeGenUtility . Uncapitalize ( moduleName ) } Types';") ;
640
+ }
641
+
642
+ // TODO: export this from facility-core
643
+ code . WriteLine ( ) ;
644
+ using ( code . Block ( "const standardErrorCodes" + IfTypeScript ( ": { [code: string]: number }" ) + " = {" , "};" ) )
645
+ {
646
+ code . WriteLine ( "'NotModified': 304," ) ;
647
+ code . WriteLine ( "'InvalidRequest': 400," ) ;
648
+ code . WriteLine ( "'NotAuthenticated': 401," ) ;
649
+ code . WriteLine ( "'NotAuthorized': 403," ) ;
650
+ code . WriteLine ( "'NotFound': 404," ) ;
651
+ code . WriteLine ( "'Conflict': 409," ) ;
652
+ code . WriteLine ( "'RequestTooLarge': 413," ) ;
653
+ code . WriteLine ( "'TooManyRequests': 429," ) ;
654
+ code . WriteLine ( "'InternalError': 500," ) ;
655
+ code . WriteLine ( "'ServiceUnavailable': 503," ) ;
656
+
657
+ foreach ( var errorSetInfo in httpServiceInfo . ErrorSets )
658
+ {
659
+ foreach ( var error in errorSetInfo . Errors )
660
+ {
661
+ code . WriteLine ( $ "'{ error . ServiceError . Name } ': { ( int ) error . StatusCode } ,") ;
662
+ }
663
+ }
664
+ }
665
+
666
+ // TODO: export this from facility-core?
667
+ code . WriteLine ( ) ;
668
+ using ( code . Block ( "function parseBoolean(value" + IfTypeScript ( ": string | undefined" ) + ") {" , "}" ) )
669
+ {
670
+ using ( code . Block ( "if (typeof value === 'string') {" , "}" ) )
671
+ {
672
+ code . WriteLine ( "const lowerValue = value.toLowerCase();" ) ;
673
+ using ( code . Block ( "if (lowerValue === 'true') {" , "}" ) )
674
+ code . WriteLine ( "return true;" ) ;
675
+ using ( code . Block ( "if (lowerValue === 'false') {" , "}" ) )
676
+ code . WriteLine ( "return false;" ) ;
677
+ }
678
+ code . WriteLine ( "return undefined;" ) ;
679
+ }
680
+
681
+ if ( TypeScript )
682
+ {
683
+ code . WriteLine ( ) ;
684
+ /* All code below this is meant to create the code that is in handRollingPlugin.ts */
685
+ using ( code . Block ( $ "export type { capModuleName } PluginOptions = {{", "}" ) )
686
+ {
687
+ code . WriteLine ( $ "api: I{ capModuleName } ;") ;
688
+ }
689
+ }
690
+
691
+ code . WriteLine ( ) ;
692
+ using ( code . Block ( $ "export const { camelCaseModuleName } Plugin: FastifyPluginAsync<{ capModuleName } PluginOptions> = async (fastify, opts) => {{", "}" ) )
693
+ {
694
+ code . WriteLine ( "throw new Error('Not implemented');" ) ;
695
+ }
696
+ } ) ) ;
697
+ }
698
+
609
699
return new CodeGenOutput ( namedTexts , new List < CodeGenPattern > ( ) ) ;
610
700
}
611
701
@@ -618,6 +708,7 @@ public override void ApplySettings(FileGeneratorSettings settings)
618
708
ModuleName = ourSettings . ModuleName ;
619
709
TypeScript = ourSettings . TypeScript ;
620
710
Express = ourSettings . Express ;
711
+ Fastify = ourSettings . Fastify ;
621
712
DisableESLint = ourSettings . DisableESLint ;
622
713
}
623
714
0 commit comments