@dazhicheng/openapi 1.1.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.
@@ -1,254 +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;
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,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 };