diff --git a/Rest.Net.Example/Program.cs b/Rest.Net.Example/Program.cs index ae53d73..3a15f7f 100644 --- a/Rest.Net.Example/Program.cs +++ b/Rest.Net.Example/Program.cs @@ -11,61 +11,61 @@ using System.Collections; namespace MontoyaTech.Rest.Net.Example { + public class BaseUser + { + public string Id; + + public char FirstInitial; + + public UserRole Role { get; set; } + + public List Permissions; + + public class Permission + { + public string Name; + + public Types Type; + + public enum Types { Read, Write } + } + } + + [RouteTypeName("UserDto")] + public class User : BaseUser + { + public PlatformID MachineType; + + public string Name = null; + + public List List = null; + + public string[] Array = null; + + public ulong Property { get; set; } + + public User() { } + + public User(string name) + { + this.Name = name; + } + } + + public enum UserRole : byte + { + Unknown = 0, + Admin = 2, + User = 1 + } + + public class IncludedType + { + public int Test; + } + public class Program { - public class BaseUser - { - public string Id; - - public char FirstInitial; - - public UserRole Role { get; set; } - - public List Permissions; - - public class Permission - { - public string Name; - - public Types Type; - - public enum Types { Read, Write } - } - } - - [RouteTypeName("UserDto")] - public class User : BaseUser - { - public PlatformID MachineType; - - public string Name = null; - - public List List = null; - - public string[] Array = null; - - public ulong Property { get; set; } - - public User() { } - - public User(string name) - { - this.Name = name; - } - } - - public enum UserRole : byte - { - Unknown = 0, - Admin = 2, - User = 1 - } - - public class IncludedType - { - public int Test; - } - public static RouteFileCache FileCache = new RouteFileCache(100 * 1024 * 1024); public static void Main(string[] args) @@ -87,16 +87,15 @@ namespace MontoyaTech.Rest.Net.Example string code = listener.GenerateCSharpClient(); - File.WriteAllText("Client.cs", code); + File.WriteAllText("Client.cs", listener.GenerateCSharpClient()); - Console.WriteLine(code); - Console.WriteLine(); + File.WriteAllText("Client.js", listener.GenerateJavascriptClient()); - string staticCode = listener.GenerateCSharpClient("StaticClient", staticCode: true); + File.WriteAllText("StaticClient.cs", listener.GenerateCSharpClient("StaticClient", staticCode: true)); - File.WriteAllText("Client.Static.cs", staticCode); + File.WriteAllText("StaticClient.js", listener.GenerateJavascriptClient("StaticClient", staticCode: true)); - Console.WriteLine(staticCode); + Console.WriteLine("Generated Client.cs, Client.js, StaticClient.cs, StaticClient.js"); listener.RequestPreProcessEvent += (HttpListenerContext context) => { Console.WriteLine($"[{context.Request.HttpMethod}] Request start: " + context.Request.RawUrl); diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index 09d50cc..f190a4a 100644 --- a/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net.csproj @@ -17,7 +17,7 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.4.9 + 1.5.0 Logo_Symbol_Black_Outline.png diff --git a/Rest.Net/RestJavascriptClientGenerator.cs b/Rest.Net/RestJavascriptClientGenerator.cs index 7681a87..bb18784 100644 --- a/Rest.Net/RestJavascriptClientGenerator.cs +++ b/Rest.Net/RestJavascriptClientGenerator.cs @@ -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(); + + 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