Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 724ddca

Browse files
committed
Generate fastify plugin shell.
And generate conformance plugin.
1 parent b4f1516 commit 724ddca

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// DO NOT EDIT: generated by fsdgenjs
2+
/* eslint-disable */
3+
4+
import { FastifyPluginAsync } from 'fastify';
5+
import { IServiceResult } from 'facility-core';
6+
import { IConformanceApi, IGetApiInfoRequest, IGetApiInfoResponse, IGetWidgetsRequest, IGetWidgetsResponse, ICreateWidgetRequest, ICreateWidgetResponse, IGetWidgetRequest, IGetWidgetResponse, IDeleteWidgetRequest, IDeleteWidgetResponse, IGetWidgetBatchRequest, IGetWidgetBatchResponse, IMirrorFieldsRequest, IMirrorFieldsResponse, ICheckQueryRequest, ICheckQueryResponse, ICheckPathRequest, ICheckPathResponse, IMirrorHeadersRequest, IMirrorHeadersResponse, IMixedRequest, IMixedResponse, IRequiredRequest, IRequiredResponse, IMirrorBytesRequest, IMirrorBytesResponse, IMirrorTextRequest, IMirrorTextResponse, IBodyTypesRequest, IBodyTypesResponse, IWidget, IAny, IAnyArray, IAnyMap, IAnyResult, IAnyNullable, IHasWidget, Answer, ApiErrors } from '../conformanceApiTypes';
7+
export * from '../conformanceApiTypes';
8+
9+
const standardErrorCodes: { [code: string]: number } = {
10+
'NotModified': 304,
11+
'InvalidRequest': 400,
12+
'NotAuthenticated': 401,
13+
'NotAuthorized': 403,
14+
'NotFound': 404,
15+
'Conflict': 409,
16+
'RequestTooLarge': 413,
17+
'TooManyRequests': 429,
18+
'InternalError': 500,
19+
'ServiceUnavailable': 503,
20+
'NotAdmin': 403,
21+
'TooHappy': 500,
22+
};
23+
24+
function parseBoolean(value: string | undefined) {
25+
if (typeof value === 'string') {
26+
const lowerValue = value.toLowerCase();
27+
if (lowerValue === 'true') {
28+
return true;
29+
}
30+
if (lowerValue === 'false') {
31+
return false;
32+
}
33+
}
34+
return undefined;
35+
}
36+
37+
export type ConformanceApiPluginOptions = {
38+
api: IConformanceApi;
39+
}
40+
41+
export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOptions> = async (fastify, opts) => {
42+
throw new Error('Not implemented');
43+
}

src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public static int GenerateJavaScript(JavaScriptGeneratorSettings settings) =>
3333
/// </summary>
3434
public bool Express { get; set; }
3535

36+
/// <summary>
37+
/// True to generate Fastify plugin.
38+
/// </summary>
39+
public bool Fastify { get; set; }
40+
3641
/// <summary>
3742
/// True to disable ESLint via code comment.
3843
/// </summary>
@@ -46,6 +51,7 @@ public override CodeGenOutput GenerateOutput(ServiceInfo service)
4651
var httpServiceInfo = HttpServiceInfo.Create(service);
4752

4853
var moduleName = ModuleName ?? service.Name;
54+
var camelCaseModuleName = CodeGenUtility.ToCamelCase(moduleName);
4955
var capModuleName = CodeGenUtility.Capitalize(moduleName);
5056
var typesFileName = CodeGenUtility.Uncapitalize(moduleName) + "Types" + (TypeScript ? ".ts" : ".js");
5157
var clientFileName = CodeGenUtility.Uncapitalize(moduleName) + (TypeScript ? ".ts" : ".js");
@@ -606,6 +612,90 @@ public override CodeGenOutput GenerateOutput(ServiceInfo service)
606612
}));
607613
}
608614

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+
609699
return new CodeGenOutput(namedTexts, new List<CodeGenPattern>());
610700
}
611701

@@ -618,6 +708,7 @@ public override void ApplySettings(FileGeneratorSettings settings)
618708
ModuleName = ourSettings.ModuleName;
619709
TypeScript = ourSettings.TypeScript;
620710
Express = ourSettings.Express;
711+
Fastify = ourSettings.Fastify;
621712
DisableESLint = ourSettings.DisableESLint;
622713
}
623714

src/Facility.CodeGen.JavaScript/JavaScriptGeneratorSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public sealed class JavaScriptGeneratorSettings : FileGeneratorSettings
2222
/// </summary>
2323
public bool Express { get; set; }
2424

25+
/// <summary>
26+
/// True to generate Fastify plugin.
27+
/// </summary>
28+
public bool Fastify { get; set; }
29+
2530
/// <summary>
2631
/// True to disable ESLint via code comment.
2732
/// </summary>

src/fsdgenjs/FsdGenJavaScriptApp.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public sealed class FsdGenJavaScriptApp : CodeGeneratorApp
2222
" Generates TypeScript.",
2323
" --express",
2424
" Generates Express service.",
25+
" --fastify",
26+
" Generates Fastify plugin.",
2527
" --disable-eslint",
2628
" Disables ESLint via code comment.",
2729
};
@@ -34,6 +36,7 @@ protected override FileGeneratorSettings CreateSettings(ArgsReader args) =>
3436
ModuleName = args.ReadOption("module"),
3537
TypeScript = args.ReadFlag("typescript"),
3638
Express = args.ReadFlag("express"),
39+
Fastify = args.ReadFlag("fastify"),
3740
DisableESLint = args.ReadFlag("disable-eslint"),
3841
};
3942
}

tools/Build/Build.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void CodeGen(bool verify)
4646

4747
RunDotNet("FacilityConformance", "fsd", "--output", "conformance/ConformanceApi.fsd", verifyOption);
4848
RunDotNet("FacilityConformance", "json", "--output", "conformance/ConformanceTests.json", verifyOption);
49-
RunCodeGen("conformance/ConformanceApi.fsd", "conformance/ts/src/", "--typescript", "--indent", "2", "--disable-eslint");
49+
RunCodeGen("conformance/ConformanceApi.fsd", "conformance/ts/src/", "--typescript", "--fastify", "--indent", "2", "--disable-eslint");
5050

5151
void RunCodeGen(params string?[] args) =>
5252
RunDotNet(new[] { "run", "--no-build", "--project", $"src/{codegen}", "-f", "net6.0", "-c", configuration, "--", "--newline", "lf", verifyOption }.Concat(args));

0 commit comments

Comments
 (0)