Skip to content

Commit c6a2894

Browse files
feat: support response type comments
1 parent ba4a9c5 commit c6a2894

File tree

4 files changed

+130
-8
lines changed

4 files changed

+130
-8
lines changed

.changeset/stale-cycles-win.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': minor
3+
---
4+
5+
feat: support response type comments

src/generator/util.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export function getDefaultType(
254254
return `{ ${keys(schemaObject.properties)
255255
.map((key) => {
256256
let required = false;
257+
const property = (schemaObject.properties?.[key] || {}) as SchemaObject;
257258
258259
if (isBoolean(schemaObject.required) && schemaObject.required) {
259260
required = true;
@@ -266,20 +267,20 @@ export function getDefaultType(
266267
required = true;
267268
}
268269
269-
if (
270-
'required' in (schemaObject.properties[key] || {}) &&
271-
(schemaObject.properties[key] as SchemaObject)?.required
272-
) {
270+
if (property.required) {
273271
required = true;
274272
}
273+
275274
/**
276275
* 将类型属性变为字符串,兼容错误格式如:
277276
* 3d_tile(数字开头)等错误命名,
278277
* 在后面进行格式化的时候会将正确的字符串转换为正常形式,
279278
* 错误的继续保留字符串。
280279
* */
281-
return `'${key}'${required ? '' : '?'}: ${getDefaultType(
282-
schemaObject.properties?.[key],
280+
return `
281+
${property.description ? `/** ${property.description} */` : ''}
282+
'${key}'${required ? '' : '?'}: ${getDefaultType(
283+
property,
283284
namespace
284285
)}; `;
285286
})
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"openapi": "3.0.2",
3+
"info": {
4+
"title": "Swagger Petstore - OpenAPI 3.0",
5+
"description": "This is a sample Pet Store Server based on the OpenAPI 3.0 specification. You can find out more about\nSwagger at [http://swagger.io](http://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!\nYou can now help us improve the API whether it's by making changes to the definition itself or to the code.\nThat way, with time, we can improve the API in general, and expose some of the new features in OAS3.\n\nSome useful links:\n- [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)\n- [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)",
6+
"termsOfService": "http://swagger.io/terms/",
7+
"contact": {
8+
"email": "apiteam@swagger.io"
9+
},
10+
"license": {
11+
"name": "Apache 2.0",
12+
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
13+
},
14+
"version": "1.0.19"
15+
},
16+
"externalDocs": {
17+
"description": "Find out more about Swagger",
18+
"url": "http://swagger.io"
19+
},
20+
"servers": [
21+
{
22+
"url": "/api/v3"
23+
}
24+
],
25+
"paths": {
26+
"/cgi-bin/gettoken": {
27+
"get": {
28+
"summary": "获取access_token",
29+
"deprecated": false,
30+
"description": "> **为了安全考虑,开发者请勿将access_token返回给前端,需要开发者保存在后台,所有访问企业微信api的请求由后台发起**\n\n获取access_token是调用企业微信API接口的第一步,相当于创建了一个登录凭证,其它的业务API接口,都需要依赖于access_token来鉴权调用者身份。\n因此开发者,在使用业务接口前,要明确access_token的颁发来源,使用正确的access_token。\n\n\n**权限说明:**\n每个应用有独立的secret,获取到的access_token只能本应用使用,所以每个应用的access_token应该分开来获取。\n\n\n**注意事项:**\n开发者需要缓存access_token,用于后续接口的调用(注意:不能频繁调用gettoken接口,否则会受到频率拦截)。当access_token失效或过期时,需要重新获取。\n\naccess_token的有效期通过返回的expires_in来传达,正常情况下为7200秒(2小时),有效期内重复获取返回相同结果,过期后获取会返回新的access_token。\n由于企业微信每个应用的access_token是彼此独立的,所以进行缓存时需要区分应用来进行存储。\naccess_token至少保留512字节的存储空间。\n企业微信可能会出于运营需要,提前使access_token失效,开发者应实现access_token失效时重新获取的逻辑。",
31+
"tags": [],
32+
"parameters": [
33+
{
34+
"name": "corpid",
35+
"in": "query",
36+
"description": "企业ID,获取方式参考:[术语说明-corpid](https://developer.work.weixin.qq.com/document/path/91039#14953/corpid)",
37+
"required": true,
38+
"example": "ID",
39+
"schema": {
40+
"type": "string"
41+
}
42+
},
43+
{
44+
"name": "corpsecret",
45+
"in": "query",
46+
"description": "应用的凭证密钥,获取方式参考:[术语说明-secret](https://developer.work.weixin.qq.com/document/path/91039#14953/secret)",
47+
"required": true,
48+
"example": "SECRET",
49+
"schema": {
50+
"type": "string"
51+
}
52+
}
53+
],
54+
"responses": {
55+
"200": {
56+
"description": "",
57+
"content": {
58+
"application/json": {
59+
"schema": {
60+
"type": "object",
61+
"properties": {
62+
"errcode": {
63+
"type": "integer",
64+
"description": "出错返回码,为0表示成功,非0表示调用失败"
65+
},
66+
"errmsg": {
67+
"type": "string",
68+
"description": "返回码提示语"
69+
},
70+
"access_token": {
71+
"type": "string",
72+
"description": "获取到的凭证,最长为512字节"
73+
},
74+
"expires_in": {
75+
"type": "integer",
76+
"description": "凭证的有效时间(秒)"
77+
}
78+
},
79+
"required": [
80+
"errcode",
81+
"errmsg",
82+
"access_token",
83+
"expires_in"
84+
],
85+
"x-apifox-orders": [
86+
"errcode",
87+
"errmsg",
88+
"access_token",
89+
"expires_in"
90+
],
91+
"x-apifox-ignore-properties": []
92+
},
93+
"example": {
94+
"errcode": 0,
95+
"errmsg": "ok",
96+
"access_token": "accesstoken000001",
97+
"expires_in": 7200
98+
}
99+
}
100+
},
101+
"headers": {},
102+
"x-apifox-name": "成功"
103+
}
104+
},
105+
"security": [],
106+
"x-apifox-sourceurl": "https://developer.work.weixin.qq.com/document/path/91039",
107+
"x-apifox-folder": "企业内部开发/开发指南",
108+
"x-apifox-status": "released",
109+
"x-run-in-apifox": "https://app.apifox.com/web/project/6276513/apis/api-288122020-run"
110+
}
111+
}
112+
},
113+
"components": {
114+
"schemas": {}
115+
}
116+
}

test/genOpenapi.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { generateService } = require('../dist/index');
22

33
generateService({
4-
schemaPath: `${__dirname}/example-files/openapi-desc-enum.json`,
5-
serversPath: './apis/openapi-desc-enum',
4+
schemaPath: `${__dirname}/example-files/openapi-response-desc.json`,
5+
serversPath: './apis/openapi-response-desc',
66
isSupportParseEnumDesc: true,
77
});

0 commit comments

Comments
 (0)