问题描述
我参与了一个用 TypeScipt 编写的项目,我正在尝试添加与 Swagger 兼容的 TypeScript Web Server.
考虑到易维护性,实现它的最基本策略是什么.
I am part of a project written in TypeScipt and I am trying to add TypeScript Web Server which will be compatible with Swagger.
What is the most basic strategy to implement it, considering easy maintainability.
对于 Typescript,我注意到存在用于从 TypeScript 接口生成 JSON 模型的Typson"库.
For Typescript I have notice that exists 'Typson' library for generating a JSON Model from TypeScript Interface.
对于 Swagger,我尝试使用 'swagger-node-restify' 库,因为它支持将 JSON 模型添加到 swagger.
For Swagger, I have tried using 'swagger-node-restify' library, since it supports adding JSON models to swagger.
但是,我遇到了一些问题:
However, I encounter some problems:
- Typson 不支持 typeScript 导入的语法 -(
从restify"导入{Server}
) - 我尝试实现 'swagger-node-restify' 示例(Pet 示例),但是 localhost:8080/api-docs.json GET 请求的响应缺少 API 的所有 SPEC 数据.
{"apiVersion":"0.1","swaggerVersion":"1.1","basePath":"http://localhost:8080","apis":[{"path":"/api-docs.{format}/pet","description":"none"}]}
推荐答案
我建议使用 yaml
或 json
描述一个 Swagger 兼容 API并从中生成服务器.
I suggest to describe a Swagger compliant API using yaml
or json
and to generate the server from that.
swagger-server 可以在 express
之上构建 API实时(无源代码生成).
swagger-server can build APIs on top of express
in real time (no source code generation).
有 JavaScript
代码生成器:
There are JavaScript
code generators :
使用
-l nodejs-server
swagger-node 是一个不错的选择,但似乎很难与TypeScript
swagger-node is a great alternative but seems hard to integrate with TypeScript
这篇关于在 TypeScript 中创建 swagger web 服务的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!