Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically generate TypeScript client code for ASP.NET Core controllers

I'm searching for a way to automatically generate the TypesScript client code from a ASP.NET Core Web-Application (Currently using Visual Studio 2017RC and webpack).

Are there any existing tools to generate the TypeScript client either in the build pipeline from visual studio or with webpack? I tried to use swagger middleware and then generate to client from the swagger URL, but I'm not sure if the intermediate swagger generation is the right tool for the job. Also the disconnectedness from the build tools is not ideal.

like image 992
Fionn Avatar asked Dec 22 '16 03:12

Fionn


2 Answers

  1. Both NSwag and Swashbuckle/swagger-codegen are awesome.

    1. Typewriter is another awesome option.

    2. So, here is how I would take the decision:

      • Need to generate TypeScript Models as per the C# models, go for TypeWriter
      • Need to generate complete client side code with command line that you can add as a build or publish step go for Swagger Code Gen

      • Need to play with the generated Swagger Spec or Generated Code to add your customizations - go for NSwag

      • Need to show Client inline code snippets in Swagger UI, use SwaggerUI-snippets
like image 83
Gaurav Madaan Avatar answered Nov 03 '22 07:11

Gaurav Madaan


You can add Swashbuckle to your ASP.NET Core webapp and then generate Swagger/OpenAPI spec.

With the Swagger/OpenAPI spec, you can then use Swagger Codegen to generate not only TypeScript API clients (Node, Angular, Angular2, Fetch), but also API clients in C#, Java, JS, Swift, etc as well as API documentation.

The easiest way to try Swagger Codegen is to use https://editor.swagger.io by importing your spec and then select the target API client/server you want to generate. (https://editor.swagger.io leverages https://generator.swagger.io to generate code and generator.swagger.io is powered by Swagger Codegen)

Swagger Codegen can also generate server stubs in C# Nancyfx or ASP.NET core so you may consider following the contract-first approach by writing the spec using https://editor.swagger.io and then generating both API clients and server stubs.

UPDATE: On May 2018, about 50 top contributors and template creators of Swagger Codegen decided to fork Swagger Codegen to maintain a community-driven version called OpenAPI Generator. Please refer to the Q&A for more information.

like image 17
William Cheng Avatar answered Nov 03 '22 08:11

William Cheng