Beispiel #1
0
        private static void GenerateEndpoint(StringBuilder sb, EndpointInfo endpoint, TsOptions tsOptions)
        {
            sb.Append($"\t\t{endpoint.NameCamelCase}: function(");
            // generate function vars
            var separator = "";

            foreach (var parameter in endpoint.Parameters)
            {
                sb.Append(separator);
                sb.Append(parameter.JsName);
                if (tsOptions != null)
                {
                    sb.Append(": ").Append(TsGenerator.GenerateTypescriptType(parameter.Info.ParameterType, tsOptions));
                }
                separator = ", ";
            }
            // continue function
            sb.Append($"){{ return http(\"{endpoint.HttpMethod}\", ");
            // resolve route params
            var httpRoute = "\"" + endpoint.HttpRoute;

            foreach (var parameter in endpoint.Parameters)
            {
                if (parameter.IsRoute)
                {
                    var templateName = "{" + parameter.HttpName + "}";
                    httpRoute = httpRoute.Replace(templateName, "\" + encodeURIComponent(" + parameter.JsName + ") + \"");
                }
            }
            // resolve query params
            separator = "?";
            foreach (var parameter in endpoint.Parameters)
            {
                if (parameter.IsQuery)
                {
                    httpRoute += separator + parameter.HttpName + "=\" + encodeURIComponent(" + parameter.JsName + ") + \"";
                    separator  = "&";
                }
            }
            // write route
            sb.Append(httpRoute + "\", ");
            // find body param
            ParameterInfo bodyParam = null;

            foreach (var parameter in endpoint.Parameters)
            {
                if (parameter.IsBody)
                {
                    if (bodyParam == null)
                    {
                        bodyParam = parameter;
                    }
                    else
                    {
                        throw new CrispyException($"It can only have 1 body param but there are at least 2.");
                    }
                }
            }
            if (bodyParam == null)
            {
                sb.Append("null");
            }
            else
            {
                sb.Append(bodyParam.JsName);
            }

            sb.Append(", ");
            // return type formatter

            if (endpoint.ReturnType == typeof(String))
            {
                sb.Append("String");
            }
            else if (endpoint.ReturnType == typeof(Boolean))
            {
                sb.Append("Boolean");
            }
            else if (endpoint.ReturnType == typeof(DateTime) || endpoint.ReturnType == typeof(DateTimeOffset))
            {
                sb.Append("DateFormatter");
            }
            else if (TypeHelpers.IsNumericType(endpoint.ReturnType))
            {
                sb.Append("Number");
            }
            else if (endpoint.ReturnType == typeof(void))
            {
                sb.Append("undefined");
            }
            else
            {
                sb.Append("JSON.parse");
            }

            // done method
            sb.Append(")");
            if (tsOptions != null)
            {
                sb.Append(" as Promise<").Append(TsGenerator.GenerateTypescriptType(endpoint.ReturnType, tsOptions)).Append(">");
            }
            sb.Append("; },\n");
        }
Beispiel #2
0
        private static String TypeGenImpl(Type type, TsOptions options, Stack <Type> typeStack)
        {
            if (options == null)
            {
                options = DEFAULT_OPTIONS;
            }
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task <>))
            {
                var redirect = type.GetProperty("Result").PropertyType;
                return(GuardedTypeGen(redirect, options, typeStack));
            }
            if (type == typeof(Task))
            {
                return("void");
            }
            if (type == typeof(String) ||
                type == typeof(char) ||
                type == typeof(DateTime) ||
                type == typeof(DateTimeOffset) ||
                type == typeof(TimeSpan))
            {
                return("string");
            }
            if (type == typeof(bool))
            {
                return("boolean");
            }
            if (TypeHelpers.IsNumericType(type))
            {
                return("number");
            }
            if (type == typeof(void))
            {
                return("void");
            }
            if (type.IsArray)
            {
                return(GuardedTypeGen(type.GetElementType(), options, typeStack) + "[]");
            }
            if (type.IsEnum)
            {
                if (options.EnumNumberValues)
                {
                    return(String.Join(" | ", type.GetEnumValues() as IEnumerable <int>));
                }
                else
                {
                    return(String.Join(" | ", type.GetEnumNames().Select(name => "'" + name + "'")));
                }
            }
            var iDictionary = type.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDictionary <,>));

            if (iDictionary != null)
            {
                var args = iDictionary.GetGenericArguments();
                if (args.Length == 2)
                {
                    return("{ [key:string]: " + GuardedTypeGen(args[1], options, typeStack) + " }");
                }
                return("{ [key:string]: string }");
            }
            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                var generics = type.GetGenericArguments();
                if (generics.Length == 1)
                {
                    return(GuardedTypeGen(generics[0], options, typeStack) + "[]");
                }
                return("any[]");
            }
            var nullableUndelyingType = Nullable.GetUnderlyingType(type);

            if (nullableUndelyingType != null)
            {
                // is nullable<T>
                return(GuardedTypeGen(nullableUndelyingType, options, typeStack) + " | null");
            }
            var bindingFlags = BindingFlags.Instance;

            if (options.Public)
            {
                bindingFlags |= BindingFlags.Public;
            }
            if (options.NonPublic)
            {
                bindingFlags |= BindingFlags.NonPublic;
            }
            if (options.Readable)
            {
                if (options.Fields)
                {
                    bindingFlags |= BindingFlags.GetField;
                }
                if (options.Properties)
                {
                    bindingFlags |= BindingFlags.GetProperty;
                }
            }
            if (options.Writeable)
            {
                if (options.Fields)
                {
                    bindingFlags |= BindingFlags.SetField;
                }
                if (options.Properties)
                {
                    bindingFlags |= BindingFlags.SetProperty;
                }
            }
            var members = type.GetMembers(bindingFlags);

            if (members.Length > 0)
            {
                var strb      = new StringBuilder();
                var separator = "{ ";
                foreach (var member in members)
                {
                    if (member.MemberType == MemberTypes.Property && options.Properties)
                    {
                        var property = type.GetProperty(member.Name);
                        if (!(options.Readable && property.CanRead && (options.NonPublic || property.GetGetMethod(nonPublic: true).IsPublic) ||
                              options.Writeable && property.CanWrite && (options.NonPublic || property.GetSetMethod(nonPublic: true).IsPublic)))
                        {
                            continue;
                        }
                        var name       = options.LowercaseFirstLetter ? property.Name.LowerFirstLetter() : property.Name;
                        var typeString = GuardedTypeGen(property.PropertyType, options, typeStack);
                        AppendMember(strb, name, typeString, separator);
                        separator = ", ";
                    }
                    else if (member.MemberType == MemberTypes.Field && options.Fields)
                    {
                        var field = type.GetField(member.Name, bindingFlags);
                        if (field.Name.Contains("<") || field.Name.Contains(">"))
                        {
                            continue; // skip auto generated backing fields
                        }
                        if (options.Readable || !field.IsInitOnly && options.Writeable)
                        {
                            var name       = options.LowercaseFirstLetter ? field.Name.LowerFirstLetter() : field.Name;
                            var typeString = GuardedTypeGen(field.FieldType, options, typeStack);
                            AppendMember(strb, name, typeString, separator);
                            separator = ", ";
                        }
                    }
                }
                if (strb.Length == 0)
                {
                    return("any"); // had no valid members
                }
                strb.Append(" }");
                return(strb.ToString());
            }
            return("any");
        }