T4 Web API C# to Typescript class library [closed]
Asked Answered
A

3

7

I've recently asked about C# classes to T4 viewmodels, but this is different so I separated it out into a second question

I'd like to automate the generate of a client library in typescript that includes endpoints for every web api method that is adorned with the HttpGet, Post, Put, Delete, Patch attributes.

This would generate the jQuery ajax calls to interface with the web api calls.

Anyone seen anything like this? I would LOVE to save some time having to hack together a T4 script.

Thanks!

Avalos answered 17/10, 2013 at 21:37 Comment(0)
U
5

It looks like this guy used T4 to generate a TypeScript proxy to his Web API service. http://galador.net/codeblog/post/2013/11/12/Client-Side-Web-Application-primer.aspx

However, if your only client will be from the browser, you may want to consider using SignalR instead with this T4 template. https://gist.github.com/robfe/4583549

There are also a few libraries that generate TypeScript definition files from CLR types:

Unreflective answered 22/11, 2013 at 7:42 Comment(3)
Ultimately I wrote my own using T4. Quite the learning experience! These resources are excellent however!Avalos
Have you published your T4 anywhere?Darbee
Typewriter Visual Studio Extension also does this - frhagn.github.io/Typewriter/index.htmlPrior
H
3

Try using Weld: https://weld.codeplex.com/

All you have to do is add an attribute to your controller action and Weld will create a typescript proxy.

From their website:

Instead of:

 [HttpGet]
 public int Sum(int x,int y)
 {
     return x + y;
 }

and using $.ajax

 var url = "/Home/Sum";
 var data = { x: 2 , y: 3};
 $.ajax({
     url: url,
     data: data,
     success: showResult
 });

Just add a attribute:

[AjaxMethod]

And do this in your typescript:

HomeController.prototype.Sum(2, 3, showResult); 
Hebrides answered 17/3, 2015 at 16:28 Comment(1)
I don't really like the Weld solution that much. I would much rather prefer to build a visual studio extension that generates typescript classes on save using Roslyn to parse the document, so something like this: [TypeScriptExport("~/typescript/controllers/")] or just [TypeScriptExport("Users")]. I have started working on the project but don't have much time.. if anyone wants to collaborate let me know.Hebrides
I
0

I am currently working on a toolchain to solve this exact problem: Generate the client code for calling a Web API controller.

The generators can be called in a T4 template, the cmd line and a Windows GUI:

enter image description here

Check out http://nswag.org

Inapplicable answered 25/9, 2015 at 7:29 Comment(1)
Recommendation requests for off-site resources or tools are off-topic on Stack Overflow. If you answer them, you specifically reinforce the belief that Stack Overflow is a good place to answer those questions. It is not. Please don't answer these questions even if you know a good answer as most answers will be highly opinionated ("I personally like..."). You can open the flag dialogue on the question and see the close reason in full under the off-topic category, or in the help center. You should also not even answer in a comment, as the effect is similar to an actual answer.Raynold

© 2022 - 2024 — McMap. All rights reserved.