openapi-ts-request 0.1.0
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/LICENSE +21 -0
- package/README.md +147 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +31 -0
- package/dist/generator/config.d.ts +29 -0
- package/dist/generator/config.js +57 -0
- package/dist/generator/file.d.ts +2 -0
- package/dist/generator/file.js +36 -0
- package/dist/generator/mockGenarator.d.ts +6 -0
- package/dist/generator/mockGenarator.js +202 -0
- package/dist/generator/serviceGenarator.d.ts +31 -0
- package/dist/generator/serviceGenarator.js +640 -0
- package/dist/generator/type.d.ts +34 -0
- package/dist/generator/type.js +2 -0
- package/dist/generator/util.d.ts +18 -0
- package/dist/generator/util.js +314 -0
- package/dist/index.d.ts +125 -0
- package/dist/index.js +32 -0
- package/dist/log.d.ts +2 -0
- package/dist/log.js +6 -0
- package/dist/parser-mock/index.d.ts +19 -0
- package/dist/parser-mock/index.js +223 -0
- package/dist/parser-mock/primitives.d.ts +1 -0
- package/dist/parser-mock/primitives.js +33 -0
- package/dist/parser-mock/util.d.ts +90 -0
- package/dist/parser-mock/util.js +46 -0
- package/dist/type.d.ts +72 -0
- package/dist/type.js +30 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +77 -0
- package/package.json +81 -0
- package/prettier.config.cjs +9 -0
- package/templates/displayEnumLabel.njk +10 -0
- package/templates/interface.njk +54 -0
- package/templates/serviceController.njk +168 -0
- package/templates/serviceIndex.njk +13 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ArraySchemaObject, BinaryArraySchemaObject, ComponentsObject, ISchemaObject, NonArraySchemaObject, OperationObject, ReferenceObject, SchemaObject } from '../type';
|
|
2
|
+
import { ITypeItem } from './type';
|
|
3
|
+
export declare function stripDot(str: string): string;
|
|
4
|
+
export declare function resolveTypeName(typeName: string): string;
|
|
5
|
+
export declare function getDefaultType(schemaObject?: ISchemaObject | string, namespace?: string, schemas?: ComponentsObject['schemas']): string;
|
|
6
|
+
export declare function getDefaultFileTag(operationObject: OperationObject, apiPath: string): string[];
|
|
7
|
+
export declare function handleDuplicateTypeNames(interfaceTPConfigs: ITypeItem[]): void;
|
|
8
|
+
export declare function getBasePrefix(paths: string[]): string;
|
|
9
|
+
export declare function genDefaultFunctionName(path: string, pathBasePrefix: string): string;
|
|
10
|
+
export declare function getFinalFileName(s: string): string;
|
|
11
|
+
export declare function replaceDot(s: string): string;
|
|
12
|
+
export declare function resolveFunctionName(functionName: string, methodName: string): string;
|
|
13
|
+
export declare function markAllowSchema(schemaStr: string, schemas: ComponentsObject['schemas']): void;
|
|
14
|
+
export declare function isReferenceObject(schema: unknown): schema is ReferenceObject;
|
|
15
|
+
export declare function isSchemaObject(schema: unknown): schema is SchemaObject;
|
|
16
|
+
export declare function isNonArraySchemaObject(schema: unknown): schema is NonArraySchemaObject;
|
|
17
|
+
export declare function isArraySchemaObject(schema: unknown): schema is ArraySchemaObject;
|
|
18
|
+
export declare function isBinaryArraySchemaObject(schema: unknown): schema is BinaryArraySchemaObject;
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBinaryArraySchemaObject = exports.isArraySchemaObject = exports.isNonArraySchemaObject = exports.isSchemaObject = exports.isReferenceObject = exports.markAllowSchema = exports.resolveFunctionName = exports.replaceDot = exports.getFinalFileName = exports.genDefaultFunctionName = exports.getBasePrefix = exports.handleDuplicateTypeNames = exports.getDefaultFileTag = exports.getDefaultType = exports.resolveTypeName = exports.stripDot = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
6
|
+
const reserved_words_1 = tslib_1.__importDefault(require("reserved-words"));
|
|
7
|
+
const tiny_pinyin_1 = tslib_1.__importDefault(require("tiny-pinyin"));
|
|
8
|
+
const log_1 = tslib_1.__importDefault(require("../log"));
|
|
9
|
+
const config_1 = require("./config");
|
|
10
|
+
function stripDot(str) {
|
|
11
|
+
return str.replace(/[-_ .](\w)/g, (_all, letter) => letter.toUpperCase());
|
|
12
|
+
}
|
|
13
|
+
exports.stripDot = stripDot;
|
|
14
|
+
// 兼容C#泛型的typeLastName取法
|
|
15
|
+
function getTypeLastName(typeName) {
|
|
16
|
+
var _a, _b, _c, _d, _e;
|
|
17
|
+
const tempTypeName = typeName || '';
|
|
18
|
+
const childrenTypeName = (_a = tempTypeName === null || tempTypeName === void 0 ? void 0 : tempTypeName.match(/\[\[.+\]\]/g)) === null || _a === void 0 ? void 0 : _a[0];
|
|
19
|
+
if (!childrenTypeName) {
|
|
20
|
+
const publicKeyToken = ((_c = (_b = tempTypeName.split('PublicKeyToken=')) === null || _b === void 0 ? void 0 : _b[1]) !== null && _c !== void 0 ? _c : '').replace('null', '');
|
|
21
|
+
const firstTempTypeName = (_e = (_d = tempTypeName.split(',')) === null || _d === void 0 ? void 0 : _d[0]) !== null && _e !== void 0 ? _e : tempTypeName;
|
|
22
|
+
let typeLastName = firstTempTypeName.split('/').pop().split('.').pop();
|
|
23
|
+
if (typeLastName.endsWith('[]')) {
|
|
24
|
+
typeLastName =
|
|
25
|
+
typeLastName.substring(0, typeLastName.length - 2) + 'Array';
|
|
26
|
+
}
|
|
27
|
+
// 特殊处理C#默认系统类型,不追加publicKeyToken
|
|
28
|
+
const isCsharpSystemType = firstTempTypeName.startsWith('System.');
|
|
29
|
+
if (!publicKeyToken || isCsharpSystemType) {
|
|
30
|
+
return typeLastName;
|
|
31
|
+
}
|
|
32
|
+
return `${typeLastName}_${publicKeyToken}`;
|
|
33
|
+
}
|
|
34
|
+
const currentTypeName = getTypeLastName(tempTypeName.replace(childrenTypeName, ''));
|
|
35
|
+
const childrenTypeNameLastName = getTypeLastName(childrenTypeName.substring(2, childrenTypeName.length - 2));
|
|
36
|
+
return `${currentTypeName}_${childrenTypeNameLastName}`;
|
|
37
|
+
}
|
|
38
|
+
// 类型声明过滤关键字
|
|
39
|
+
function resolveTypeName(typeName) {
|
|
40
|
+
if (reserved_words_1.default.check(typeName)) {
|
|
41
|
+
return `__openAPI__${typeName}`;
|
|
42
|
+
}
|
|
43
|
+
const typeLastName = getTypeLastName(typeName);
|
|
44
|
+
const name = typeLastName
|
|
45
|
+
.replace(/[-_ ](\w)/g, (_all, letter) => letter.toUpperCase())
|
|
46
|
+
.replace(/[^\w^\s^\u4e00-\u9fa5]/gi, '');
|
|
47
|
+
// 当model名称是number开头的时候,ts会报错。这种场景一般发生在后端定义的名称是中文
|
|
48
|
+
if (name === '_' || /^\d+$/.test(name)) {
|
|
49
|
+
(0, log_1.default)('⚠️ models不能以number开头,原因可能是Model定义名称为中文, 建议联系后台修改');
|
|
50
|
+
return `Pinyin_${name}`;
|
|
51
|
+
}
|
|
52
|
+
if (!/[\u3220-\uFA29]/.test(name) && !/^\d$/.test(name)) {
|
|
53
|
+
return name;
|
|
54
|
+
}
|
|
55
|
+
const noBlankName = name.replace(/ +/g, '');
|
|
56
|
+
return tiny_pinyin_1.default.convertToPinyin(noBlankName, '', true);
|
|
57
|
+
}
|
|
58
|
+
exports.resolveTypeName = resolveTypeName;
|
|
59
|
+
function getRefName(refObject) {
|
|
60
|
+
if (!isReferenceObject(refObject)) {
|
|
61
|
+
return refObject;
|
|
62
|
+
}
|
|
63
|
+
const refPaths = refObject.$ref.split('/');
|
|
64
|
+
return resolveTypeName(refPaths[refPaths.length - 1]);
|
|
65
|
+
}
|
|
66
|
+
function getDefaultType(schemaObject, namespace = '', schemas) {
|
|
67
|
+
var _a, _b;
|
|
68
|
+
if ((0, lodash_1.isUndefined)(schemaObject) || (0, lodash_1.isNull)(schemaObject)) {
|
|
69
|
+
return 'any';
|
|
70
|
+
}
|
|
71
|
+
if (!(0, lodash_1.isObject)(schemaObject)) {
|
|
72
|
+
return schemaObject;
|
|
73
|
+
}
|
|
74
|
+
if (isReferenceObject(schemaObject)) {
|
|
75
|
+
return [namespace, getRefName(schemaObject)].filter((s) => s).join('.');
|
|
76
|
+
}
|
|
77
|
+
let type = schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject.type;
|
|
78
|
+
const dateEnum = ['Date', 'date', 'dateTime', 'date-time', 'datetime'];
|
|
79
|
+
const stringEnum = ['string', 'email', 'password', 'url', 'byte', 'binary'];
|
|
80
|
+
if (type === 'null') {
|
|
81
|
+
return 'null';
|
|
82
|
+
}
|
|
83
|
+
if (config_1.numberEnum.includes(schemaObject.format)) {
|
|
84
|
+
type = 'number';
|
|
85
|
+
}
|
|
86
|
+
if (schemaObject.enum) {
|
|
87
|
+
type = 'enum';
|
|
88
|
+
}
|
|
89
|
+
if (config_1.numberEnum.includes(type)) {
|
|
90
|
+
return 'number';
|
|
91
|
+
}
|
|
92
|
+
if (dateEnum.includes(type)) {
|
|
93
|
+
return 'Date';
|
|
94
|
+
}
|
|
95
|
+
if (stringEnum.includes(type)) {
|
|
96
|
+
return 'string';
|
|
97
|
+
}
|
|
98
|
+
if (type === 'boolean') {
|
|
99
|
+
return 'boolean';
|
|
100
|
+
}
|
|
101
|
+
if (type === 'array') {
|
|
102
|
+
let items = schemaObject.items;
|
|
103
|
+
if ('schema' in schemaObject) {
|
|
104
|
+
items = schemaObject.schema.items;
|
|
105
|
+
}
|
|
106
|
+
if (Array.isArray(items)) {
|
|
107
|
+
const arrayItemType = items
|
|
108
|
+
.map((subType) => getDefaultType((subType.schema || subType), namespace))
|
|
109
|
+
.toString();
|
|
110
|
+
return `[${arrayItemType}]`;
|
|
111
|
+
}
|
|
112
|
+
const arrayType = getDefaultType(items, namespace);
|
|
113
|
+
return arrayType.includes(' | ') ? `(${arrayType})[]` : `${arrayType}[]`;
|
|
114
|
+
}
|
|
115
|
+
if (type === 'enum') {
|
|
116
|
+
return Array.isArray(schemaObject.enum)
|
|
117
|
+
? Array.from(new Set(schemaObject.enum.map((v) => typeof v === 'string'
|
|
118
|
+
? `"${v.replace(/"/g, '"')}"`
|
|
119
|
+
: getDefaultType(v)))).join(' | ')
|
|
120
|
+
: 'string';
|
|
121
|
+
}
|
|
122
|
+
if (schemaObject.oneOf && schemaObject.oneOf.length) {
|
|
123
|
+
return schemaObject.oneOf
|
|
124
|
+
.map((item) => getDefaultType(item, namespace))
|
|
125
|
+
.join(' | ');
|
|
126
|
+
}
|
|
127
|
+
if ((_a = schemaObject.anyOf) === null || _a === void 0 ? void 0 : _a.length) {
|
|
128
|
+
return schemaObject.anyOf
|
|
129
|
+
.map((item) => getDefaultType(item, namespace))
|
|
130
|
+
.join(' | ');
|
|
131
|
+
}
|
|
132
|
+
if ((_b = schemaObject.allOf) === null || _b === void 0 ? void 0 : _b.length) {
|
|
133
|
+
const allofList = schemaObject.allOf.map((item) => {
|
|
134
|
+
var _a;
|
|
135
|
+
if (isReferenceObject(item)) {
|
|
136
|
+
// 不使用 getRefName 函数处理,无法通过 schemas[schemaKey] 获取到schema
|
|
137
|
+
const refPaths = item.$ref.split('/');
|
|
138
|
+
const schemaKey = refPaths[refPaths.length - 1];
|
|
139
|
+
if ((_a = schemas === null || schemas === void 0 ? void 0 : schemas[schemaKey]) === null || _a === void 0 ? void 0 : _a.enum) {
|
|
140
|
+
return `I${getDefaultType(item, namespace)}`;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return getDefaultType(item, namespace);
|
|
144
|
+
});
|
|
145
|
+
return `(${allofList.join(' & ')})`;
|
|
146
|
+
}
|
|
147
|
+
if (schemaObject.type === 'object' || schemaObject.properties) {
|
|
148
|
+
if (!(0, lodash_1.keys)(schemaObject.properties).length) {
|
|
149
|
+
return 'Record<string, any>';
|
|
150
|
+
}
|
|
151
|
+
return `{ ${(0, lodash_1.keys)(schemaObject.properties)
|
|
152
|
+
.map((key) => {
|
|
153
|
+
var _a, _b;
|
|
154
|
+
let required = false;
|
|
155
|
+
if ((0, lodash_1.isBoolean)(schemaObject.required) && schemaObject.required) {
|
|
156
|
+
required = true;
|
|
157
|
+
}
|
|
158
|
+
if ((0, lodash_1.isArray)(schemaObject.required) &&
|
|
159
|
+
schemaObject.required.includes(key)) {
|
|
160
|
+
required = true;
|
|
161
|
+
}
|
|
162
|
+
if ('required' in (schemaObject.properties[key] || {}) &&
|
|
163
|
+
((_a = schemaObject.properties[key]) === null || _a === void 0 ? void 0 : _a.required)) {
|
|
164
|
+
required = true;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* 将类型属性变为字符串,兼容错误格式如:
|
|
168
|
+
* 3d_tile(数字开头)等错误命名,
|
|
169
|
+
* 在后面进行格式化的时候会将正确的字符串转换为正常形式,
|
|
170
|
+
* 错误的继续保留字符串。
|
|
171
|
+
* */
|
|
172
|
+
return `'${key}'${required ? '' : '?'}: ${getDefaultType((_b = schemaObject.properties) === null || _b === void 0 ? void 0 : _b[key], namespace)}; `;
|
|
173
|
+
})
|
|
174
|
+
.join('')}}`;
|
|
175
|
+
}
|
|
176
|
+
return 'any';
|
|
177
|
+
}
|
|
178
|
+
exports.getDefaultType = getDefaultType;
|
|
179
|
+
function getDefaultFileTag(operationObject, apiPath) {
|
|
180
|
+
return operationObject['x-swagger-router-controller']
|
|
181
|
+
? [operationObject['x-swagger-router-controller']]
|
|
182
|
+
: operationObject.tags || [operationObject.operationId] || [
|
|
183
|
+
apiPath.replace('/', '').split('/')[1],
|
|
184
|
+
];
|
|
185
|
+
}
|
|
186
|
+
exports.getDefaultFileTag = getDefaultFileTag;
|
|
187
|
+
function findDuplicateTypeNames(arr) {
|
|
188
|
+
const counts = (0, lodash_1.countBy)(arr);
|
|
189
|
+
const duplicates = (0, lodash_1.filter)((0, lodash_1.keys)(counts), (key) => counts[key] > 1);
|
|
190
|
+
return duplicates;
|
|
191
|
+
}
|
|
192
|
+
function handleDuplicateTypeNames(interfaceTPConfigs) {
|
|
193
|
+
const duplicateTypeNames = findDuplicateTypeNames((0, lodash_1.map)(interfaceTPConfigs, (item) => item.typeName));
|
|
194
|
+
if (!(0, lodash_1.isEmpty)(duplicateTypeNames)) {
|
|
195
|
+
(0, lodash_1.forEach)(duplicateTypeNames, (typeName) => {
|
|
196
|
+
const selectInterfaceTPConfigs = (0, lodash_1.filter)(interfaceTPConfigs, (interfaceTP) => interfaceTP.typeName === typeName);
|
|
197
|
+
(0, lodash_1.forEach)(selectInterfaceTPConfigs, (interfaceTP, index) => {
|
|
198
|
+
if (index >= 1) {
|
|
199
|
+
interfaceTP.typeName = `${interfaceTP.typeName}${index + 1}`;
|
|
200
|
+
if (interfaceTP.displayLabelFuncName) {
|
|
201
|
+
interfaceTP.displayLabelFuncName = `${interfaceTP.displayLabelFuncName}${index + 1}`;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.handleDuplicateTypeNames = handleDuplicateTypeNames;
|
|
209
|
+
// 检测所有path重复区域(prefix)
|
|
210
|
+
function getBasePrefix(paths) {
|
|
211
|
+
const arr = [];
|
|
212
|
+
paths
|
|
213
|
+
.map((item) => item.split('/'))
|
|
214
|
+
.forEach((pathItem) => {
|
|
215
|
+
pathItem.forEach((item, key) => {
|
|
216
|
+
if (arr.length <= key) {
|
|
217
|
+
arr[key] = [];
|
|
218
|
+
}
|
|
219
|
+
arr[key].push(item);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
const res = [];
|
|
223
|
+
arr
|
|
224
|
+
.map((item) => Array.from(new Set(item)))
|
|
225
|
+
.every((item) => {
|
|
226
|
+
const b = item.length === 1;
|
|
227
|
+
if (b) {
|
|
228
|
+
res.push(item);
|
|
229
|
+
}
|
|
230
|
+
return b;
|
|
231
|
+
});
|
|
232
|
+
return `${res.join('/')}/`;
|
|
233
|
+
}
|
|
234
|
+
exports.getBasePrefix = getBasePrefix;
|
|
235
|
+
// 将地址path路径转为大驼峰
|
|
236
|
+
function genDefaultFunctionName(path, pathBasePrefix) {
|
|
237
|
+
// 首字母转大写
|
|
238
|
+
function toUpperFirstLetter(text) {
|
|
239
|
+
return text.charAt(0).toUpperCase() + text.slice(1);
|
|
240
|
+
}
|
|
241
|
+
return path === null || path === void 0 ? void 0 : path.replace(pathBasePrefix, '').split('/').map((str) => {
|
|
242
|
+
/**
|
|
243
|
+
* 兼容错误命名如 /user/:id/:name
|
|
244
|
+
* 因为是typeName,所以直接进行转换
|
|
245
|
+
* */
|
|
246
|
+
let s = resolveTypeName(str);
|
|
247
|
+
if (s.includes('-')) {
|
|
248
|
+
s = s.replace(/(-\w)+/g, (_match, p1) => p1 === null || p1 === void 0 ? void 0 : p1.slice(1).toUpperCase());
|
|
249
|
+
}
|
|
250
|
+
if (s.match(/^{.+}$/gim)) {
|
|
251
|
+
return `By${toUpperFirstLetter(s.slice(1, s.length - 1))}`;
|
|
252
|
+
}
|
|
253
|
+
return toUpperFirstLetter(s);
|
|
254
|
+
}).join('');
|
|
255
|
+
}
|
|
256
|
+
exports.genDefaultFunctionName = genDefaultFunctionName;
|
|
257
|
+
function getFinalFileName(s) {
|
|
258
|
+
// 支持下划线、中划线和空格分隔符,注意分隔符枚举值的顺序不能改变,否则正则匹配会报错
|
|
259
|
+
return s.replace(/[-_ ](\w)/g, (_all, letter) => letter.toUpperCase());
|
|
260
|
+
}
|
|
261
|
+
exports.getFinalFileName = getFinalFileName;
|
|
262
|
+
function replaceDot(s) {
|
|
263
|
+
return s
|
|
264
|
+
.replace(/\./g, '_')
|
|
265
|
+
.replace(/[-_ ](\w)/g, (_all, letter) => letter.toUpperCase());
|
|
266
|
+
}
|
|
267
|
+
exports.replaceDot = replaceDot;
|
|
268
|
+
function resolveFunctionName(functionName, methodName) {
|
|
269
|
+
// 类型声明过滤关键字
|
|
270
|
+
if (reserved_words_1.default.check(functionName)) {
|
|
271
|
+
return `${functionName}Using${methodName.toUpperCase()}`;
|
|
272
|
+
}
|
|
273
|
+
return functionName;
|
|
274
|
+
}
|
|
275
|
+
exports.resolveFunctionName = resolveFunctionName;
|
|
276
|
+
// 标记引用的 $ref 对应的schema
|
|
277
|
+
function markAllowSchema(schemaStr, schemas) {
|
|
278
|
+
const refs = schemaStr.match(/#\/components\/schemas\/([A-Za-z0-9._-]+)/g);
|
|
279
|
+
(0, lodash_1.forEach)(refs, (ref) => {
|
|
280
|
+
const refPaths = ref.split('/');
|
|
281
|
+
const schema = schemas === null || schemas === void 0 ? void 0 : schemas[refPaths[refPaths.length - 1]];
|
|
282
|
+
if (schema) {
|
|
283
|
+
schema.isAllowed = true;
|
|
284
|
+
}
|
|
285
|
+
markAllowSchema(JSON.stringify(schema), schemas);
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
exports.markAllowSchema = markAllowSchema;
|
|
289
|
+
function isReferenceObject(schema) {
|
|
290
|
+
return (schema === null || schema === void 0 ? void 0 : schema.$ref) !== undefined;
|
|
291
|
+
}
|
|
292
|
+
exports.isReferenceObject = isReferenceObject;
|
|
293
|
+
function isSchemaObject(schema) {
|
|
294
|
+
return (schema === null || schema === void 0 ? void 0 : schema.properties) !== undefined;
|
|
295
|
+
}
|
|
296
|
+
exports.isSchemaObject = isSchemaObject;
|
|
297
|
+
function isNonArraySchemaObject(schema) {
|
|
298
|
+
return ((schema === null || schema === void 0 ? void 0 : schema.type) === 'object' &&
|
|
299
|
+
(schema === null || schema === void 0 ? void 0 : schema.properties) !== undefined);
|
|
300
|
+
}
|
|
301
|
+
exports.isNonArraySchemaObject = isNonArraySchemaObject;
|
|
302
|
+
function isArraySchemaObject(schema) {
|
|
303
|
+
return (((schema === null || schema === void 0 ? void 0 : schema.type) === 'array' ||
|
|
304
|
+
(schema === null || schema === void 0 ? void 0 : schema.type) === 'stringArray') &&
|
|
305
|
+
(schema === null || schema === void 0 ? void 0 : schema.items) !== undefined);
|
|
306
|
+
}
|
|
307
|
+
exports.isArraySchemaObject = isArraySchemaObject;
|
|
308
|
+
function isBinaryArraySchemaObject(schema) {
|
|
309
|
+
var _a, _b;
|
|
310
|
+
return (isArraySchemaObject(schema) &&
|
|
311
|
+
(((_a = schema.items) === null || _a === void 0 ? void 0 : _a.format) === 'binary' ||
|
|
312
|
+
((_b = schema.items) === null || _b === void 0 ? void 0 : _b.format) === 'base64'));
|
|
313
|
+
}
|
|
314
|
+
exports.isBinaryArraySchemaObject = isBinaryArraySchemaObject;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { APIDataType } from './generator/type';
|
|
2
|
+
import { ComponentsObject, OpenAPIObject, OperationObject, ReferenceObject, SchemaObject } from './type';
|
|
3
|
+
export type GenerateServiceProps = {
|
|
4
|
+
/**
|
|
5
|
+
* Swagger 2.0 或 OpenAPI 3.0 的地址
|
|
6
|
+
*/
|
|
7
|
+
schemaPath: string;
|
|
8
|
+
/**
|
|
9
|
+
* 生成的文件夹的路径
|
|
10
|
+
*/
|
|
11
|
+
serversPath?: string;
|
|
12
|
+
/**
|
|
13
|
+
* 自定义请求方法路径
|
|
14
|
+
* 默认: import request from 'axios';
|
|
15
|
+
* 方式一: '@/request'
|
|
16
|
+
* 方式二: import request from '@/request';
|
|
17
|
+
*/
|
|
18
|
+
requestLibPath?: string;
|
|
19
|
+
/**
|
|
20
|
+
* 只解析归属于 tags 集合的api 和 schema
|
|
21
|
+
*/
|
|
22
|
+
allowedTags?: string[];
|
|
23
|
+
/**
|
|
24
|
+
* 自定义请求方法 options 参数类型
|
|
25
|
+
*/
|
|
26
|
+
requestOptionsType?: string;
|
|
27
|
+
/**
|
|
28
|
+
* 自定义请求方法表达式
|
|
29
|
+
* 例如: const request = require('node-fetch');
|
|
30
|
+
*/
|
|
31
|
+
requestImportStatement?: string;
|
|
32
|
+
/**
|
|
33
|
+
* api 的前缀
|
|
34
|
+
*/
|
|
35
|
+
apiPrefix?: string | ((params: {
|
|
36
|
+
path: string;
|
|
37
|
+
method: string;
|
|
38
|
+
namespace: string;
|
|
39
|
+
functionName: string;
|
|
40
|
+
}) => string);
|
|
41
|
+
/**
|
|
42
|
+
* response中数据字段
|
|
43
|
+
* example: ['result', 'res']
|
|
44
|
+
*/
|
|
45
|
+
dataFields?: string[];
|
|
46
|
+
/**
|
|
47
|
+
* mock目录
|
|
48
|
+
*/
|
|
49
|
+
mockFolder?: string;
|
|
50
|
+
/**
|
|
51
|
+
* 默认为false,true时使用null代替可选值
|
|
52
|
+
*/
|
|
53
|
+
nullable?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* 模板文件、请求函数采用小驼峰命名
|
|
56
|
+
*/
|
|
57
|
+
isCamelCase?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* 命名空间名称,默认为API,不需要关注
|
|
60
|
+
*/
|
|
61
|
+
namespace?: string;
|
|
62
|
+
/**
|
|
63
|
+
* 模板文件的文件路径,不需要关注
|
|
64
|
+
*/
|
|
65
|
+
templatesFolder?: string;
|
|
66
|
+
/**
|
|
67
|
+
* 自定义 hook
|
|
68
|
+
*/
|
|
69
|
+
hook?: {
|
|
70
|
+
/** change open api data after constructor */
|
|
71
|
+
afterOpenApiDataInited?: (openAPIData: OpenAPIObject) => OpenAPIObject;
|
|
72
|
+
/** 自定义函数名称 */
|
|
73
|
+
customFunctionName?: (data: APIDataType) => string;
|
|
74
|
+
/** 自定义类型名称 */
|
|
75
|
+
customTypeName?: (data: APIDataType) => string;
|
|
76
|
+
/** 自定义 options 默认值 */
|
|
77
|
+
customOptionsDefaultValue?: (data: OperationObject) => Record<string, any> | undefined;
|
|
78
|
+
/** 自定义类名 */
|
|
79
|
+
customClassName?: (tagName: string) => string;
|
|
80
|
+
/**
|
|
81
|
+
* 自定义获取type hook
|
|
82
|
+
* 返回非字符串将使用默认方法获取type
|
|
83
|
+
* @example set number to string
|
|
84
|
+
* function customType(schemaObject,namespace){
|
|
85
|
+
* if(schemaObject.type==='number' && !schemaObject.format){
|
|
86
|
+
* return 'BigDecimalString';
|
|
87
|
+
* }
|
|
88
|
+
* }
|
|
89
|
+
*/
|
|
90
|
+
customType?: ({ schemaObject, namespace, schemas, originGetType, }: {
|
|
91
|
+
schemaObject: SchemaObject | ReferenceObject;
|
|
92
|
+
namespace: string;
|
|
93
|
+
schemas?: ComponentsObject['schemas'];
|
|
94
|
+
originGetType: (schemaObject: SchemaObject, namespace: string) => string;
|
|
95
|
+
}) => string;
|
|
96
|
+
/**
|
|
97
|
+
* 自定义生成文件名,可返回多个,表示生成多个文件
|
|
98
|
+
* 返回为空,则使用默认的获取方法获取
|
|
99
|
+
* @example 使用operationId生成文件名
|
|
100
|
+
* function customFileNames(operationObject,apiPath){
|
|
101
|
+
* const operationId=operationObject.operationId;
|
|
102
|
+
* if (!operationId) {
|
|
103
|
+
* console.warn('[Warning] no operationId', apiPath);
|
|
104
|
+
* return;
|
|
105
|
+
* }
|
|
106
|
+
* const res = operationId.split('_');
|
|
107
|
+
* if (res.length > 1) {
|
|
108
|
+
* res.shift();
|
|
109
|
+
* if (res.length > 2) {
|
|
110
|
+
* console.warn('[Warning] operationId has more than 2 part', apiPath);
|
|
111
|
+
* }
|
|
112
|
+
* return [res.join('_')];
|
|
113
|
+
* } else {
|
|
114
|
+
* const controllerName = (res || [])[0];
|
|
115
|
+
* if (controllerName) {
|
|
116
|
+
* return [controllerName];
|
|
117
|
+
* }
|
|
118
|
+
* return;
|
|
119
|
+
* }
|
|
120
|
+
* }
|
|
121
|
+
*/
|
|
122
|
+
customFileNames?: (operationObject: OperationObject, apiPath: string, apiMethod: string) => string[];
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
export declare function generateService({ requestLibPath, schemaPath, mockFolder, allowedTags, ...rest }: GenerateServiceProps): Promise<void>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateService = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
6
|
+
const mockGenarator_1 = require("./generator/mockGenarator");
|
|
7
|
+
const serviceGenarator_1 = tslib_1.__importDefault(require("./generator/serviceGenarator"));
|
|
8
|
+
const util_1 = require("./util");
|
|
9
|
+
function generateService(_a) {
|
|
10
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
11
|
+
var { requestLibPath, schemaPath, mockFolder, allowedTags } = _a, rest = tslib_1.__rest(_a, ["requestLibPath", "schemaPath", "mockFolder", "allowedTags"]);
|
|
12
|
+
if (!schemaPath) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const openAPI = (yield (0, util_1.getOpenAPIConfig)(schemaPath));
|
|
16
|
+
if ((0, lodash_1.isEmpty)(openAPI)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const requestImportStatement = (0, util_1.getImportStatement)(requestLibPath);
|
|
20
|
+
const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, requestOptionsType: '{[key: string]: any}', namespace: 'API', nullable: false, isCamelCase: true, allowedTags: allowedTags
|
|
21
|
+
? (0, lodash_1.map)(allowedTags, (item) => item.toLowerCase())
|
|
22
|
+
: null }, rest), openAPI);
|
|
23
|
+
serviceGenerator.genFile();
|
|
24
|
+
if (mockFolder) {
|
|
25
|
+
(0, mockGenarator_1.mockGenerator)({
|
|
26
|
+
openAPI,
|
|
27
|
+
mockFolder: mockFolder,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
exports.generateService = generateService;
|
package/dist/log.d.ts
ADDED
package/dist/log.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
5
|
+
const log = (...rest) => console.log(`${chalk_1.default.blue('[openAPI]')}: ${rest.join('\n')}`);
|
|
6
|
+
exports.default = log;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { OpenAPIV3 } from 'openapi-types';
|
|
2
|
+
import { OpenAPIObject } from '../type';
|
|
3
|
+
export default class OpenAPIGeneratorMockJs {
|
|
4
|
+
protected openAPI: OpenAPIObject;
|
|
5
|
+
constructor(openAPI: OpenAPIObject);
|
|
6
|
+
private sampleFromSchema;
|
|
7
|
+
parser(): {
|
|
8
|
+
openapi: string;
|
|
9
|
+
info: OpenAPIV3.InfoObject;
|
|
10
|
+
servers?: OpenAPIV3.ServerObject[];
|
|
11
|
+
paths: OpenAPIV3.PathsObject<{}, {}>;
|
|
12
|
+
components?: OpenAPIV3.ComponentsObject;
|
|
13
|
+
security?: OpenAPIV3.SecurityRequirementObject[];
|
|
14
|
+
tags?: OpenAPIV3.TagObject[];
|
|
15
|
+
externalDocs?: OpenAPIV3.ExternalDocumentationObject;
|
|
16
|
+
'x-express-openapi-additional-middleware'?: (((request: any, response: any, next: any) => Promise<void>) | ((request: any, response: any, next: any) => void))[];
|
|
17
|
+
'x-express-openapi-validation-strict'?: boolean;
|
|
18
|
+
};
|
|
19
|
+
}
|