Iva*_*lev 16 code-generation swagger typescript
我正在寻找一种从Swagger架构生成简单TypeScript接口的方法.我发现的大多数解决方案都是不必要的复杂.
我想生成这样的接口:
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,
}
Run Code Online (Sandbox Code Playgroud)
从这样的架构:
{
"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": {}
}
Run Code Online (Sandbox Code Playgroud)
Ser*_*kov 24
我正在使用swagger-typescript-api从 swagger 模式生成接口
npx swagger-typescript-api -p PATH_TO_YOUR_SCHEMA -o ./
Run Code Online (Sandbox Code Playgroud)
不确定这是否是理智的方法,这是我第一次与Swagger玩耍。
我碰到了以下链接,并从与之集成的项目中粘贴了架构。从顶部的“生成客户端”菜单中,我选择了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";
Run Code Online (Sandbox Code Playgroud)
也许稍加调整就可以完全满足您的需求。
进一步的阅读可能与https://github.com/swagger-api/swagger-codegen之类的工具有关,但是在线Web编辑器是一种快速而肮脏的方法。
你可以试试这个工具sw2dts,它生成的代码如下:
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;
Run Code Online (Sandbox Code Playgroud)
Color 枚举似乎需要一些调整,它应该包含要迭代的属性名称,而不是实数。
您还可以查看autorest.typescript客户端代码生成器。它为所有模型定义提供了良好的接口,准确地定义了只读属性和枚举。它支持一堆自定义扩展,这些自定义扩展可用于改善所生成客户端的用户体验。您可以看一下一些生成的示例客户端。
奖励:生成的typescript客户端可以在node.js中使用,也可以在webpack的帮助下在浏览器中使用。
我使用dtsgen,它在我的情况下运行良好。
yarn dtsgen --out ./path/to/generated-types.d.ts ./path/to/input/swagger.json.or.yml
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个仅生成类型而不是任何可运行代码的包。我认为这与问题中的要求相同。我发现最接近的只生成类型的是这个包:
https://www.npmjs.com/package/@manifoldco/swagger-to-ts
使用 @manifoldco/swagger-to-ts 的 2.0.0 将为问题中的架构生成以下内容:
/**
* 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";
}
Run Code Online (Sandbox Code Playgroud)
注意:有一个像这样命名的包,没有任何组织前缀。确保您尝试使用带有 @manifoldco 前缀的版本。起初我错过了这个细节并且非常困惑:-)。
| 归档时间: |
|
| 查看次数: |
15187 次 |
| 最近记录: |