@dazhicheng/openapi 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/cli.d.ts +2 -0
- package/dist/index.d.ts +141 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/log.d.ts +2 -0
- package/dist/mockGenerator.d.ts +8 -0
- package/dist/openAPIParserMock/index.d.ts +7 -0
- package/dist/openAPIParserMock/primitives.d.ts +31 -0
- package/dist/openAPIParserMock/utils.d.ts +8 -0
- package/dist/serviceGenerator.d.ts +460 -0
- package/dist/util.d.ts +35 -0
- package/package.json +41 -4
- package/src/cli.ts +28 -108
- package/src/index.d.ts +141 -0
- package/src/index.ts +279 -0
- package/src/log.d.ts +2 -0
- package/src/log.ts +6 -0
- package/src/mockGenerator.d.ts +8 -0
- package/src/mockGenerator.ts +239 -0
- package/src/openAPIParserMock/index.d.ts +7 -0
- package/src/openAPIParserMock/index.ts +254 -0
- package/src/openAPIParserMock/primitives.d.ts +31 -0
- package/src/openAPIParserMock/primitives.ts +30 -0
- package/src/openAPIParserMock/utils.d.ts +8 -0
- package/src/openAPIParserMock/utils.ts +54 -0
- package/src/serviceGenerator.d.ts +460 -0
- package/src/serviceGenerator.ts +1090 -0
- package/src/typings.d.ts +6 -0
- package/src/util.d.ts +35 -0
- package/src/util.ts +203 -0
- package/templates/interface.njk +54 -0
- package/templates/serviceController.njk +44 -0
- package/templates/serviceIndex.njk +14 -0
- package/tsconfig.json +2 -1
- package/openapi.config.ts +0 -16
- package/src/discovery.ts +0 -44
- package/src/fetcher.ts +0 -19
- package/src/formatter.ts +0 -16
- package/src/generator.ts +0 -187
- package/src/parser.ts +0 -336
- package/src/types.ts +0 -91
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import Mock from "mockjs";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import { prettierFile, writeFile } from "./util";
|
|
4
|
+
import { dirname, join } from "path";
|
|
5
|
+
import OpenAPIParserMock from "./openAPIParserMock/index";
|
|
6
|
+
import Log from "./log";
|
|
7
|
+
import pinyin from "tiny-pinyin";
|
|
8
|
+
import { GenerateServiceProps } from "./index";
|
|
9
|
+
Mock.Random.extend({
|
|
10
|
+
country() {
|
|
11
|
+
const data = [
|
|
12
|
+
"阿根廷",
|
|
13
|
+
"澳大利亚",
|
|
14
|
+
"巴西",
|
|
15
|
+
"加拿大",
|
|
16
|
+
"中国",
|
|
17
|
+
"法国",
|
|
18
|
+
"德国",
|
|
19
|
+
"印度",
|
|
20
|
+
"印度尼西亚",
|
|
21
|
+
"意大利",
|
|
22
|
+
"日本",
|
|
23
|
+
"韩国",
|
|
24
|
+
"墨西哥",
|
|
25
|
+
"俄罗斯",
|
|
26
|
+
"沙特阿拉伯",
|
|
27
|
+
"南非",
|
|
28
|
+
"土耳其",
|
|
29
|
+
"英国",
|
|
30
|
+
"美国",
|
|
31
|
+
];
|
|
32
|
+
const id = (Math.random() * data.length).toFixed();
|
|
33
|
+
return data[id];
|
|
34
|
+
},
|
|
35
|
+
phone() {
|
|
36
|
+
const phonepreFix = ["111", "112", "114"]; // 自己写前缀哈
|
|
37
|
+
return this.pick(phonepreFix) + Mock.mock(/\d{8}/); // Number()
|
|
38
|
+
},
|
|
39
|
+
status() {
|
|
40
|
+
const status = ["success", "error", "default", "processing", "warning"];
|
|
41
|
+
return status[(Math.random() * 4).toFixed(0)];
|
|
42
|
+
},
|
|
43
|
+
authority() {
|
|
44
|
+
const status = ["admin", "user", "guest"];
|
|
45
|
+
return status[(Math.random() * status.length).toFixed(0)];
|
|
46
|
+
},
|
|
47
|
+
avatar() {
|
|
48
|
+
const avatar = [
|
|
49
|
+
"https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg",
|
|
50
|
+
"https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png",
|
|
51
|
+
"https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png",
|
|
52
|
+
"https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png",
|
|
53
|
+
"https://gw.alipayobjects.com/zos/rmsportal/OKJXDXrmkNshAMvwtvhu.png",
|
|
54
|
+
"https://avatars0.githubusercontent.com/u/507615?s=40&v=4",
|
|
55
|
+
"https://avatars1.githubusercontent.com/u/8186664?s=40&v=4",
|
|
56
|
+
];
|
|
57
|
+
const id = (Math.random() * avatar.length).toFixed();
|
|
58
|
+
return avatar[id];
|
|
59
|
+
},
|
|
60
|
+
group() {
|
|
61
|
+
const data = ["体验技术部", "创新科技组", "前端 6 组", "区块链平台部", "服务技术部"];
|
|
62
|
+
const id = (Math.random() * data.length).toFixed();
|
|
63
|
+
return data[id];
|
|
64
|
+
},
|
|
65
|
+
label() {
|
|
66
|
+
const label = [
|
|
67
|
+
"很有想法的",
|
|
68
|
+
"小清新",
|
|
69
|
+
"傻白甜",
|
|
70
|
+
"阳光少年",
|
|
71
|
+
"大咖",
|
|
72
|
+
"健身达人",
|
|
73
|
+
"程序员",
|
|
74
|
+
"算法工程师",
|
|
75
|
+
"川妹子",
|
|
76
|
+
"名望程序员",
|
|
77
|
+
"大长腿",
|
|
78
|
+
"海纳百川",
|
|
79
|
+
"专注设计",
|
|
80
|
+
"爱好广泛",
|
|
81
|
+
"IT 互联网",
|
|
82
|
+
];
|
|
83
|
+
const id = (Math.random() * label.length).toFixed();
|
|
84
|
+
return label[id];
|
|
85
|
+
},
|
|
86
|
+
href() {
|
|
87
|
+
const href = [
|
|
88
|
+
"https://preview.pro.ant.design/dashboard/analysis",
|
|
89
|
+
"https://ant.design",
|
|
90
|
+
"https://procomponents.ant.design/",
|
|
91
|
+
"https://umijs.org/",
|
|
92
|
+
"https://github.com/umijs/dumi",
|
|
93
|
+
];
|
|
94
|
+
const id = (Math.random() * href.length).toFixed();
|
|
95
|
+
return href[id];
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const genMockData = (example: string) => {
|
|
100
|
+
if (!example) {
|
|
101
|
+
return {};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (typeof example === "string") {
|
|
105
|
+
return Mock.mock(example);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (Array.isArray(example)) {
|
|
109
|
+
return Mock.mock(example);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return Object.keys(example)
|
|
113
|
+
.map(name => {
|
|
114
|
+
return {
|
|
115
|
+
[name]: Mock.mock(example[name]),
|
|
116
|
+
};
|
|
117
|
+
})
|
|
118
|
+
.reduce((pre, next) => {
|
|
119
|
+
return {
|
|
120
|
+
...pre,
|
|
121
|
+
...next,
|
|
122
|
+
};
|
|
123
|
+
}, {});
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const genByTemp = ({
|
|
127
|
+
method,
|
|
128
|
+
path,
|
|
129
|
+
parameters,
|
|
130
|
+
status,
|
|
131
|
+
data,
|
|
132
|
+
mockConfig,
|
|
133
|
+
}: {
|
|
134
|
+
method: string;
|
|
135
|
+
path: string;
|
|
136
|
+
parameters: {
|
|
137
|
+
name: string;
|
|
138
|
+
in: string;
|
|
139
|
+
description: string;
|
|
140
|
+
required: boolean;
|
|
141
|
+
schema: { type: string };
|
|
142
|
+
example: string;
|
|
143
|
+
}[];
|
|
144
|
+
status: string;
|
|
145
|
+
data: string;
|
|
146
|
+
mockConfig: GenerateServiceProps["mockConfig"];
|
|
147
|
+
}) => {
|
|
148
|
+
if (!["get", "put", "post", "delete", "patch"].includes(method.toLocaleLowerCase())) {
|
|
149
|
+
return "";
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let securityPath = path;
|
|
153
|
+
parameters?.forEach(item => {
|
|
154
|
+
if (item.in === "path") {
|
|
155
|
+
securityPath = securityPath.replace(`{${item.name}}`, `:${item.name}`);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
if (mockConfig.msw) {
|
|
160
|
+
return `'${method.toUpperCase()} ${securityPath}': ${data}`;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return `'${method.toUpperCase()} ${securityPath}': (req: Request, res: Response) => {
|
|
164
|
+
res.status(${status}).send(${data});
|
|
165
|
+
}`;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const genMockFiles = (mockFunction: string[], mockConfig: GenerateServiceProps["mockConfig"]) => {
|
|
169
|
+
return prettierFile(`
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
${mockConfig.msw ? "" : "import { Request, Response } from 'express'"}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
export default {
|
|
175
|
+
${mockFunction.join("\n,")}
|
|
176
|
+
}`)[0];
|
|
177
|
+
};
|
|
178
|
+
export type genMockDataServerConfig = {
|
|
179
|
+
openAPI: any;
|
|
180
|
+
mockFolder: string;
|
|
181
|
+
mockConfig: GenerateServiceProps["mockConfig"];
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const mockGenerator = async ({ openAPI, mockFolder, mockConfig }: genMockDataServerConfig) => {
|
|
185
|
+
const openAPParse = new OpenAPIParserMock(openAPI);
|
|
186
|
+
const docs = openAPParse.parser();
|
|
187
|
+
const pathList = Object.keys(docs.paths);
|
|
188
|
+
const { paths } = docs;
|
|
189
|
+
const mockActionsObj = {};
|
|
190
|
+
pathList.forEach(path => {
|
|
191
|
+
const pathConfig = paths[path];
|
|
192
|
+
Object.keys(pathConfig).forEach(method => {
|
|
193
|
+
const methodConfig = pathConfig[method];
|
|
194
|
+
if (methodConfig) {
|
|
195
|
+
let conte = (
|
|
196
|
+
methodConfig.operationId ||
|
|
197
|
+
methodConfig?.tags?.join("/") ||
|
|
198
|
+
path.replace("/", "").split("/")[1]
|
|
199
|
+
)?.replace(/[^\w^\s^\u4e00-\u9fa5]/gi, "");
|
|
200
|
+
if (/[\u3220-\uFA29]/.test(conte)) {
|
|
201
|
+
conte = pinyin.convertToPinyin(conte, "", true);
|
|
202
|
+
}
|
|
203
|
+
if (!conte) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const data = genMockData(methodConfig.responses?.["200"]?.example);
|
|
207
|
+
if (!mockActionsObj[conte]) {
|
|
208
|
+
mockActionsObj[conte] = [];
|
|
209
|
+
}
|
|
210
|
+
const tempFile = genByTemp({
|
|
211
|
+
method,
|
|
212
|
+
path,
|
|
213
|
+
parameters: methodConfig.parameters,
|
|
214
|
+
status: "200",
|
|
215
|
+
data: JSON.stringify(data),
|
|
216
|
+
mockConfig,
|
|
217
|
+
});
|
|
218
|
+
if (tempFile) {
|
|
219
|
+
mockActionsObj[conte].push(tempFile);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
Object.keys(mockActionsObj).forEach(file => {
|
|
225
|
+
if (!file || file === "undefined") {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (file.includes("/")) {
|
|
229
|
+
const dirName = dirname(join(mockFolder, `${file}.ts`));
|
|
230
|
+
if (!fs.existsSync(dirName)) {
|
|
231
|
+
fs.mkdirSync(dirName);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
writeFile(mockFolder, `${file}.ts`, genMockFiles(mockActionsObj[file], mockConfig));
|
|
235
|
+
});
|
|
236
|
+
Log("✅ 生成 mock 文件成功");
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export { mockGenerator };
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/* eslint-disable no-continue */
|
|
2
|
+
/* eslint-disable guard-for-in */
|
|
3
|
+
/* eslint-disable no-restricted-syntax */
|
|
4
|
+
import memoizee from "memoizee";
|
|
5
|
+
|
|
6
|
+
import * as utils from "./utils";
|
|
7
|
+
import primitives from "./primitives";
|
|
8
|
+
|
|
9
|
+
const getDateByName = (name: string[] | string, parentsKey?: string[]) => {
|
|
10
|
+
if (!name || name.length < 1) {
|
|
11
|
+
return "string";
|
|
12
|
+
}
|
|
13
|
+
if (Array.isArray(name)) {
|
|
14
|
+
return getDateByName([...name].pop(), name);
|
|
15
|
+
}
|
|
16
|
+
if (["nickname", "name"].includes(name)) {
|
|
17
|
+
return "cname";
|
|
18
|
+
}
|
|
19
|
+
if (["owner", "firstName", "lastName", "username"].includes(name)) {
|
|
20
|
+
return "name";
|
|
21
|
+
}
|
|
22
|
+
if (["avatar"].includes(name)) {
|
|
23
|
+
return "avatar";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (["group"].includes(name)) {
|
|
27
|
+
return "group";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (name.toLocaleLowerCase().endsWith("id")) {
|
|
31
|
+
return "uuid";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (name.toLocaleLowerCase().endsWith("type") || name.toLocaleLowerCase().endsWith("key") || ["key"].includes(name)) {
|
|
35
|
+
return "id";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (name.toLocaleLowerCase().endsWith("label") || ["label"].includes(name)) {
|
|
39
|
+
const newParents = [...parentsKey];
|
|
40
|
+
newParents.pop();
|
|
41
|
+
const newType = getDateByName(newParents);
|
|
42
|
+
if (newType !== "string" && newType !== "csentence") {
|
|
43
|
+
return newType;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return "label";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (["email"].includes(name)) {
|
|
50
|
+
return "email";
|
|
51
|
+
}
|
|
52
|
+
if (["password"].includes(name)) {
|
|
53
|
+
return "string(16)";
|
|
54
|
+
}
|
|
55
|
+
if (["phone"].includes(name)) {
|
|
56
|
+
return "phone";
|
|
57
|
+
}
|
|
58
|
+
if (["province"].includes(name)) {
|
|
59
|
+
return "province";
|
|
60
|
+
}
|
|
61
|
+
if (["city"].includes(name)) {
|
|
62
|
+
return "city";
|
|
63
|
+
}
|
|
64
|
+
if (["addr", "address"].includes(name)) {
|
|
65
|
+
return "county";
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (["country"].includes(name)) {
|
|
69
|
+
return "country";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
["url", "imageUrl", "href"].includes(name) ||
|
|
74
|
+
name.toLocaleLowerCase().endsWith("url") ||
|
|
75
|
+
name.toLocaleLowerCase().endsWith("urls") ||
|
|
76
|
+
name.toLocaleLowerCase().endsWith("image") ||
|
|
77
|
+
name.toLocaleLowerCase().endsWith("link")
|
|
78
|
+
) {
|
|
79
|
+
return "href";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (name.toLocaleLowerCase().endsWith("errorcode")) {
|
|
83
|
+
return "errorCode";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (
|
|
87
|
+
["type", "status"].includes(name) ||
|
|
88
|
+
name.toLocaleLowerCase().endsWith("status") ||
|
|
89
|
+
name.toLocaleLowerCase().endsWith("type")
|
|
90
|
+
) {
|
|
91
|
+
return "status";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (name.toLocaleLowerCase().endsWith("authority")) {
|
|
95
|
+
return "authority";
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return "csentence";
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
function primitive(schemaParams, propsName) {
|
|
102
|
+
const schema = utils.objectify(schemaParams);
|
|
103
|
+
const { type, format } = schema;
|
|
104
|
+
const value = primitives[`${type}_${format || getDateByName(propsName)}`] || primitives[type];
|
|
105
|
+
|
|
106
|
+
if (typeof schema.example === "undefined") {
|
|
107
|
+
return value || `Unknown Type: ${schema.type}`;
|
|
108
|
+
}
|
|
109
|
+
return schema.example;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
class OpenAPIGeneratorMockJs {
|
|
113
|
+
protected openAPI: any;
|
|
114
|
+
constructor(openAPI) {
|
|
115
|
+
this.openAPI = openAPI;
|
|
116
|
+
this.sampleFromSchema = memoizee(this.sampleFromSchema);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
sampleFromSchema = (schema: any, propsName?: string[], schemaSet: Set<string> = new Set()) => {
|
|
120
|
+
const schemaRef = schema?.$ref;
|
|
121
|
+
|
|
122
|
+
if (schemaRef) {
|
|
123
|
+
// 如果之前已经使用过该引用结构,直接返回null,不然会陷入无限递归的情况
|
|
124
|
+
if (schemaSet.has(schemaRef)) {
|
|
125
|
+
return null;
|
|
126
|
+
} else {
|
|
127
|
+
schemaSet.add(schemaRef);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const localSchema = schemaRef
|
|
132
|
+
? utils.get(this.openAPI, schemaRef.replace("#/", "").split("/"))
|
|
133
|
+
: utils.objectify(schema);
|
|
134
|
+
|
|
135
|
+
let { type } = localSchema;
|
|
136
|
+
const { properties, additionalProperties, items, anyOf, oneOf, allOf } = localSchema;
|
|
137
|
+
|
|
138
|
+
if (allOf) {
|
|
139
|
+
let obj = {};
|
|
140
|
+
allOf.forEach(item => {
|
|
141
|
+
const newObj = this.sampleFromSchema(item, propsName, new Set(schemaSet));
|
|
142
|
+
obj = {
|
|
143
|
+
...obj,
|
|
144
|
+
...newObj,
|
|
145
|
+
};
|
|
146
|
+
});
|
|
147
|
+
return obj;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!type) {
|
|
151
|
+
if (properties) {
|
|
152
|
+
type = "object";
|
|
153
|
+
} else if (items) {
|
|
154
|
+
type = "array";
|
|
155
|
+
} else if (anyOf || oneOf) {
|
|
156
|
+
type = "union";
|
|
157
|
+
} else {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (type === "null") {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (type === "object") {
|
|
167
|
+
const props = utils.objectify(properties);
|
|
168
|
+
const obj: Record<string, any> = {};
|
|
169
|
+
for (const name in props) {
|
|
170
|
+
obj[name] = this.sampleFromSchema(props[name], [...(propsName || []), name], new Set(schemaSet));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (additionalProperties === true) {
|
|
174
|
+
obj.additionalProp1 = {};
|
|
175
|
+
return obj;
|
|
176
|
+
}
|
|
177
|
+
if (additionalProperties) {
|
|
178
|
+
const additionalProps = utils.objectify(additionalProperties);
|
|
179
|
+
const additionalPropVal = this.sampleFromSchema(additionalProps, propsName, new Set(schemaSet));
|
|
180
|
+
|
|
181
|
+
for (let i = 1; i < 4; i += 1) {
|
|
182
|
+
obj[`additionalProp${i}`] = additionalPropVal;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return obj;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (type === "array") {
|
|
189
|
+
const item = this.sampleFromSchema(items, propsName, new Set(schemaSet));
|
|
190
|
+
return new Array(parseInt((Math.random() * 20).toFixed(0), 10)).fill(item);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (type === "union") {
|
|
194
|
+
const subschemas = anyOf || oneOf;
|
|
195
|
+
const subschemas_length = (subschemas && subschemas.length) || 0;
|
|
196
|
+
if (subschemas_length) {
|
|
197
|
+
const index = utils.getRandomInt(0, subschemas_length);
|
|
198
|
+
const obj = this.sampleFromSchema(subschemas[index], propsName, new Set(schemaSet));
|
|
199
|
+
return obj;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (localSchema.enum) {
|
|
204
|
+
if (localSchema.default) return localSchema.default;
|
|
205
|
+
return utils.normalizeArray(localSchema.enum)[0];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (type === "file") {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
return primitive(localSchema, propsName);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
parser = () => {
|
|
215
|
+
const openAPI = {
|
|
216
|
+
...this.openAPI,
|
|
217
|
+
};
|
|
218
|
+
for (const path in openAPI.paths) {
|
|
219
|
+
for (const method in openAPI.paths[path]) {
|
|
220
|
+
const api = openAPI.paths[path][method];
|
|
221
|
+
for (const code in api.responses) {
|
|
222
|
+
const response = api.responses[code];
|
|
223
|
+
|
|
224
|
+
const keys = Object.keys(response.content || {});
|
|
225
|
+
if (keys.length) {
|
|
226
|
+
let key: string;
|
|
227
|
+
|
|
228
|
+
if (keys.includes("application/json")) {
|
|
229
|
+
key = "application/json";
|
|
230
|
+
} else if (keys.includes("*/*")) {
|
|
231
|
+
key = "*/*";
|
|
232
|
+
} else {
|
|
233
|
+
key = keys[0];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const schema = utils.inferSchema(response.content[key]);
|
|
237
|
+
|
|
238
|
+
if (schema) {
|
|
239
|
+
response.example = schema ? this.sampleFromSchema(schema) : null;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (!api.parameters) continue;
|
|
244
|
+
for (const parameter of api.parameters) {
|
|
245
|
+
const schema = utils.inferSchema(parameter);
|
|
246
|
+
parameter.example = schema ? this.sampleFromSchema(schema) : null;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return openAPI;
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export default OpenAPIGeneratorMockJs;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
string: string;
|
|
3
|
+
string_email: string;
|
|
4
|
+
string_phone: string;
|
|
5
|
+
string_username: string;
|
|
6
|
+
string_url: string;
|
|
7
|
+
string_uuid: string;
|
|
8
|
+
string_id: string;
|
|
9
|
+
string_status: string;
|
|
10
|
+
string_authority: string;
|
|
11
|
+
string_label: string;
|
|
12
|
+
string_group: string;
|
|
13
|
+
string_csentence: string;
|
|
14
|
+
string_cname: string;
|
|
15
|
+
string_name: string;
|
|
16
|
+
string_errorCode: string;
|
|
17
|
+
string_country: string;
|
|
18
|
+
string_county: string;
|
|
19
|
+
string_province: string;
|
|
20
|
+
string_city: string;
|
|
21
|
+
string_avatar: string;
|
|
22
|
+
string_href: string;
|
|
23
|
+
"string_string(16)": string;
|
|
24
|
+
"string_date-time": string;
|
|
25
|
+
string_date: string;
|
|
26
|
+
number: string;
|
|
27
|
+
number_float: string;
|
|
28
|
+
integer: string;
|
|
29
|
+
boolean: string;
|
|
30
|
+
};
|
|
31
|
+
export default _default;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
string: "@string",
|
|
3
|
+
string_email: "@email",
|
|
4
|
+
string_phone: "@phone",
|
|
5
|
+
string_username: "@cname",
|
|
6
|
+
string_url: "@url",
|
|
7
|
+
string_uuid: "@guid",
|
|
8
|
+
string_id: "@increment",
|
|
9
|
+
string_status: "@status",
|
|
10
|
+
string_authority: "@authority",
|
|
11
|
+
string_label: "@label",
|
|
12
|
+
string_group: "@group",
|
|
13
|
+
string_csentence: "@csentence",
|
|
14
|
+
string_cname: "@cname",
|
|
15
|
+
string_name: "@last",
|
|
16
|
+
string_errorCode: "@increment",
|
|
17
|
+
string_country: "@country",
|
|
18
|
+
string_county: "@county(true)",
|
|
19
|
+
string_province: "@province",
|
|
20
|
+
string_city: "@city",
|
|
21
|
+
string_avatar: "@avatar",
|
|
22
|
+
string_href: "@href",
|
|
23
|
+
"string_string(16)": "@string(16)",
|
|
24
|
+
"string_date-time": "@datetime",
|
|
25
|
+
string_date: "@date",
|
|
26
|
+
number: "@integer(60, 100)",
|
|
27
|
+
number_float: "@float(60, 100, 3, 5)",
|
|
28
|
+
integer: "@integer(60, 100)",
|
|
29
|
+
boolean: "@boolean",
|
|
30
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
declare function isObject(obj: any): boolean;
|
|
2
|
+
declare function objectify(thing: any): any;
|
|
3
|
+
declare function get(entity: any, path: (string | number)[]): any;
|
|
4
|
+
declare function normalizeArray(arr: any): any[];
|
|
5
|
+
declare function isFunc(thing: any): boolean;
|
|
6
|
+
declare function inferSchema(thing: any): any;
|
|
7
|
+
declare function getRandomInt(min: any, max: any): number;
|
|
8
|
+
export { isObject, get, objectify, isFunc, inferSchema, normalizeArray, getRandomInt };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
function isObject(obj) {
|
|
2
|
+
return !!obj && typeof obj === "object";
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function objectify(thing) {
|
|
6
|
+
if (!isObject(thing)) return {};
|
|
7
|
+
return thing;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function get(entity: any, path: (string | number)[]) {
|
|
11
|
+
let current = entity;
|
|
12
|
+
|
|
13
|
+
for (let i = 0; i < path.length; i += 1) {
|
|
14
|
+
if (current === null || current === undefined) {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
current = current[path[i]];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return current;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function normalizeArray(arr) {
|
|
25
|
+
if (Array.isArray(arr)) return arr;
|
|
26
|
+
return [arr];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isFunc(thing) {
|
|
30
|
+
return typeof thing === "function";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function inferSchema(thing) {
|
|
34
|
+
if (thing.schema) {
|
|
35
|
+
return thing.schema;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (thing.properties) {
|
|
39
|
+
return {
|
|
40
|
+
...thing,
|
|
41
|
+
type: "object",
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return thing;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function getRandomInt(min, max) {
|
|
49
|
+
const minCeiled = Math.ceil(min);
|
|
50
|
+
const maxFloored = Math.floor(max);
|
|
51
|
+
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { isObject, get, objectify, isFunc, inferSchema, normalizeArray, getRandomInt };
|