DEV Community

Philip Schoeman
Philip Schoeman

Posted on

WillCore.Requests: Auto Generating Fetch and XMLXHR Requests for Web API

.NET CORE 2.2 WebAPI and JavaScript

A developer usually tend to avoid repetitive work. In coding we have the DRY principle, and I like to apply that philosophy to the way I code. That is why I got frustrated typing out Fetch request after Fetch request trying to consume a .NET Core Web API service I wrote for a project of my own. Why can't I just reference a WSDL and add a service reference to my JS?

That is when I got the idea to write a library to build JS code from your controllers, parameters and results. The generated code should also have full JSDoc comments so you can have intellisense support in Visual Studio.

I tried different routes, from T4 templates to building JavaScript via a node reference in C#. In the end I ended up with custom templates and a hierarchical C# class structure mimicking that of JavaScript.

I am planning to add the following to the library:
1) C# code generator that will also automatically build a NuGet package with all the code to consume your API.
2) C# class and action level attributes that allows you to generate custom JSDoc description comments.
3) Functionality to auto generate a HTML page that will allow you to add predefined, calculated and static parameters and results for every controller action, so that you can test your API by simply open the page.

I think it turned out pretty good, but any feedback is welcome.

Please note that the library is still in alpha state, but I have used it on a different projects and it seems quite stable. Is this something people would use?

GitHub: https://github.com/PhilipCoder/WillCoreRequests

Latest comments (1)

Collapse
 
webhostingecom1 profile image
Web Hosting eCommerce • Edited

Great for sharing about web API request generate. your this post is valuable for me and it will definitely help my website SiteGround Coupon keep it up good work.