public void ServiceStackBlockHelperTestUsingResourceFiles()
        {
            var gen = new MainGenerator(new Options(), new GenerationConfiguration());

            MainGenerator.SetupHelpers();
            gen.SetupTemplates("ServiceStack_Angular");

            var model        = new ServiceStackApiModel();
            var requestModel = new ServiceStackRequestModel {
                Name = "MyRequest", ReturnTypeTypeScriptName = "string"
            };

            model.Requests.Add(requestModel);
            requestModel.Routes.Add(new ServiceStackRouteInfo("GET", "path", "url2", "", new List <string> {
                "test"
            }));
            requestModel.Routes.Add(new ServiceStackRouteInfo("GET", "path", "url", "", new List <string>()));

            var compiledTemplate = Handlebars.Compile("{{> main.hbs }}");
            var templateOutput   = compiledTemplate(model);

            _testOutputHelper.WriteLine(templateOutput);
        }
Example #2
0
        private void GenerateAngularActions()
        {
            if (!_options.GenerateServiceStackRequests)
            {
                throw new Exception("Angular is only supported for ServiceStack APIs at the moment!  Please submit a Pull Request!");
            }

            var requiredImports = new HashSet <string>();
            var allRequests     = new List <string>();

            var model = new ServiceStackApiModel();

            foreach (var assemblyName in _options.Assemblies)
            {
                var assembly = Assembly.LoadFrom(assemblyName);
                var requests = GetServiceStackRequestTypes(assembly);

                foreach (var request in requests)
                {
                    try
                    {
                        var requestModel = new ServiceStackRequestModel();
                        model.Requests.Add(requestModel);
                        var returnType  = _ssHelper.GetResponseTypeForRequest(request);
                        var returnsVoid = _ssHelper.ReturnsVoid(request);

                        var returnTypeTypeScriptName = returnsVoid ? "void" : (returnType != null ? TypeConverter.GetTypeScriptName(returnType) : "any");
                        var routes = request.GetCustomAttributes().Where(attr => attr.GetType().FullName == "ServiceStack.RouteAttribute");

                        if (!routes.Any())
                        {
                            continue;
                        }
                        allRequests.Add(request.Name);
                        requestModel.ReturnTypeTypeScriptName = returnTypeTypeScriptName;

                        requestModel.Name = request.Name;

                        var items = new List <ServiceStackRouteInfo>();
                        foreach (var route in routes)
                        {
                            var verbs = route.GetType().GetProperty("Verbs").GetValue(route) as string;
                            var path  = route.GetType().GetProperty("Path").GetValue(route) as string;

                            if (string.IsNullOrWhiteSpace(verbs))
                            {
                                throw new Exception("No HTTP verbs defined");
                            }
                            if (string.IsNullOrWhiteSpace(path))
                            {
                                throw new Exception("No route path defined");
                            }

                            foreach (var verb in verbs.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(v => v.Trim()))
                            {
                                if (verb.Equals("Options", StringComparison.OrdinalIgnoreCase))
                                {
                                    continue;
                                }

                                var isGet     = verb.Equals("get", StringComparison.OrdinalIgnoreCase);
                                var url       = _ssHelper.GenerateUrlFromRoute(path, request, isGet, _options.SupportMomentJs, out var routeParameters);
                                var rawUrl    = _ssHelper.GenerateUrlFromRoute(path, request, false, _options.SupportMomentJs, out _);
                                var routeInfo = new ServiceStackRouteInfo(verb, path, url, rawUrl, routeParameters);
                                routeInfo.ReturnTypeTypeScriptName = requestModel.ReturnTypeTypeScriptName;
                                items.Add(routeInfo);
                                requestModel.Routes.Add(routeInfo);
                            }
                        }

                        foreach (var item in items)
                        {
                            var actionParameters = _ssHelper.GetActionParameters(request, item);
                            item.ActionParameters = actionParameters;
                            if (item.Verb == "POST" || item.Verb == "PUT" || item.Verb == "PATCH" || item.Verb == "GET")
                            {
                                actionParameters.Add(new ActionParameterInfo
                                {
                                    Name = item.Verb == "GET" ? "querystring" : "body",
                                    Type = item.RouteParameters.Any()
                                        ? $"Omit<{TypeConverter.GetTypeScriptName(request)}, {string.Join(" | ", item.RouteParameters.Select(p => $"'{p}'"))}>"
                                        : TypeConverter.GetTypeScriptName(request)
                                });
                                actionParameters.ForEach(a =>
                                {
                                    if (a.Type.Contains(".") && !a.Type.StartsWith("Enums."))
                                    {
                                        foreach (var s in a.Type.GetTopLevelNamespaces())
                                        {
                                            requiredImports.Add(s);
                                        }
                                    }
                                });
                                item.ActionParameters = actionParameters;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Failure processing request type: " + request.FullName);
                        Console.WriteLine("Message: " + ex.Message);
                        throw;
                    }
                }
            }

            if (_options.GenerateAsModules)
            {
                model.RequiredImports = requiredImports.ToList();
            }

            SetupHelpers();
            SetupTemplates("ServiceStack_Angular");
            var actions = Handlebars.Compile("{{> main.hbs }}")(model);

            File.WriteAllText(Path.Combine(_options.OutputFilePath, _options.ActionsOutputFileName ?? "actions.ts"), actions);
        }