public void GenerateWebApiActions() { var output = new StringBuilder("module Api {"); //TODO: allow this is be configured output.Append(_interfaces); foreach (var assemblyName in _options.Assemblies) { var assembly = Assembly.LoadFrom(assemblyName); var controllers = assembly.GetTypes().Where(_configuration.ControllerPredicate).OrderBy(t => t.Name); foreach (var controller in controllers) { var actions = controller.GetMethods() .Where(_configuration.ActionsPredicate) .Where(m => m.DeclaringType == controller) .OrderBy(m => m.Name); if (!actions.Any()) { continue; } var controllerName = controller.Name.Replace("Controller", ""); output.AppendFormat("\r\n export class {0} {{\r\n", controllerName); // TODO: WebAPI supports multiple actions with the same name but different parameters - this doesn't! foreach (var action in actions) { if (NotAnAction(action)) { continue; } var httpMethod = GetHttpMethod(action); var returnType = TypeScriptDefinitionsGenerator.Common.TypeConverter.GetTypeScriptName(action.ReturnType); var actionParameters = _configuration.GetActionParameters(action); var dataParameter = actionParameters.FirstOrDefault(a => !a.FromUri && !a.RouteProperty); var dataParameterName = dataParameter == null ? "null" : dataParameter.Name; var url = _configuration.UrlGenerator.GetUrl(action); // allow ajax options to be passed in to override defaults output.AppendFormat(" public static {0}({1}): JQueryPromise<{2}> {{\r\n", action.Name.ToCamelCase(), GetMethodParameters(actionParameters, "IExtendedAjaxSettings"), returnType); output.AppendFormat(" return ServiceCaller.{0}({1}, {2}, ajaxOptions);\r\n", httpMethod, url, dataParameterName); output.AppendLine(" }"); output.AppendLine(); } output.AppendLine(" }"); } } output.Append("}"); File.WriteAllText(Path.Combine(_options.OutputFilePath, "actions.ts"), output.ToString()); if (!_options.SuppressDefaultServiceCaller) { // Write the default service caller using (var stream = typeof(MainGenerator).Assembly.GetManifestResourceStream(typeof(MainGenerator).Namespace + ".Resources.ServiceCaller.ts")) using (var reader = new StreamReader(stream)) { File.WriteAllText(Path.Combine(_options.OutputFilePath, "servicecaller.ts"), reader.ReadToEnd()); } } }
public void GenerateWebApiActions() { if (_options.GenerateServiceStackRequests) { throw new Exception("WebApi actions are not supported for ServiceStack APIs at the moment! Please submit a Pull Request!"); } var model = new WebApiModel(); foreach (var assemblyName in _options.Assemblies) { var assembly = Assembly.LoadFrom(assemblyName); var controllers = assembly.GetTypes().Where(_configuration.ControllerPredicate).OrderBy(t => t.Name); foreach (var controller in controllers) { var actions = controller.GetMethods() .Where(_configuration.ActionsPredicate) .Where(m => m.DeclaringType == controller) .OrderBy(m => m.Name); if (!actions.Any()) { continue; } var controllerName = controller.Name.Replace("Controller", ""); var controllerModel = new Controller(); controllerModel.ServerType = controller; controllerModel.TypeScriptName = controllerName; model.Controllers.Add(controllerModel); // TODO: WebAPI supports multiple actions with the same name but different parameters - this doesn't! foreach (var action in actions) { if (NotAnAction(action)) { continue; } var httpMethod = GetHttpMethod(action); var returnType = TypeScriptDefinitionsGenerator.Common.TypeConverter.GetTypeScriptName(action.ReturnType); var actionParameters = _configuration.GetActionParameters(action); var dataParameter = actionParameters.FirstOrDefault(a => !a.FromUri && !a.RouteProperty); var dataParameterName = dataParameter == null ? "null" : dataParameter.Name; var url = _configuration.UrlGenerator.GetUrl(action); var actionModel = new Action(); actionModel.Verbs = new[] { httpMethod }; actionModel.Url = url; actionModel.TypeScriptName = action.Name.ToCamelCase(); actionModel.ActionParameters = actionParameters; actionModel.DataParameterName = dataParameterName; actionModel.ReturnType = returnType; controllerModel.Actions.Add(actionModel); } } } SetupHelpers(); SetupTemplates("WebApi_jQuery"); var result = Handlebars.Compile("{{> main.hbs }}")(model); File.WriteAllText(Path.Combine(_options.OutputFilePath, _options.ActionsOutputFileName ?? "actions.ts"), result); if (!_options.SuppressDefaultServiceCaller) { // Write the default service caller using (var stream = typeof(MainGenerator).Assembly.GetManifestResourceStream(typeof(MainGenerator).Namespace + ".Resources.ServiceCaller.ts")) using (var reader = new StreamReader(stream)) { File.WriteAllText(Path.Combine(_options.OutputFilePath, "servicecaller.ts"), reader.ReadToEnd()); } } }