Пример #1
0
        protected virtual void WriteController(ApiControllerDesc controller, CustomIndentedTextWriter writer)
        {
            var actions = controller.Actions.Select(a => ParseAction(controller, a)).ToArray();

            var duplicateRoute = actions.GroupBy(a => a.Action.Method + a.Template).FirstOrDefault(g => g.Count() > 1);

            if (duplicateRoute != null)
            {
                throw new Exception($"Two actions ('{duplicateRoute.First().Action.ActionName}', '{duplicateRoute.Skip(1).First().Action.ActionName}') " +
                                    $"in controller '{controller.ControllerName}' share the same route '{duplicateRoute.First().Template}' and method '{duplicateRoute.First().Action.Method}'.");
            }

            var baseClass = String.IsNullOrWhiteSpace(_options.ControllerBaseClass) ? "" : $" extends {_options.ControllerBaseClass}";

            var controllerName = $"{controller.ControllerName}Service";

            writer.WriteLine($"export class {controllerName}{baseClass} {{");
            writer.Indent++;
            writer.WriteLine("protected _basePath: string;");
            writer.WriteLine($"protected {ajaxVariableName}: {_options.AjaxClassName};");

            writer.WriteLine();
            writer.WriteLine($"public endpoints = {{");
            foreach (var a in actions)
            {
                writer.Indent++;
                WriteEndpoint(a, writer);
                writer.Indent--;
            }
            writer.WriteLine($"}}");
            writer.WriteLine();

            writer.WriteLine($"public constructor(basePath?: string, {optionsVariableName}?: {_options.OptionsClassName}) {{");
            writer.Indent++;

            if (!String.IsNullOrEmpty(baseClass))
            {
                writer.WriteLine("super();");
            }

            writer.WriteLine("basePath = (basePath || \"\");");
            writer.WriteLine("this._basePath = (basePath.substr(-1) == \"/\") ? basePath.substr(0, basePath.length - 1) : basePath;");
            writer.WriteLine($"this.{ajaxVariableName} = new {_options.AjaxClassName}({optionsVariableName});");
            foreach (var a in actions)
            {
                writer.WriteLine($"this.{a.NameString} = this.{a.NameString}.bind(this);");
            }
            writer.Indent--;
            writer.WriteLine("}");

            foreach (var a in actions)
            {
                writer.WriteLine();
                WriteMethod(a, writer);
            }

            writer.Indent--;
            writer.WriteLine("}");
        }
Пример #2
0
        public ApiControllerDesc AddApiDescription(string name, string route = null)
        {
            var api = new ApiControllerDesc();

            api.ControllerName = name;

            if (!String.IsNullOrWhiteSpace(route))
            {
                api.RouteTemplate = route;
            }

            _apis.Add(api);
            return(api);
        }
Пример #3
0
        protected virtual ParsedApiDesc ParseAction(ApiControllerDesc controller, ApiActionDesc action)
        {
            var    actionRegex     = @"(?:\[action\]|\{action\})";
            var    controllerRegex = @"(?:\[controller\]|\{controller\})";
            string template        = _options.DefaultRouteTemplate;

            if (controller.RouteTemplate != null || action.RouteTemplate != null)
            {
                if (controller.RouteTemplate != null)
                {
                    template = controller.RouteTemplate;
                }
                if (action.RouteTemplate != null)
                {
                    var art      = action.RouteTemplate.TrimStart('~');
                    var absolute = art.StartsWith("/");
                    template = absolute ? art : JoinUrls(template, art);
                }
                template = template.Trim('/');
            }

            template = Regex.Replace(template, actionRegex, action.ActionName, RegexOptions.IgnoreCase);
            template = Regex.Replace(template, controllerRegex, controller.ControllerName, RegexOptions.IgnoreCase);

            ApiParamDesc postParameter  = null;
            ApiParamDesc modelParameter = null;

            ApiParamDesc[] getParameters = ValidateParameters(action.Parameters, action.Method, template, out postParameter, out modelParameter);

            string GetParamString(IEnumerable <ApiParamDesc> aa) => String.Join(", ", aa.Select(p => $"{p.ParameterName}{(p.IsOptional ? "?" : "")}: {_converter.GetTypeScriptName(p.ParameterType)}"));

            var path = BuildUrlString(action, template, getParameters, modelParameter);

            return(new ParsedApiDesc
            {
                EndpointParamString = GetParamString(modelParameter != null ? new[] { modelParameter } : getParameters),
                ParamString = GetParamString(action.Parameters),
                PathString = path,
                PostParameter = postParameter,
                ModelParameter = modelParameter,
                GetParameters = getParameters,
                NameString = _options.FnActionName(action),
                Action = action,
                Template = template,
            });
        }