예제 #1
0
        public string Generate(DynamicApiControllerInfo controllerInfo, string servicePrefix)
        {
            if (_servicePrefix != servicePrefix)
            {
                //if there is a change in servicePrefix, we need to generate the types again
                _servicePrefix = servicePrefix;
                _typesToBeDone = new HashSet <Type>();
                _doneTypes     = new HashSet <Type>();
            }
            _controllerInfo = controllerInfo;

            var script = new StringBuilder();

            script.AppendLine("     interface " + _controllerInfo.ServiceName.Substring(_controllerInfo.ServiceName.IndexOf('/') + 1));
            script.AppendLine("     {");

            foreach (var methodInfo in _controllerInfo.Actions.Values)
            {
                PrepareInputParameterTypes(methodInfo.Method);

                List <Type> newTypes   = new List <Type>();
                var         returnType = TypeScriptHelper.GetTypeContractName(methodInfo.Method.ReturnType, newTypes);
                this.AddNewTypesIfRequired(newTypes.ToArray());
                if (returnType == "void")
                {
                    script.AppendLine(string.Format("            {0} ({1}): abp.IPromise; ", methodInfo.ActionName.ToCamelCase(), GetMethodInputParameter(methodInfo.Method)));
                }
                else
                {
                    script.AppendLine(string.Format("            {0} ({1}): abp.IGenericPromise<{2}>; ", methodInfo.ActionName.ToCamelCase(), GetMethodInputParameter(methodInfo.Method), returnType));
                }
            }

            script.AppendLine("     }");
            while (_typesToBeDone != null && _typesToBeDone.Count > 0)
            {
                Type type = _typesToBeDone.First();

                script.AppendLine(GenerateTypeScript(type));
                _doneTypes.Add(type);
                _typesToBeDone.RemoveWhere(x => x == type);
            }
            return(script.ToString());
        }
예제 #2
0
        protected string GenerateTypeScript(Type type)
        {
            if (type.IsArray ||
                (type.IsGenericType && (typeof(List <>).IsAssignableFrom(type.GetGenericTypeDefinition()) ||
                                        typeof(ICollection <>).IsAssignableFrom(type.GetGenericTypeDefinition()) ||
                                        typeof(IEnumerable <>).IsAssignableFrom(type.GetGenericTypeDefinition())
                                        ))
                )
            {
                if (type.GetElementType() != null)
                {
                    this.AddNewTypesIfRequired(type.GetElementType());
                }
                return("");
            }

            if (type.IsGenericType && typeof(Nullable <>).IsAssignableFrom(type.GetGenericTypeDefinition()))
            {
                return("");
            }



            var         myscript = new StringBuilder();
            List <Type> newTypes = new List <Type>();

            myscript.AppendLine("     export class " + TypeScriptHelper.GetTypeContractName(type, newTypes));
            myscript.AppendLine("         {");
            foreach (var property in type.GetProperties())
            {
                myscript.AppendLine(string.Format("{0} : {1} ;", property.Name.ToCamelCase(), TypeScriptHelper.GetTypeContractName(property.PropertyType, newTypes)));
            }
            this.AddNewTypesIfRequired(newTypes.ToArray());
            myscript.AppendLine("         }");
            return(myscript.ToString());
        }
