Typescript generation of any class

We use the /types/typescript DTO generation almost every day.

However is there some way of generating a Typescript definition (.ts) for a specific class or many classes that are not necessary DTOs or are not in the DTO project? The logic to turn a class into Typescript must already exist.

Something like:

Typescript.Generate(Type); // -> type.ts
Typescript.Generate(Type[]); // -> types.ts

No it only works on your Services DTOs, there are IncludeTypes/ExcludeTypes DTO options, but otherwise you’d need to create a dummy service with just the Types you want to generate DTO’s for.

Is there any way to use the classes you already use for the DTOs as a utility somewhere else? Or must I look elsewhere on converting a C# class to typescript?

My previous comment listed the available options.

We needed something similar, but found that the existing DTO generation stuff was very custom to ServiceStack and not extensible. What we needed was to generate js objects for our static / const permissions based on our C# objects (so the server and client side could easily stay in sync), so we created a SS plugin. It’s not the prettiest, but you can possibly adapt to generate what you need. Here you go:

/// <summary>
/// Plugin adds the /permissions.js route to get the list of permissions defined in the system js format
/// Additionally, browsing to /metadata, scrolling to the bottom under Plugin Links, you can click to download it
/// </summary>
public class PermissionGeneratorPlugin : IPlugin
{
    public string AtRestPath = "/permissions.js";

    public void Register(IAppHost appHost)
    {
        appHost.RegisterService<PermissionGeneratorService>(AtRestPath);
        appHost
            .GetPlugin<MetadataFeature>()
            .AddPluginLink(AtRestPath.TrimStart('/'), "Download Permissions JS");

    }
}

public class PermissionGeneratorRequest { }

[DefaultRequest(typeof(PermissionGeneratorRequest))]
public class PermissionGeneratorService : Service
{
    public object Any(PermissionGeneratorRequest req)
    {
        var result = new HttpResult(PermissionJavascriptGenerator.Generate());
        result.Headers.Add("Content-Disposition", "attachment;filename=permissions.js");
        return result;
    }
}

public static class PermissionJavascriptGenerator
{
    private static string beginning = "/* eslint-disable */\nexport default ";
    private static string end = ";";

    private static Dictionary<string, Dictionary<string, string>> TypeToPermissionListMap { get; set; } = new Dictionary<string, Dictionary<string, string>>();

    private static List<Type> Types { get; set; } = new List<Type>();

    static PermissionJavascriptGenerator()
    {
        Types.Add(typeof(Permissions.System));
        Types.Add(typeof(Permissions.User));
        Types.Add(typeof(Permissions.Event));
        Types.Add(typeof(Permissions.Agency));

        foreach (var type in Types)
        {
            var fields = type.GetConstantsAndValues();
            TypeToPermissionListMap.Add(type.Name, fields);
        }
    }

    public static string Generate()
    {
        var sb = new StringBuilder(beginning);

        sb.Append(TypeToPermissionListMap.ToJson().IndentJson());

        sb.Append(end);

        return sb.ToString();
    }
}

So for a class like this:

public static class System
    {
        public const string CreatePermissionGroups = "Create permission groups";
        public const string SearchPermissionGroups = "Search permission groups";
        public const string DeletePermissionGroups = "Delete permission groups";

        public static List<string> All = new List<string>()
        {
            CreatePermissionGroups,
            SearchPermissionGroups,
            DeletePermissionGroups
        };
    }

it generates this:

export default {
    "System": {
        "CreatePermissionGroups": "Create permission groups",
        "SearchPermissionGroups": "Search permission groups",
        "DeletePermissionGroups": "Delete permission groups"
    }
}

For anyone interested, I found the follow VS extension.

https://marketplace.visualstudio.com/items?itemName=frhagn.Typewriter