Question

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!

Was it helpful?

Solution

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:

OTHER TIPS

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); 

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top