Skip to content

Commit ff3e042

Browse files
chore: merge to release branch (#378)
* feat: 枚举类型支持空格 * chore: add changeset * feat: priorityRule 为 both 和 include 的时候都设置includeTags, includePaths 默认值为 [/.*/g] (#376) * feat: priorityRule 为 both 和 include 的时候都设置includeTags * fix: 添加includePaths 默认值,解决没有设置includePaths时没有类型生成问题 * chore: generate changeset --------- Co-authored-by: AdoKevin <kevinlaw1024@gmail.com>
2 parents 7e72bae + 42ee4b9 commit ff3e042

File tree

6 files changed

+113
-9
lines changed

6 files changed

+113
-9
lines changed

.changeset/plain-yaks-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': patch
3+
---
4+
5+
priorityRule 为 both 和 include 的时候都设置includeTags, includePaths 的默认值为[/.*/g]

.changeset/tame-nails-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': patch
3+
---
4+
5+
feat: 枚举类型支持空格

src/generator/serviceGenarator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ export default class ServiceGenerator {
12041204
} else if (isAllNumeric(enumArray)) {
12051205
enumStr = `{${map(enumArray, (value) => `"STRING_NUMBER_${value}"="${value}"`).join(',')}}`;
12061206
} else {
1207-
enumStr = `{${map(enumArray, (value) => `${value}="${value}"`).join(',')}}`;
1207+
enumStr = `{${map(enumArray, (value) => `"${value}"="${value}"`).join(',')}}`;
12081208
}
12091209

12101210
// 翻译枚举
@@ -1243,7 +1243,7 @@ export default class ServiceGenerator {
12431243
} else if (isAllNumeric(enumArray)) {
12441244
enumLabelTypeStr = `{${map(enumArray, (value) => `"${value}":"STRING_NUMBER_${value}"`).join(',')}}`;
12451245
} else {
1246-
enumLabelTypeStr = `{${map(enumArray, (value) => `${value}:"${value}"`).join(',')}}`;
1246+
enumLabelTypeStr = `{${map(enumArray, (value) => `"${value}":"${value}"`).join(',')}}`;
12471247
}
12481248
}
12491249

src/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ export async function generateService({
255255
mockFolder,
256256
includeTags,
257257
excludeTags,
258+
includePaths,
259+
excludePaths,
258260
authorization,
259261
isTranslateToEnglishTag,
260262
priorityRule = PriorityRule.include,
@@ -300,14 +302,28 @@ export async function generateService({
300302
? map(includeTags, (item) =>
301303
typeof item === 'string' ? item.toLowerCase() : item
302304
)
303-
: priorityRule === PriorityRule.include
305+
: priorityRule === PriorityRule.include ||
306+
priorityRule === PriorityRule.both
307+
? [/.*/g]
308+
: null,
309+
includePaths: includePaths
310+
? map(includePaths, (item) =>
311+
typeof item === 'string' ? item.toLowerCase() : item
312+
)
313+
: priorityRule === PriorityRule.include ||
314+
priorityRule === PriorityRule.both
304315
? [/.*/g]
305316
: null,
306317
excludeTags: excludeTags
307318
? map(excludeTags, (item) =>
308319
typeof item === 'string' ? item.toLowerCase() : item
309320
)
310321
: null,
322+
excludePaths: excludePaths
323+
? map(excludePaths, (item) =>
324+
typeof item === 'string' ? item.toLowerCase() : item
325+
)
326+
: null,
311327
requestOptionsType: '{[key: string]: unknown}',
312328
namespace: 'API',
313329
isGenReactQuery: false,
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Hello World API",
5+
"version": "1.0.0",
6+
"description": "A simple API that returns a student object."
7+
},
8+
"servers": [
9+
{
10+
"url": "http://localhost:3000",
11+
"description": "Local server"
12+
}
13+
],
14+
"tags": [
15+
{
16+
"name": "student",
17+
"description": "Everything about Student"
18+
}
19+
],
20+
"paths": {
21+
"/hello": {
22+
"get": {
23+
"tags": ["student"],
24+
"summary": "Returns a student object",
25+
"responses": {
26+
"200": {
27+
"description": "A student object",
28+
"content": {
29+
"application/json": {
30+
"schema": {
31+
"$ref": "#/components/schemas/Student"
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
},
40+
"components": {
41+
"schemas": {
42+
"Major": {
43+
"type": "string",
44+
"enum": ["COMPUTER SCIENCE", "MATHEMATICS", "PHYSICS", "LITERATURE"],
45+
"example": "COMPUTER SCIENCE"
46+
},
47+
"Student": {
48+
"type": "object",
49+
"properties": {
50+
"name": {
51+
"type": "string",
52+
"example": "张三"
53+
},
54+
"age": {
55+
"type": "integer",
56+
"example": 20
57+
},
58+
"gender": {
59+
"type": "string",
60+
"enum": ["MALE", "FEMALE", "OTHER"],
61+
"example": "MALE"
62+
},
63+
"major": {
64+
"$ref": "#/components/schemas/Major"
65+
}
66+
},
67+
"required": ["name", "age", "gender", "major"]
68+
}
69+
}
70+
}
71+
}

test/test.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,15 @@ const gen = async () => {
7575
return `
7676
export async function ${api.functionName}(${api.params && api.hasParams ? `params: ${ctx.namespace}.${api.typeName}` : ''}) {
7777
return request.get('${api.path}', { params })
78-
}`
79-
}
80-
else {
78+
}`;
79+
} else {
8180
return `
8281
export async function ${api.functionName}(${api.body ? `data: ${api.body.type}` : ''}) {
8382
return request.post('${api.path}', data)
84-
}`
83+
}`;
8584
}
86-
}
87-
}
85+
},
86+
},
8887
},
8988
});
9089

@@ -138,6 +137,14 @@ export async function ${api.functionName}(${api.body ? `data: ${api.body.type}`
138137
includeTags: ['pet'],
139138
});
140139

140+
// 测试 "COMPUTER SCIENCE" 这一类的枚举值在生成的类型中不报错
141+
await openAPI.generateService({
142+
schemaPath: `${__dirname}/example-files/openapi-complex-enum-convert.json`,
143+
serversPath: './apis/complex-enum-convert',
144+
includePaths: [/.*/g],
145+
includeTags: [/.*/g],
146+
});
147+
141148
// 测试生成 JSON Schemas
142149
await openAPI.generateService({
143150
schemaPath: `${__dirname}/example-files/openapi-display-enum-label.json`,

0 commit comments

Comments
 (0)