My concept involves incorporating two key elements:
- Converting C# Dto's (Data-transfer-objects) into TypeScript interfaces to ensure synchronization between client-side models and server-side.
- Transforming ASP .Net Core controller endpoints into TypeScript classes that utilize an http-service or similar mechanism. This is done to maintain alignment between client-side requests and the server.
Whenever a modification is made to a controller or dto, the generated TypeScript items should automatically update to stay aligned during development.
After conducting some research, I came across various Stack Overflow threads and other resources:
DTO to TypeScript generator suggests using the TypeLite library. While it seems promising, the documentation indicates a requirement for a [TsClass] Attribute or a class reference on startup. However, my project structure places all dtos in a *.Dtos namespace, making it difficult to apply TypeScript.Definitions().ForNameSpace(). Additionally, this only addresses the initial problem/idea.
Swashbuckly.AspNetCore could help generate swagger documentation from both controllers and dtos. The challenge lies in interpreting the swagger documentation and creating typescript classes and interfaces accordingly. One downside is that this method requires server startup, which I aim to avoid as it complicates updating upon file changes.
For context, this is a new project without any legacy code to update. All ASP .NET Core endpoints will return IActionResult to facilitate returning Ok(), BadRequest(), and so forth. Obtaining the return model may prove challenging since there's no straightforward way to retrieve the produced dto.
To resolve these issues, I have considered the following solutions:
Create a separate package/application utilizing the Swashbuckly library to generate models and controllers without initiating the entire server.
Add annotations to each endpoint, like [Produces(SomeDto)], followed by developing a small console application using reflection to gather information and generate TypeScript code. This approach, however, necessitates maintaining duplicate information to ensure consistency.
Unfortunately, neither of these strategies support auto-updating upon saving C# source files.
I am eager to engage in discussions and welcome any suggestions for improvement.