如何从Swagger模式生成基本的TypeScript接口?
我正在寻找一种方法,从一个招摇过市的模式生成简单的类型脚本接口。我发现的大多数解决方案都是不必要的复杂 我希望生成如下界面:如何从Swagger模式生成基本的TypeScript接口?,typescript,swagger,code-generation,Typescript,Swagger,Code Generation,我正在寻找一种方法,从一个招摇过市的模式生成简单的类型脚本接口。我发现的大多数解决方案都是不必要的复杂 我希望生成如下界面: export interface IBar { a?: string; b: number; c: Date; baz?: IBaz; } export interface IBaz { d: number; color: Color; } export enum Color { RED = 0, GRE
export interface IBar {
a?: string;
b: number;
c: Date;
baz?: IBaz;
}
export interface IBaz {
d: number;
color: Color;
}
export enum Color {
RED = 0,
GREEN = 1,
BLUE = 2,
}
从这样的模式:
{
"x-generator": "NSwag v11.14.0.0 (NJsonSchema v9.10.24.0 (Newtonsoft.Json v9.0.0.0))",
"swagger": "2.0",
"info": {
"title": "",
"version": ""
},
"schemes": [],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/api/Foo/GetBarDescriptions": {
"get": {
"tags": [
"Foo"
],
"operationId": "Foo_GetBarDescriptions",
"parameters": [],
"responses": {
"200": {
"description": "",
"schema": {
"type": "array",
"items": {
"type": "string"
}
},
"x-nullable": true
}
}
}
},
"/api/Foo/GetBar": {
"get": {
"tags": [
"Foo"
],
"operationId": "Foo_GetBar",
"parameters": [
{
"type": "integer",
"name": "id",
"in": "query",
"required": true,
"x-nullable": false,
"format": "int32"
}
],
"responses": {
"200": {
"description": "",
"schema": {
"$ref": "#/definitions/Bar"
},
"x-nullable": true
}
}
}
},
"/api/Foo/SetBar": {
"post": {
"tags": [
"Foo"
],
"operationId": "Foo_SetBar",
"parameters": [
{
"name": "value",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/Bar"
},
"x-nullable": true
}
],
"responses": {
"204": {
"description": ""
}
}
}
}
},
"definitions": {
"Bar": {
"type": "object",
"additionalProperties": false,
"required": [
"B",
"C"
],
"properties": {
"A": {
"type": "string"
},
"B": {
"type": "integer",
"format": "int32"
},
"C": {
"type": "string",
"format": "date-time"
},
"Baz": {
"$ref": "#/definitions/Baz"
}
}
},
"Baz": {
"type": "object",
"additionalProperties": false,
"required": [
"D",
"Color"
],
"properties": {
"D": {
"type": "number",
"format": "decimal"
},
"Color": {
"$ref": "#/definitions/Color"
}
}
},
"Color": {
"type": "integer",
"description": "",
"x-enumNames": [
"RED",
"GREEN",
"BLUE"
],
"enum": [
0,
1,
2
]
}
},
"parameters": {},
"responses": {},
"securityDefinitions": {}
}
我不确定这是不是一个明智的方法,这是我第一次和大摇大摆玩 我偶然发现了以下链接,并粘贴了与之集成的项目中的模式。从顶部的“生成客户机”菜单中,我选择了一个TypeScript预设,它生成了一个最小的项目,在这个项目中,我可以提取我需要的位、接口和类等 我试着运行你的模式。下面是生成代码的一小部分摘录:
export interface Bar {
"a"?: string;
"b": number;
"c": Date;
"baz"?: Baz;
}
export interface Baz {
"d": number;
"color": Color;
}
/**
*
*/
export type Color = "0" | "1" | "2";
也许再稍微调整一下,它就能完全满足你的需求
进一步的阅读可能是关于像这样的工具,但在线web编辑器是一种快速而肮脏的方法。您还可以看看客户端代码生成器。它为所有模型定义提供了良好的接口,准确地定义了只读属性和枚举。它支持一系列有助于改善生成的客户端的用户体验的功能。您可以查看一些生成的
好处:生成的typescript客户端可以在node.js中工作,也可以在webpack的帮助下在浏览器中工作。您还可以使用简单的json-to-typescript转换器从每个模式生成每个接口。 它不是全自动的,但总比没有好。。。简单。您可以尝试此工具,它生成如下代码:
export interface Bar {
A?: string;
B: number; // int32
C: string; // date-time
Baz?: Baz;
}
export interface Baz {
D: number; // decimal
Color: Color;
}
/**
*
*/
export type Color = 0 | 1 | 2;
颜色枚举似乎需要稍作调整,它应该包含要迭代的属性的名称,而不是实数。我使用它,在我的例子中效果很好
yarn dtsgen --out ./path/to/generated-types.d.ts ./path/to/input/swagger.json.or.yml
灵感来自:
为各种语言和框架生成服务器存根和客户端SDK,包括Node.js
要生成Node.js服务器存根,请使用-l nodejs server
参数运行codegen
示例(Mac):
swagger codegen generate-l typescript angular-i swagger.yaml-o~/Downloads/ts test
我使用它从swagger模式生成接口
npx swagger-typescript-api -p PATH_TO_YOUR_SCHEMA -o ./
我正在寻找一个只生成类型的包,而不是任何可运行的代码。我认为这与问题中的要求相同。我所发现的仅生成类型的最接近的软件包是: 使用@manifoldco/swagger的2.0.0版本到ts将为问题中的模式生成以下内容:
/**
* This file was auto-generated by swagger-to-ts.
* Do not make direct changes to the file.
*/
export interface definitions {
Bar: { A?: string; B: number; C: string; Baz?: definitions["Baz"] };
Baz: { D: number; Color: definitions["Color"] };
Color: "0" | "1" | "2";
}
注意:有一个名为这样的包,没有任何组织前缀。请确保尝试带有@manifoldco前缀的。起初,我忽略了这个细节,非常困惑:-)。对于当前的案例,我决定采用过度招摇的方式,并将其用于新一代。我仍然在为将来的案例(当然是NodeJ)寻找一个最合适的生成器,最好是在第三方模板引擎上使用JS插值字符串的生成器。我可能误解了您或此链接,但您上面生成的模式引用了NSwag。其文档声称集成了AutoRest(以下列出的解决方案之一)。也许它是新的,但它说,
该项目在一个工具链中结合了Swashback(OpenAPI/Swagger生成)和AutoRest(客户端生成)的功能。
from:。我自己也在调查这件事,做完后我会再次发表评论。是的,这件事很轻松。更困难的部分是为模式生成设置项目,因为注释和其他因素必须满足Swagger/NSwag的期望。由于在上面的示例中已经完成了这项工作,NSwagStudio只需单击一下即可生成TypeScript,并且可以保存配置以供自动化工具链使用。我唯一的抱怨是提供的模板非常糟糕。。。超过90%的默认生成的代理/客户端代码包含重复,这应该是一个曾经声明过的独立问题,而默认DTO模板包含我无法消除的意外类型联合。