reinforced-typings

Generate multiple interfaces for one class/interface


Is there a way to generate multiple interfaces/classes for one class.

I'll have a SignalR hub class that looks like this:

[TsInterface]
public class ChatHub : Hub
{

    public async Task<IEnumerable<string>> GetMessages() {...}

    public async Task EmitMessage(string msg) {...}
}

And this is what i want the output to be like:

interface ChatHub extends SignalR.Hub.Proxy {
    client: ChatHubClient;
    server: ChatHubServer;
}

interface ChatHubClient { // always empty. }

interface ChatHubServer {
    getMessages(): Promise<string[]>;
    emitMessage(msg: string): Promise<void>;
}

Solution

  • You can achieve that using simple interface code generator:

    using Reinforced.Typings.Ast;
    using Reinforced.Typings.Ast.TypeNames;
    using Reinforced.Typings.Generators;
    
    class ClientAppender : InterfaceCodeGenerator
    {
        public override RtInterface GenerateNode(Type element, RtInterface result, TypeResolver resolver)
        {
    
            var existing = base.GenerateNode(element, result, resolver);
            if (existing == null) return null;
    
            if (this.Context.Location.CurrentNamespace == null) return existing;
            RtInterface clientIFace = new RtInterface()
            {
                Name = new RtSimpleTypeName(string.Format("{0}Client", element.Name))
            };
            this.Context.Location.CurrentNamespace.CompilationUnits.Add(clientIFace);
            return existing;
        }
    }
    

    Then use it like

     builder.ExportAsInterface<ChatHub>()
                .WithPublicMethods()
                .WithPublicProperties()
                .WithCodeGenerator<ClientAppender>();
    

    Sorry, I had no free time to answer your previous question.