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:
		@@ -11,61 +11,61 @@ using System.Collections;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace MontoyaTech.Rest.Net.Example
 | 
					namespace MontoyaTech.Rest.Net.Example
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    public class BaseUser
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public string Id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public char FirstInitial;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public UserRole Role { get; set; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public List<Permission> 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<string> 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 Program
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        public class BaseUser
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            public string Id;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            public char FirstInitial;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            public UserRole Role { get; set; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            public List<Permission> 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<string> 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 RouteFileCache FileCache = new RouteFileCache(100 * 1024 * 1024);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public static void Main(string[] args)
 | 
					        public static void Main(string[] args)
 | 
				
			||||||
@@ -87,16 +87,15 @@ namespace MontoyaTech.Rest.Net.Example
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            string code = listener.GenerateCSharpClient();
 | 
					            string code = listener.GenerateCSharpClient();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            File.WriteAllText("Client.cs", code);
 | 
					            File.WriteAllText("Client.cs", listener.GenerateCSharpClient());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Console.WriteLine(code);
 | 
					            File.WriteAllText("Client.js", listener.GenerateJavascriptClient());
 | 
				
			||||||
            Console.WriteLine();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            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) => {
 | 
					            listener.RequestPreProcessEvent += (HttpListenerContext context) => {
 | 
				
			||||||
                Console.WriteLine($"[{context.Request.HttpMethod}] Request start: " + context.Request.RawUrl);
 | 
					                Console.WriteLine($"[{context.Request.HttpMethod}] Request start: " + context.Request.RawUrl);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,7 +17,7 @@
 | 
				
			|||||||
    <AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
 | 
					    <AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
 | 
				
			||||||
    <RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
 | 
					    <RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
 | 
				
			||||||
    <GenerateDocumentationFile>True</GenerateDocumentationFile>
 | 
					    <GenerateDocumentationFile>True</GenerateDocumentationFile>
 | 
				
			||||||
    <Version>1.4.9</Version>
 | 
					    <Version>1.5.0</Version>
 | 
				
			||||||
    <PackageReleaseNotes></PackageReleaseNotes>
 | 
					    <PackageReleaseNotes></PackageReleaseNotes>
 | 
				
			||||||
    <PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
 | 
					    <PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
 | 
				
			||||||
  </PropertyGroup>
 | 
					  </PropertyGroup>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -62,8 +62,12 @@ namespace MontoyaTech.Rest.Net
 | 
				
			|||||||
            //Create the client constructor or init method
 | 
					            //Create the client constructor or init method
 | 
				
			||||||
            if (this.StaticCode)
 | 
					            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
 | 
					                //Make sure the baseUrl isn't null or whitespace
 | 
				
			||||||
                writer.WriteBreak().WriteLine("if (baseUrl == null || baseUrl == undefined || baseUrl.trim() == '') {").Indent();
 | 
					                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($"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();
 | 
					            return writer.ToString();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -170,7 +186,7 @@ namespace MontoyaTech.Rest.Net
 | 
				
			|||||||
            {
 | 
					            {
 | 
				
			||||||
                return "string";
 | 
					                return "string";
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else if (typeCode == TypeCode.Object)
 | 
					            else if (typeCode == TypeCode.Object || type.IsEnum)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                if (type.DeclaringType != null && !IsTypeDotNet(type.DeclaringType))
 | 
					                if (type.DeclaringType != null && !IsTypeDotNet(type.DeclaringType))
 | 
				
			||||||
                    return $"{this.GetTypeFullyResolvedName(type.DeclaringType)}{base.GetTypeFullyResolvedName(type)}";
 | 
					                    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"))
 | 
					            if (!type.IsEnum && !(IsTypeDotNet(type.BaseType) && type.BaseType.Name == "Object"))
 | 
				
			||||||
                writer.Write(" extends ").Write(this.GetTypeFullyResolvedName(type.BaseType));
 | 
					                writer.Write(" extends ").Write(this.GetTypeFullyResolvedName(type.BaseType));
 | 
				
			||||||
 | 
					            else if (type.IsEnum)
 | 
				
			||||||
 | 
					                writer.Write(" extends Number");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            writer.WriteLine(" {").Indent();
 | 
					            writer.WriteLine(" {").Indent();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -240,33 +258,36 @@ namespace MontoyaTech.Rest.Net
 | 
				
			|||||||
                    this.GenerateJavascriptIncludedProperty(property, writer);
 | 
					                    this.GenerateJavascriptIncludedProperty(property, writer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            //Generate a helper constructor
 | 
					            //Generate a helper constructor
 | 
				
			||||||
            writer.WriteBreak().WriteLine("/**").Indent();
 | 
					            if (!type.IsEnum)
 | 
				
			||||||
            writer.WriteLine("@method");
 | 
					            {
 | 
				
			||||||
 | 
					                writer.WriteBreak().WriteLine("/**").Indent();
 | 
				
			||||||
 | 
					                writer.WriteLine("@method");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            foreach (var field in fields)
 | 
					                foreach (var field in fields)
 | 
				
			||||||
                writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(field.FieldType)}}} {field.Name}");
 | 
					                    writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(field.FieldType)}}} {field.Name}");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            foreach (var property in properties)
 | 
					                foreach (var property in properties)
 | 
				
			||||||
                writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(property.PropertyType)}}} {property.Name}");
 | 
					                    writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(property.PropertyType)}}} {property.Name}");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            writer.Outdent().WriteLine("*/");
 | 
					                writer.Outdent().WriteLine("*/");
 | 
				
			||||||
            writer.Write("constructor(");
 | 
					                writer.Write("constructor(");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            foreach (var field in fields)
 | 
					                foreach (var field in fields)
 | 
				
			||||||
                writer.WriteSeparator().Write(field.Name).Write(" = ").Write(this.GetTypeDefaultValue(field.FieldType));
 | 
					                    writer.WriteSeparator().Write(field.Name).Write(" = ").Write(this.GetTypeDefaultValue(field.FieldType));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            foreach (var property in properties)
 | 
					                foreach (var property in properties)
 | 
				
			||||||
                writer.WriteSeparator().Write(property.Name).Write(" = ").Write(this.GetTypeDefaultValue(property.PropertyType));
 | 
					                    writer.WriteSeparator().Write(property.Name).Write(" = ").Write(this.GetTypeDefaultValue(property.PropertyType));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            writer.WriteLine(") {").Indent();
 | 
					                writer.WriteLine(") {").Indent();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            foreach (var field in fields)
 | 
					                foreach (var field in fields)
 | 
				
			||||||
                writer.Write("this.").Write(field.Name).Write(" = ").Write(field.Name).WriteLine(";");
 | 
					                    writer.Write("this.").Write(field.Name).Write(" = ").Write(field.Name).WriteLine(";");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            foreach (var property in properties)
 | 
					                foreach (var property in properties)
 | 
				
			||||||
                writer.Write("this.").Write(property.Name).Write(" = ").Write(property.Name).WriteLine(";");
 | 
					                    writer.Write("this.").Write(property.Name).Write(" = ").Write(property.Name).WriteLine(";");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            writer.Outdent().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)
 | 
					            if (field.DeclaringType != null && field.DeclaringType.IsEnum)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                writer.WriteLine("/** @type {number} */");
 | 
					                writer.WriteLine($"/** @type {{{GetTypeFullyResolvedName(field.DeclaringType)}}} */");
 | 
				
			||||||
                writer.WriteLine($"static {field.Name} = {field.GetRawConstantValue()};");
 | 
					                writer.WriteLine($"static {field.Name} = {field.GetRawConstantValue()};");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else
 | 
					            else
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user