private void WriteMethodSignature(TypeScriptWriter writer, Method method) { writer.StartLine("{0}(", TextUtils.CamelCase(method.Name)); for (var index = 0; index < method.ParamInfos.Length; index++) { if (index > 0) writer.Append(", "); var pi = method.ParamInfos[index]; writer.AppendFormat("{0}: {1}", TextUtils.CamelCase(pi.Name), GetTsType(pi.Type) ); writer.AddType(pi.Type); } writer.Append(")"); //@@TODO use union types if (method.ReturnType == typeof(HttpResponseMessage)) { writer.Append(" : any"); } else { var returnType = GetTsType(method.ReturnType); writer.AddType(method.ReturnType); writer.AppendFormat(" : ng.IHttpPromise<{0}>", returnType); } }
private void WriteInterface(TypeScriptWriter writer) { writer.IncIndent(); writer.StartLine("export interface I{0} {{\n", _type.Name.Replace("Controller", "Api")); using (writer.Indent()) { foreach (var method in _methods) { WriteMethodSignature(writer, method); writer.Append(";\n"); } } writer.StartLine("}\n"); }
private void WriteTo(TypeScriptWriter writer) { WriteInterface(writer); WriteImpl(writer); }
public string Generate() { var writer = new TypeScriptWriter(); WriteTo(writer); return writer.Write(_ns + "." + _cname.Replace("Api", "")); }
private void WriteImpl(TypeScriptWriter writer) { writer.StartLine("\n"); writer.StartLine("/* implementation */\n"); writer.StartLine("class {0} implements I{0}{{\n", _cname); using (writer.Indent()) { writer.StartLine("\n"); writer.StartLine("static endpoint =\"{0}\";\n", _endpoint); /* * $inject * */ writer.StartLine("static $inject = ["); var keys = _dependencies.Keys.ToArray(); for (var index = 0; index < keys.Length; index++) { var dependency = keys[index]; if (index > 0) writer.Append(", "); writer.AppendFormat("\"{0}\"", dependency); } writer.Append("];\n"); /* * constructor * */ writer.NewLine(); writer.StartLine("constructor(\n"); using (writer.Indent()) { for (var index = 0; index < keys.Length; index++) { var dependency = keys[index]; if (index > 0) writer.Append(",\n"); writer.StartLine("private {0}: {1}", dependency, _dependencies[dependency]); } writer.Append("\n"); } writer.StartLine(") { }\n"); /* * factory * @@TODO switch to service * */ writer.NewLine(); writer.StartLine("static factory() {\n"); using (writer.Indent()) { writer.StartLine("return ("); for (var index = 0; index < keys.Length; index++) { var dependency = keys[index]; if (index > 0) writer.Append(","); writer.AppendFormat("{0}: {1}", dependency, _dependencies[dependency]); } writer.AppendFormat(") => new {0}(", _cname); for (var index = 0; index < keys.Length; index++) { var dependency = keys[index]; if (index > 0) writer.Append(","); writer.Append(dependency); } writer.Append(");\n"); } writer.StartLine("}\n"); /* * methods * */ var controllerRoot = _type.Name.ToLowerInvariant().Replace("controller", ""); foreach (var method in _methods) { writer.NewLine(); var methodUri = method.GetUri(controllerRoot); WriteMethodSignature(writer, method); writer.Append("{\n"); var httpMethod = method.HttpMethod; using (writer.Indent()) { if (method.ParamInfos.Length == 0) { writer.StartLine( "return this.$http.{0}<{3}>({2}.endpoint + \"{1}\", {{}});\n", httpMethod, methodUri, _cname, GetTsType(method.ReturnType) ); } else { if (httpMethod == "get") { var qs = new StringBuilder(); for (int index = 0; index < method.ParamInfos.Length; index++) { var paramInfo = method.ParamInfos[index]; if (index > 0) qs.Append("&"); qs.AppendFormat("{0}=\" + encodeURIComponent({0}) +\"", TextUtils.CamelCase(paramInfo.Name)); } writer.StartLine( "return this.$http.{0}<{3}>({2}.endpoint + \"{1}?{4}\", {{}});\n", httpMethod, methodUri, _cname, GetTsType(method.ReturnType), qs.ToString() ); } if (httpMethod == "post") { if (method.ParamInfos.Length == 1) { writer.StartLine( "return this.$http.{0}<{4}>({3}.endpoint + \"{1}\", {2});\n", httpMethod, methodUri, method.ParamInfos.First().Name, _cname, GetTsType(method.ReturnType) ); } else { throw new NotImplementedException("Post with more than one parameter not (yet) supported\n" + JsonConvert.SerializeObject(method, Formatting.Indented)); } } } } writer.StartLine("}\n"); } } writer.StartLine("}\n"); writer.NewLine(); writer.StartLine("export const apiId = \"{0}\";\n", TextUtils.CamelCase(_cname)); writer.StartLine("angular.module(\"{0}\")\n", _moduleName); writer.StartLine(" .factory(apiId, {0}.factory());", _cname); writer.NewLine(); }
public IndentScope(TypeScriptWriter writer) { _writer = writer; _writer.IncIndent(); }