Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Multiple Typescript Files with NSwag

Tags:

swagger

nswag

I've just started looking into using NSwag to auto-generate typescript clients for a WebAPI project. I've naively started with this simple build step:

nswag webapi2swagger /assembly:bin\MyProject.WebAPI.dll /output:Swagger.json
nswag swagger2tsclient /input:Swagger.json /output:WebAPIs.ts

This works great... maybe too great. I've now got a 17000 line typescript file full of clients for every endpoint in the API.

I know I could specify individual classes or controllers for nswag to generate, but I'd like it to be automatic for new controllers as we continue to build the system. Ideally, I'd probably be best served with a set of typescript files where each file represented a single controller.

Does anyone know how I might use the command line tools to break up the output that way?

like image 435
Jake Stevenson Avatar asked Oct 22 '25 00:10

Jake Stevenson


2 Answers

I don't think this is possible with the current CLI tools, so I wrote a small console application which uses reflection to gather controllers from the webapi assembly, then calls the WebApiToSwaggerGenerator and SwaggerToTypeScriptClientGenerator for each controller individually.

like image 64
Jake Stevenson Avatar answered Oct 24 '25 21:10

Jake Stevenson


This is not possible yet but you can follow this feature's progress on: https://github.com/RicoSuter/NSwag/issues/1398

The page contains some suggested workarounds. One is to use this tool: https://github.com/hemiaoio/nswag-ts-splitter

like image 38
ofthelit Avatar answered Oct 24 '25 20:10

ofthelit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!