Bumped package version to 1.5.0, changed Program.cs a little of the example. Improved the Javascript Client generator by exporting all types and changing enum to behave in a more useful way.

This commit is contained in:
2023-04-13 15:00:24 -07:00
parent e2c5aba868
commit fe5fc73d14
3 changed files with 103 additions and 83 deletions

View File

@@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.4.9</Version>
<Version>1.5.0</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup>

View File

@@ -62,8 +62,12 @@ namespace MontoyaTech.Rest.Net
//Create the client constructor or init method
if (this.StaticCode)
{
writer.WriteBreak().WriteLine("/** @param {string} baseUrl */");
writer.Write("static init(baseUrl) ").WriteLine("{").Indent();
writer.WriteBreak().WriteLine("/**").Indent();
writer.WriteLine("Initializes the api client with a given baseUrl of where to send requests.");
writer.WriteLine("@param {string} baseUrl");
writer.Outdent().WriteLine("*/");
writer.Write("static Init(baseUrl) ").WriteLine("{").Indent();
//Make sure the baseUrl isn't null or whitespace
writer.WriteBreak().WriteLine("if (baseUrl == null || baseUrl == undefined || baseUrl.trim() == '') {").Indent();
@@ -116,7 +120,19 @@ namespace MontoyaTech.Rest.Net
writer.WriteBreak().WriteLine($"window.{this.ClientName} = {this.ClientName};");
writer.WriteBreak().WriteLine($"export {{ {this.ClientName} }};");
//Export the Client and all the Types
writer.WriteBreak().WriteLine("export {").Indent();
writer.WriteLine($"{this.ClientName},");
foreach (var type in includedTypes)
{
var newName = type.GetCustomAttribute<RouteTypeName>();
writer.WriteLine($"{(type.DeclaringType != null ? type.DeclaringType.Name : "")}{(newName != null ? newName.Name : type.Name)},");
}
writer.Outdent().WriteLine("};");
return writer.ToString();
}
@@ -170,7 +186,7 @@ namespace MontoyaTech.Rest.Net
{
return "string";
}
else if (typeCode == TypeCode.Object)
else if (typeCode == TypeCode.Object || type.IsEnum)
{
if (type.DeclaringType != null && !IsTypeDotNet(type.DeclaringType))
return $"{this.GetTypeFullyResolvedName(type.DeclaringType)}{base.GetTypeFullyResolvedName(type)}";
@@ -221,6 +237,8 @@ namespace MontoyaTech.Rest.Net
if (!type.IsEnum && !(IsTypeDotNet(type.BaseType) && type.BaseType.Name == "Object"))
writer.Write(" extends ").Write(this.GetTypeFullyResolvedName(type.BaseType));
else if (type.IsEnum)
writer.Write(" extends Number");
writer.WriteLine(" {").Indent();
@@ -240,33 +258,36 @@ namespace MontoyaTech.Rest.Net
this.GenerateJavascriptIncludedProperty(property, writer);
//Generate a helper constructor
writer.WriteBreak().WriteLine("/**").Indent();
writer.WriteLine("@method");
if (!type.IsEnum)
{
writer.WriteBreak().WriteLine("/**").Indent();
writer.WriteLine("@method");
foreach (var field in fields)
writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(field.FieldType)}}} {field.Name}");
foreach (var field in fields)
writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(field.FieldType)}}} {field.Name}");
foreach (var property in properties)
writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(property.PropertyType)}}} {property.Name}");
foreach (var property in properties)
writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(property.PropertyType)}}} {property.Name}");
writer.Outdent().WriteLine("*/");
writer.Write("constructor(");
writer.Outdent().WriteLine("*/");
writer.Write("constructor(");
foreach (var field in fields)
writer.WriteSeparator().Write(field.Name).Write(" = ").Write(this.GetTypeDefaultValue(field.FieldType));
foreach (var field in fields)
writer.WriteSeparator().Write(field.Name).Write(" = ").Write(this.GetTypeDefaultValue(field.FieldType));
foreach (var property in properties)
writer.WriteSeparator().Write(property.Name).Write(" = ").Write(this.GetTypeDefaultValue(property.PropertyType));
foreach (var property in properties)
writer.WriteSeparator().Write(property.Name).Write(" = ").Write(this.GetTypeDefaultValue(property.PropertyType));
writer.WriteLine(") {").Indent();
writer.WriteLine(") {").Indent();
foreach (var field in fields)
writer.Write("this.").Write(field.Name).Write(" = ").Write(field.Name).WriteLine(";");
foreach (var field in fields)
writer.Write("this.").Write(field.Name).Write(" = ").Write(field.Name).WriteLine(";");
foreach (var property in properties)
writer.Write("this.").Write(property.Name).Write(" = ").Write(property.Name).WriteLine(";");
foreach (var property in properties)
writer.Write("this.").Write(property.Name).Write(" = ").Write(property.Name).WriteLine(";");
writer.Outdent().WriteLine("}");
writer.Outdent().WriteLine("}");
}
writer.Outdent().WriteLine("}");
@@ -284,7 +305,7 @@ namespace MontoyaTech.Rest.Net
if (field.DeclaringType != null && field.DeclaringType.IsEnum)
{
writer.WriteLine("/** @type {number} */");
writer.WriteLine($"/** @type {{{GetTypeFullyResolvedName(field.DeclaringType)}}} */");
writer.WriteLine($"static {field.Name} = {field.GetRawConstantValue()};");
}
else