예제 #3
0
        public string Generate(DynamicApiControllerInfo controllerInfo, string servicePrefix)
        {
            if (_servicePrefix != servicePrefix)
            {
                //if there is a change in servicePrefix, we need to generate the types again
                _servicePrefix = servicePrefix;
                _typesToBeDone = new HashSet <Type>();
                _doneTypes     = new HashSet <Type>();
            }
            _controllerInfo = controllerInfo;

            var script = new StringBuilder();

            script.AppendLine("     export class " + _controllerInfo.ServiceName.Substring(_controllerInfo.ServiceName.IndexOf('/') + 1));
            script.AppendLine("     {");
            script.AppendLine("         static $inject = ['$http'];");
            script.AppendLine("         constructor(private $http: ng.IHttpService){");
            script.AppendLine("     }");
            foreach (var methodInfo in _controllerInfo.Actions.Values)
            {
                PrepareInputParameterTypes(methodInfo.Method);
                List <Type> newTypes   = new List <Type>();
                var         returnType = TypeScriptHelper.GetTypeContractName(methodInfo.Method.ReturnType, newTypes);
                this.AddNewTypesIfRequired(newTypes.ToArray());
                if (returnType == "void")
                {
                    script.AppendLine(string.Format("           public {0} = function ({1}): abp.IPromise ",
                                                    methodInfo.ActionName.ToCamelCase(), GetMethodInputParameter(methodInfo.Method)));
                    script.AppendLine("{");
                    script.AppendLine("                    var self = this;");
                    script.AppendLine("                    return self.$http(angular.extend({");
                    script.AppendLine("                        abp: true,");
                    script.AppendLine("                        url: abp.appPath + '" + ActionScriptingHelper.GenerateUrlWithParameters(_controllerInfo, methodInfo) + "',");
                    script.AppendLine("                        method: '" + methodInfo.Verb.ToString().ToUpper(CultureInfo.InvariantCulture) + "',");

                    if (methodInfo.Verb == HttpVerb.Get)
                    {
                        script.AppendLine("                        params: " + ActionScriptingHelper.GenerateBody(methodInfo));
                    }
                    else
                    {
                        script.AppendLine("                        data: JSON.stringify(" + ActionScriptingHelper.GenerateBody(methodInfo) + ")");
                    }

                    script.AppendLine("                    }, httpParams));");


                    script.AppendLine("}");
                }

                else
                {
                    script.AppendLine(string.Format("           public {0} = function ({1}): abp.IGenericPromise<{2}> ", methodInfo.ActionName.ToCamelCase(),
                                                    GetMethodInputParameter(methodInfo.Method), returnType));
                    script.AppendLine("{");
                    script.AppendLine("                    var self = this;");
                    script.AppendLine("                    return self.$http(angular.extend({");
                    script.AppendLine("                        abp: true,");
                    script.AppendLine("                        url: abp.appPath + '" + ActionScriptingHelper.GenerateUrlWithParameters(_controllerInfo, methodInfo) + "',");
                    script.AppendLine("                        method: '" + methodInfo.Verb.ToString().ToUpper(CultureInfo.InvariantCulture) + "',");

                    if (methodInfo.Verb == HttpVerb.Get)
                    {
                        script.AppendLine("                        params: " + ActionScriptingHelper.GenerateBody(methodInfo));
                    }
                    else
                    {
                        script.AppendLine("                        data: JSON.stringify(" + ActionScriptingHelper.GenerateBody(methodInfo) + ")");
                    }

                    script.AppendLine("                    }, httpParams));");

                    script.AppendLine("}");
                }
            }

            script.AppendLine("     }");

            script.AppendLine("angular.module('abp').service('abp.services." + _controllerInfo.ServiceName.Replace("/", ".") + "', abp.services." + _controllerInfo.ServiceName.Replace("/", ".") + ");");


            while (_typesToBeDone != null && _typesToBeDone.Count > 0)
            {
                Type type = _typesToBeDone.First();

                script.AppendLine(GenerateTypeScript(type));
                _doneTypes.Add(type);
                _typesToBeDone.RemoveWhere(x => x == type);
            }
            return(script.ToString());
        }
예제 #4
0
        protected string GetMethodInputParameter(MethodInfo methodInfo)
        {
            var script = new StringBuilder();

            List <Type> newTypes = new List <Type>();

            foreach (var parameter in methodInfo.GetParameters())
            {
                script.Append(string.Format("{0} : {1},", parameter.Name.ToCamelCase(), TypeScriptHelper.GetTypeContractName(parameter.ParameterType, newTypes)));
            }
            script.Append("httpParams?: any");
            this.AddNewTypesIfRequired(newTypes.ToArray());
            return(script.ToString());
        }