Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,7 @@ private static string RenderUriComponent(ServiceFieldInfo field, ServiceInfo ser
switch (fieldTypeKind)
{
case ServiceTypeKind.Enum:
case ServiceTypeKind.ExternalEnum:
return $"request.{fieldName}";
case ServiceTypeKind.String:
case ServiceTypeKind.Bytes:
Expand All @@ -987,6 +988,7 @@ private static string RenderUriComponent(ServiceFieldInfo field, ServiceInfo ser
case ServiceTypeKind.Double:
return $"encodeURIComponent(request.{fieldName}.toString())";
case ServiceTypeKind.Dto:
case ServiceTypeKind.ExternalDto:
case ServiceTypeKind.Error:
case ServiceTypeKind.Object:
throw new NotSupportedException("Field type not supported on path/query: " + fieldTypeKind);
Expand All @@ -1002,6 +1004,7 @@ private string ParseFieldValue(ServiceFieldInfo field, ServiceInfo service, stri
switch (fieldTypeKind)
{
case ServiceTypeKind.Enum:
case ServiceTypeKind.ExternalEnum:
return $"{value}{IfTypeScript($" as {field.TypeName}")}";
case ServiceTypeKind.String:
case ServiceTypeKind.Bytes:
Expand All @@ -1016,6 +1019,7 @@ private string ParseFieldValue(ServiceFieldInfo field, ServiceInfo service, stri
case ServiceTypeKind.Double:
return $"parseFloat({value})";
case ServiceTypeKind.Dto:
case ServiceTypeKind.ExternalDto:
case ServiceTypeKind.Error:
case ServiceTypeKind.Object:
throw new NotSupportedException("Field type not supported on path/query/header: " + fieldTypeKind);
Expand All @@ -1031,6 +1035,7 @@ private static string RenderFieldValue(ServiceFieldInfo field, ServiceInfo servi
switch (fieldTypeKind)
{
case ServiceTypeKind.Enum:
case ServiceTypeKind.ExternalEnum:
case ServiceTypeKind.String:
case ServiceTypeKind.Bytes:
case ServiceTypeKind.DateTime:
Expand All @@ -1042,6 +1047,7 @@ private static string RenderFieldValue(ServiceFieldInfo field, ServiceInfo servi
case ServiceTypeKind.Double:
return $"{value}.toString()";
case ServiceTypeKind.Dto:
case ServiceTypeKind.ExternalDto:
case ServiceTypeKind.Error:
case ServiceTypeKind.Object:
throw new NotSupportedException("Field type not supported on path/query: " + fieldTypeKind);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,42 @@ public void GenerateExampleApiTypeScript_ExternEnumWithNameAndModule()
Assert.That(typesFile.Text, Does.Contain("thing?: Thing;"));
}

[Test]
public void GenerateExampleApiTypeScript_ExternEnumAsUriParam()
{
const string definition = "[csharp] service TestApi { [js(name: \"SomeExternalEnum\", module: \"extern-enum-module\")] extern enum Thing; [http(method: GET, path: \"/myMethod\")] method myMethod { e: Thing; }: {} }";
var parser = new FsdParser();
var service = parser.ParseDefinition(new ServiceDefinitionText("TestApi.fsd", definition));
var generator = new JavaScriptGenerator { GeneratorName = "JavaScriptGeneratorTests", TypeScript = true, Express = true };
var result = generator.GenerateOutput(service);
Assert.That(result, Is.Not.Null);

var typesFile = result.Files.Single(f => f.Name == "testApiTypes.ts");
Assert.That(typesFile.Text, Does.Contain("export interface IMyMethodRequest"));
Assert.That(typesFile.Text, Does.Contain("e?: Thing;"));

var serverFile = result.Files.Single(f => f.Name == "testApiServer.ts");
Assert.That(serverFile.Text, Does.Contain("request.e = req.query['e'] as Thing;"));
}

[Test]
public void GenerateExampleApiTypeScript_ExternEnumAsHeader()
{
const string definition = "[csharp] service TestApi { [js(name: \"SomeExternalEnum\", module: \"extern-enum-module\")] extern enum Thing; [http(method: GET, path: \"/myMethod\")] method myMethod { [http(from: header, name: \"Thing-Header\")] e: Thing; }: {} }";
var parser = new FsdParser();
var service = parser.ParseDefinition(new ServiceDefinitionText("TestApi.fsd", definition));
var generator = new JavaScriptGenerator { GeneratorName = "JavaScriptGeneratorTests", TypeScript = true, Express = true };
var result = generator.GenerateOutput(service);
Assert.That(result, Is.Not.Null);

var typesFile = result.Files.Single(f => f.Name == "testApiTypes.ts");
Assert.That(typesFile.Text, Does.Contain("export interface IMyMethodRequest"));
Assert.That(typesFile.Text, Does.Contain("e?: Thing;"));

var serverFile = result.Files.Single(f => f.Name == "testApiServer.ts");
Assert.That(serverFile.Text, Does.Contain("request.e = req.header('Thing-Header');"));
}

[Test]
public void GenerateExampleApiTypeScript_ExternEnumWithoutJsAttribute()
{
Expand Down