Chau Tran
Chau Tran

Reputation: 5088

Swagger-Codegen: How do I consolidate all files into one file for Client-Code-Generation

I’ve just got started with Swagger and NodeJS. I was able to implement Swagger to my NodeExpress application and was also able to generate typescript-client-code with Swagger-Codegen (Typescript-Angular) to be exact.

One problem that I have is the generated code is so spread out many different files. I was hoping that it only output one file api.ts and it contains everything from API calls and interfaces/models.

I’ve been looking for a way to solve this problem because it is hard to read and maintain the generated-client-code as the backend grows.

Any suggestions or pointers would be much appreciated.

Happy Holiday! Thank you

EDIT: I have been looking for answers for this for a couple of days and still haven't found one. I'm currently working on a project with ASP.NET Core and they have NSwag which does what I want to achieve with Node Swagger.

Upvotes: 11

Views: 4319

Answers (2)

JP Ventura
JP Ventura

Reputation: 5732

TL; DR

You may compile all files into a single *.ts file as shown at this post.

Continuous Integration Approach

Swagger code generator simplifies maintenance because it allows you to think in terms of continuous integrations.

You should not be worried about code review or aesthetics (because it is a machine generated code), but about:

  • API versioning
  • Functions, methods and classes signatures
  • Documentation

If you are working with a CI system such as Jenkins or Ansible, you could automatically deploy the library to a private NPM account (for JS and TS) or Maven server (for Java and Kotlin).

Private Maven

Keeping the package version number consistently updated will allow the IDE to correctly prompt the user about updates on the API.

enter image description here

Upvotes: 8

user196106
user196106

Reputation:

Swagger uses Mustache templates for generating the code. For making simpler changes you can simple create a copy of one of the built-in templates and modify that.

Then you can use your modified template like this:

swagger-codegen-cli generate -t path/to/template/dir/ -i spec.json

The output directory structure, however, cannot be changed using templates alone. For that you'd need a custom codegen module. You can either create your own or modify one of the built-in ones.

Upvotes: 4

Related Questions