简体   繁体   English

Swagger-Codegen:如何将所有文件合并到一个文件中以进行客户端代码生成

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

I've just got started with Swagger and NodeJS. 我刚刚开始使用Swagger和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. 我能够将Swagger实现到我的NodeExpress应用程序,并且能够准确地生成带有Swagger-Codegen(Typescript-Angular)的typescript-client-code。

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. 我希望它只输出一个文件api.ts ,它包含API调用和接口/模型的所有内容。

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 . 我目前正在开发一个使用ASP.NET Core的项目,他们有NSwag ,它可以实现我想用Node Swagger实现的目标。

TL; TL; DR DR

You may compile all files into a single *.ts file as shown at this post . 你可以编译所有的文件到一个单一的*.ts文件在显示这个职位

Continuous Integration Approach 持续集成方法

Swagger code generator simplifies maintenance because it allows you to think in terms of continuous integrations . Swagger代码生成器简化了维护,因为它允许您根据连续集成进行思考。

You should not be worried about code review or aesthetics (because it is a machine generated code), but about: 你不应该担心代码审查或美学(因为它是一个机器生成的代码),但是关于:

  • API versioning API版本控制
  • 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). 如果您正在使用诸如Jenkins或Ansible之类的CI系统,则可以自动将库部署到私有NPM帐户 (用于JS和TS)或Maven服务器 (用于Java和Kotlin)。

私人Maven

Keeping the package version number consistently updated will allow the IDE to correctly prompt the user about updates on the API. 保持程序包版本号不断更新将允许IDE正确地提示用户有关API的更新。

在此输入图像描述

Swagger uses Mustache templates for generating the code. Swagger使用Mustache模板生成代码。 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. 为此,您需要一个自定义codegen模块。 You can either create your own or modify one of the built-in ones . 您可以创建自己的或修改其中一个内置的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM