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,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
const memoizee_1 = tslib_1.__importDefault(require("memoizee"));
|
|
6
|
+
const util_1 = require("../generator/util");
|
|
7
|
+
const primitives_1 = require("./primitives");
|
|
8
|
+
const util_2 = require("./util");
|
|
9
|
+
function getDateByName(name, parentsKey) {
|
|
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
|
+
if (['group'].includes(name)) {
|
|
26
|
+
return 'group';
|
|
27
|
+
}
|
|
28
|
+
if (name.toLocaleLowerCase().endsWith('id')) {
|
|
29
|
+
return 'uuid';
|
|
30
|
+
}
|
|
31
|
+
if (name.toLocaleLowerCase().endsWith('type') ||
|
|
32
|
+
name.toLocaleLowerCase().endsWith('key') ||
|
|
33
|
+
['key'].includes(name)) {
|
|
34
|
+
return 'id';
|
|
35
|
+
}
|
|
36
|
+
if (name.toLocaleLowerCase().endsWith('label') || ['label'].includes(name)) {
|
|
37
|
+
const newParents = [...parentsKey];
|
|
38
|
+
newParents.pop();
|
|
39
|
+
const newType = getDateByName(newParents);
|
|
40
|
+
if (newType !== 'string' && newType !== 'csentence') {
|
|
41
|
+
return newType;
|
|
42
|
+
}
|
|
43
|
+
return 'label';
|
|
44
|
+
}
|
|
45
|
+
if (['email'].includes(name)) {
|
|
46
|
+
return 'email';
|
|
47
|
+
}
|
|
48
|
+
if (['password'].includes(name)) {
|
|
49
|
+
return 'string(16)';
|
|
50
|
+
}
|
|
51
|
+
if (['phone'].includes(name)) {
|
|
52
|
+
return 'phone';
|
|
53
|
+
}
|
|
54
|
+
if (['province'].includes(name)) {
|
|
55
|
+
return 'province';
|
|
56
|
+
}
|
|
57
|
+
if (['city'].includes(name)) {
|
|
58
|
+
return 'city';
|
|
59
|
+
}
|
|
60
|
+
if (['addr', 'address'].includes(name)) {
|
|
61
|
+
return 'county';
|
|
62
|
+
}
|
|
63
|
+
if (['country'].includes(name)) {
|
|
64
|
+
return 'country';
|
|
65
|
+
}
|
|
66
|
+
if (['url', 'imageUrl', 'href'].includes(name) ||
|
|
67
|
+
name.toLocaleLowerCase().endsWith('url') ||
|
|
68
|
+
name.toLocaleLowerCase().endsWith('urls') ||
|
|
69
|
+
name.toLocaleLowerCase().endsWith('image') ||
|
|
70
|
+
name.toLocaleLowerCase().endsWith('link')) {
|
|
71
|
+
return 'href';
|
|
72
|
+
}
|
|
73
|
+
if (name.toLocaleLowerCase().endsWith('errorcode')) {
|
|
74
|
+
return 'errorCode';
|
|
75
|
+
}
|
|
76
|
+
if (['type', 'status'].includes(name) ||
|
|
77
|
+
name.toLocaleLowerCase().endsWith('status') ||
|
|
78
|
+
name.toLocaleLowerCase().endsWith('type')) {
|
|
79
|
+
return 'status';
|
|
80
|
+
}
|
|
81
|
+
if (name.toLocaleLowerCase().endsWith('authority')) {
|
|
82
|
+
return 'authority';
|
|
83
|
+
}
|
|
84
|
+
return 'csentence';
|
|
85
|
+
}
|
|
86
|
+
function primitive(schemaParams, propsName) {
|
|
87
|
+
const schema = (0, util_2.objectify)(schemaParams);
|
|
88
|
+
const { type, format } = schema;
|
|
89
|
+
const value = primitives_1.primitives[`${type}_${format || getDateByName(propsName)}`] ||
|
|
90
|
+
primitives_1.primitives[type];
|
|
91
|
+
if ((0, lodash_1.isUndefined)(schema.example)) {
|
|
92
|
+
return value || `Unknown Type: ${schema.type}`;
|
|
93
|
+
}
|
|
94
|
+
return schema.example;
|
|
95
|
+
}
|
|
96
|
+
class OpenAPIGeneratorMockJs {
|
|
97
|
+
constructor(openAPI) {
|
|
98
|
+
this.sampleFromSchema = (schema, propsName, schemaSet = new Set()) => {
|
|
99
|
+
const schemaRef = (0, util_1.isReferenceObject)(schema) ? schema.$ref : null;
|
|
100
|
+
if (schemaRef) {
|
|
101
|
+
// 如果之前已经使用过该引用结构,直接返回null,不然会陷入无限递归的情况
|
|
102
|
+
if (schemaSet.has(schemaRef)) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
schemaSet.add(schemaRef);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const localSchema = (schemaRef ? (0, util_2.get)(this.openAPI, schemaRef) : (0, util_2.objectify)(schema));
|
|
110
|
+
let type = localSchema.type;
|
|
111
|
+
const { properties, additionalProperties, items, anyOf, oneOf, allOf } = localSchema;
|
|
112
|
+
if (allOf) {
|
|
113
|
+
let obj = {};
|
|
114
|
+
allOf.forEach((item) => {
|
|
115
|
+
const newObj = this.sampleFromSchema(item, propsName, new Set(schemaSet));
|
|
116
|
+
if ((0, lodash_1.isObject)(newObj)) {
|
|
117
|
+
obj = Object.assign(Object.assign({}, obj), newObj);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
return obj;
|
|
121
|
+
}
|
|
122
|
+
if (!type) {
|
|
123
|
+
if (properties) {
|
|
124
|
+
type = 'object';
|
|
125
|
+
}
|
|
126
|
+
else if (items) {
|
|
127
|
+
type = 'array';
|
|
128
|
+
}
|
|
129
|
+
else if (anyOf || oneOf) {
|
|
130
|
+
type = 'union';
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (type === 'null') {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
if (type === 'object') {
|
|
140
|
+
const props = (0, util_2.objectify)(properties);
|
|
141
|
+
const obj = {};
|
|
142
|
+
for (const name in props) {
|
|
143
|
+
obj[name] = this.sampleFromSchema(props[name], [...(propsName || []), name], new Set(schemaSet));
|
|
144
|
+
}
|
|
145
|
+
if (additionalProperties === true) {
|
|
146
|
+
obj.additionalProp1 = {};
|
|
147
|
+
return obj;
|
|
148
|
+
}
|
|
149
|
+
if (additionalProperties) {
|
|
150
|
+
const additionalProps = (0, util_2.objectify)(additionalProperties);
|
|
151
|
+
const additionalPropVal = this.sampleFromSchema(additionalProps, propsName, new Set(schemaSet));
|
|
152
|
+
for (let i = 1; i < 4; i += 1) {
|
|
153
|
+
obj[`additionalProp${i}`] = additionalPropVal;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return obj;
|
|
157
|
+
}
|
|
158
|
+
if (type === 'array') {
|
|
159
|
+
const item = this.sampleFromSchema(items, propsName, new Set(schemaSet));
|
|
160
|
+
return new Array(parseInt((Math.random() * 20).toFixed(0), 10)).fill(item);
|
|
161
|
+
}
|
|
162
|
+
if (type === 'union') {
|
|
163
|
+
const subschemas = anyOf || oneOf;
|
|
164
|
+
const subschemas_length = (subschemas && subschemas.length) || 0;
|
|
165
|
+
if (subschemas_length) {
|
|
166
|
+
const index = (0, util_2.getRandomInt)(0, subschemas_length);
|
|
167
|
+
const obj = this.sampleFromSchema(subschemas[index], propsName, new Set(schemaSet));
|
|
168
|
+
return obj;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (localSchema.enum) {
|
|
172
|
+
if (localSchema.default) {
|
|
173
|
+
return localSchema.default;
|
|
174
|
+
}
|
|
175
|
+
return (0, util_2.normalizeArray)(localSchema.enum)[0];
|
|
176
|
+
}
|
|
177
|
+
if (type === 'file') {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
return primitive(localSchema, propsName);
|
|
181
|
+
};
|
|
182
|
+
this.openAPI = openAPI;
|
|
183
|
+
this.sampleFromSchema = (0, memoizee_1.default)(this.sampleFromSchema);
|
|
184
|
+
}
|
|
185
|
+
parser() {
|
|
186
|
+
const openAPI = Object.assign({}, this.openAPI);
|
|
187
|
+
for (const path in openAPI.paths) {
|
|
188
|
+
for (const method in openAPI.paths[path]) {
|
|
189
|
+
const api = openAPI.paths[path][method];
|
|
190
|
+
for (const code in api.responses) {
|
|
191
|
+
const response = api.responses[code];
|
|
192
|
+
const mediaTypeKeys = (0, lodash_1.keys)(response.content);
|
|
193
|
+
if (mediaTypeKeys.length) {
|
|
194
|
+
let key;
|
|
195
|
+
if (mediaTypeKeys.includes('application/json')) {
|
|
196
|
+
key = 'application/json';
|
|
197
|
+
}
|
|
198
|
+
else if (mediaTypeKeys.includes('*/*')) {
|
|
199
|
+
key = '*/*';
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
key = mediaTypeKeys[0];
|
|
203
|
+
}
|
|
204
|
+
const schema = (0, util_2.inferSchema)(response.content[key]);
|
|
205
|
+
if (schema) {
|
|
206
|
+
response.example = this.sampleFromSchema(schema);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (!api.parameters)
|
|
211
|
+
continue;
|
|
212
|
+
for (const parameter of api.parameters) {
|
|
213
|
+
const schema = (0, util_2.inferSchema)(parameter);
|
|
214
|
+
parameter.example = schema
|
|
215
|
+
? this.sampleFromSchema(schema)
|
|
216
|
+
: null;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return openAPI;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
exports.default = OpenAPIGeneratorMockJs;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const primitives: Record<string, string>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.primitives = void 0;
|
|
4
|
+
exports.primitives = {
|
|
5
|
+
string: '@string',
|
|
6
|
+
string_email: '@email',
|
|
7
|
+
string_phone: '@phone',
|
|
8
|
+
string_username: '@cname',
|
|
9
|
+
string_url: '@url',
|
|
10
|
+
string_uuid: '@guid',
|
|
11
|
+
string_id: '@increment',
|
|
12
|
+
string_status: '@status',
|
|
13
|
+
string_authority: '@authority',
|
|
14
|
+
string_label: '@label',
|
|
15
|
+
string_group: '@group',
|
|
16
|
+
string_csentence: '@csentence',
|
|
17
|
+
string_cname: '@cname',
|
|
18
|
+
string_name: '@last',
|
|
19
|
+
string_errorCode: '@increment',
|
|
20
|
+
string_country: '@country',
|
|
21
|
+
string_county: '@county(true)',
|
|
22
|
+
string_province: '@province',
|
|
23
|
+
string_city: '@city',
|
|
24
|
+
string_avatar: '@avatar',
|
|
25
|
+
string_href: '@href',
|
|
26
|
+
'string_string(16)': '@string(16)',
|
|
27
|
+
'string_date-time': '@datetime',
|
|
28
|
+
string_date: '@date',
|
|
29
|
+
number: '@integer(60, 100)',
|
|
30
|
+
number_float: '@float(60, 100, 3, 5)',
|
|
31
|
+
integer: '@integer(60, 100)',
|
|
32
|
+
boolean: '@boolean',
|
|
33
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { OpenAPIV3 } from 'openapi-types';
|
|
2
|
+
import { OpenAPIObject, ParameterObject, ReferenceObject, SchemaObject } from '../type';
|
|
3
|
+
export declare function objectify<T>(thing: T): T;
|
|
4
|
+
export declare function get(openAPI: OpenAPIObject, path: string): OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject;
|
|
5
|
+
export declare function normalizeArray(arr: string[] | string): string[];
|
|
6
|
+
export declare function inferSchema(thing: ParameterObject | SchemaObject | ReferenceObject | OpenAPIV3.MediaTypeObject): OpenAPIV3.ReferenceObject | OpenAPIV3.MediaTypeObject | {
|
|
7
|
+
type: string;
|
|
8
|
+
enum?: any[];
|
|
9
|
+
title?: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
default?: any;
|
|
12
|
+
multipleOf?: number;
|
|
13
|
+
maximum?: number;
|
|
14
|
+
exclusiveMaximum?: boolean;
|
|
15
|
+
minimum?: number;
|
|
16
|
+
exclusiveMinimum?: boolean;
|
|
17
|
+
maxLength?: number;
|
|
18
|
+
minLength?: number;
|
|
19
|
+
pattern?: string;
|
|
20
|
+
maxItems?: number;
|
|
21
|
+
minItems?: number;
|
|
22
|
+
uniqueItems?: boolean;
|
|
23
|
+
maxProperties?: number;
|
|
24
|
+
minProperties?: number;
|
|
25
|
+
required?: string[];
|
|
26
|
+
not?: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject;
|
|
27
|
+
nullable?: boolean;
|
|
28
|
+
discriminator?: OpenAPIV3.DiscriminatorObject;
|
|
29
|
+
readOnly?: boolean;
|
|
30
|
+
writeOnly?: boolean;
|
|
31
|
+
xml?: OpenAPIV3.XMLObject;
|
|
32
|
+
externalDocs?: OpenAPIV3.ExternalDocumentationObject;
|
|
33
|
+
example?: any;
|
|
34
|
+
deprecated?: boolean;
|
|
35
|
+
format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "dateTime" | "password" | "base64";
|
|
36
|
+
additionalProperties?: boolean | import("../type").ISchemaObject;
|
|
37
|
+
properties?: {
|
|
38
|
+
[name: string]: import("../type").ISchemaObject;
|
|
39
|
+
};
|
|
40
|
+
allOf?: import("../type").ISchemaObject[];
|
|
41
|
+
oneOf?: import("../type").ISchemaObject[];
|
|
42
|
+
anyOf?: import("../type").ISchemaObject[];
|
|
43
|
+
'x-enum-varnames'?: string[];
|
|
44
|
+
'x-enum-comments'?: {
|
|
45
|
+
[name: string]: string;
|
|
46
|
+
};
|
|
47
|
+
items: import("../type").ISchemaObject;
|
|
48
|
+
} | {
|
|
49
|
+
type: string;
|
|
50
|
+
enum?: any[];
|
|
51
|
+
title?: string;
|
|
52
|
+
description?: string;
|
|
53
|
+
default?: any;
|
|
54
|
+
multipleOf?: number;
|
|
55
|
+
maximum?: number;
|
|
56
|
+
exclusiveMaximum?: boolean;
|
|
57
|
+
minimum?: number;
|
|
58
|
+
exclusiveMinimum?: boolean;
|
|
59
|
+
maxLength?: number;
|
|
60
|
+
minLength?: number;
|
|
61
|
+
pattern?: string;
|
|
62
|
+
maxItems?: number;
|
|
63
|
+
minItems?: number;
|
|
64
|
+
uniqueItems?: boolean;
|
|
65
|
+
maxProperties?: number;
|
|
66
|
+
minProperties?: number;
|
|
67
|
+
required?: string[];
|
|
68
|
+
not?: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject;
|
|
69
|
+
nullable?: boolean;
|
|
70
|
+
discriminator?: OpenAPIV3.DiscriminatorObject;
|
|
71
|
+
readOnly?: boolean;
|
|
72
|
+
writeOnly?: boolean;
|
|
73
|
+
xml?: OpenAPIV3.XMLObject;
|
|
74
|
+
externalDocs?: OpenAPIV3.ExternalDocumentationObject;
|
|
75
|
+
example?: any;
|
|
76
|
+
deprecated?: boolean;
|
|
77
|
+
format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "dateTime" | "password" | "base64";
|
|
78
|
+
additionalProperties?: boolean | import("../type").ISchemaObject;
|
|
79
|
+
properties?: {
|
|
80
|
+
[name: string]: import("../type").ISchemaObject;
|
|
81
|
+
};
|
|
82
|
+
allOf?: import("../type").ISchemaObject[];
|
|
83
|
+
oneOf?: import("../type").ISchemaObject[];
|
|
84
|
+
anyOf?: import("../type").ISchemaObject[];
|
|
85
|
+
'x-enum-varnames'?: string[];
|
|
86
|
+
'x-enum-comments'?: {
|
|
87
|
+
[name: string]: string;
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
export declare function getRandomInt(min: number, max: number): number;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRandomInt = exports.inferSchema = exports.normalizeArray = exports.get = exports.objectify = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
const util_1 = require("../generator/util");
|
|
6
|
+
function objectify(thing) {
|
|
7
|
+
if (!(0, lodash_1.isObject)(thing))
|
|
8
|
+
return {};
|
|
9
|
+
return thing;
|
|
10
|
+
}
|
|
11
|
+
exports.objectify = objectify;
|
|
12
|
+
function get(openAPI, path) {
|
|
13
|
+
var _a, _b;
|
|
14
|
+
const refPaths = path.split('/');
|
|
15
|
+
const schema = (_b = (_a = openAPI.components) === null || _a === void 0 ? void 0 : _a.schemas) === null || _b === void 0 ? void 0 : _b[refPaths[refPaths.length - 1]];
|
|
16
|
+
if (!schema) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
return schema;
|
|
20
|
+
}
|
|
21
|
+
exports.get = get;
|
|
22
|
+
function normalizeArray(arr) {
|
|
23
|
+
if ((0, lodash_1.isArray)(arr))
|
|
24
|
+
return arr;
|
|
25
|
+
return [arr];
|
|
26
|
+
}
|
|
27
|
+
exports.normalizeArray = normalizeArray;
|
|
28
|
+
function isParameterObject(thing) {
|
|
29
|
+
return (thing === null || thing === void 0 ? void 0 : thing.schema) !== undefined;
|
|
30
|
+
}
|
|
31
|
+
function inferSchema(thing) {
|
|
32
|
+
if (isParameterObject(thing)) {
|
|
33
|
+
return thing.schema;
|
|
34
|
+
}
|
|
35
|
+
if ((0, util_1.isSchemaObject)(thing)) {
|
|
36
|
+
return Object.assign(Object.assign({}, thing), { type: 'object' });
|
|
37
|
+
}
|
|
38
|
+
return thing;
|
|
39
|
+
}
|
|
40
|
+
exports.inferSchema = inferSchema;
|
|
41
|
+
function getRandomInt(min, max) {
|
|
42
|
+
const minCeiled = Math.ceil(min);
|
|
43
|
+
const maxFloored = Math.floor(max);
|
|
44
|
+
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled);
|
|
45
|
+
}
|
|
46
|
+
exports.getRandomInt = getRandomInt;
|
package/dist/type.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { OpenAPIV3 } from 'openapi-types';
|
|
2
|
+
type Modify<T, R> = Omit<T, keyof R> & R;
|
|
3
|
+
type ICustomBaseSchemaObject = {
|
|
4
|
+
type: ISchemaObjectType;
|
|
5
|
+
format?: ISchemaObjectFormat;
|
|
6
|
+
additionalProperties?: boolean | ISchemaObject;
|
|
7
|
+
properties?: {
|
|
8
|
+
[name: string]: ISchemaObject;
|
|
9
|
+
};
|
|
10
|
+
allOf?: ISchemaObject[];
|
|
11
|
+
oneOf?: ISchemaObject[];
|
|
12
|
+
anyOf?: ISchemaObject[];
|
|
13
|
+
'x-enum-varnames'?: string[];
|
|
14
|
+
'x-enum-comments'?: {
|
|
15
|
+
[name: string]: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export type ArraySchemaObject = Modify<OpenAPIV3.ArraySchemaObject, ICustomBaseSchemaObject & {
|
|
19
|
+
items: ISchemaObject;
|
|
20
|
+
}>;
|
|
21
|
+
export type BinaryArraySchemaObject = ArraySchemaObject;
|
|
22
|
+
export type NonArraySchemaObject = Modify<OpenAPIV3.NonArraySchemaObject, ICustomBaseSchemaObject>;
|
|
23
|
+
export type SchemaObject = ArraySchemaObject | NonArraySchemaObject;
|
|
24
|
+
export type ISchemaObject = SchemaObject | ReferenceObject;
|
|
25
|
+
export type ReferenceObject = OpenAPIV3.ReferenceObject;
|
|
26
|
+
export type PathItemObject = Modify<OpenAPIV3.PathItemObject, {
|
|
27
|
+
parameters?: (ReferenceObject | ParameterObject)[];
|
|
28
|
+
}>;
|
|
29
|
+
export type OperationObject = Modify<OpenAPIV3.OperationObject, {
|
|
30
|
+
parameters?: (ReferenceObject | ParameterObject)[];
|
|
31
|
+
}>;
|
|
32
|
+
export type ComponentsObject = OpenAPIV3.ComponentsObject;
|
|
33
|
+
export type OpenAPIObject = OpenAPIV3.Document;
|
|
34
|
+
export type ParameterObject = Modify<OpenAPIV3.ParameterObject, {
|
|
35
|
+
schema?: ISchemaObject;
|
|
36
|
+
}>;
|
|
37
|
+
export type ResponsesObject = OpenAPIV3.ResponsesObject;
|
|
38
|
+
export type ResponseObject = OpenAPIV3.ResponseObject & {
|
|
39
|
+
example?: any;
|
|
40
|
+
};
|
|
41
|
+
export type RequestBodyObject = OpenAPIV3.RequestBodyObject;
|
|
42
|
+
export type ContentObject = {
|
|
43
|
+
[media: string]: OpenAPIV3.MediaTypeObject;
|
|
44
|
+
};
|
|
45
|
+
export declare enum SchemaObjectFormat {
|
|
46
|
+
int32 = "int32",
|
|
47
|
+
int64 = "int64",
|
|
48
|
+
float = "float",
|
|
49
|
+
double = "double",
|
|
50
|
+
byte = "byte",
|
|
51
|
+
binary = "binary",
|
|
52
|
+
date = "date",
|
|
53
|
+
dateTime = "date-time",
|
|
54
|
+
password = "password",
|
|
55
|
+
base64 = "base64"
|
|
56
|
+
}
|
|
57
|
+
export type ISchemaObjectFormat = keyof typeof SchemaObjectFormat;
|
|
58
|
+
export declare enum SchemaObjectType {
|
|
59
|
+
array = "array",
|
|
60
|
+
stringArray = "string[]",
|
|
61
|
+
boolean = "boolean",
|
|
62
|
+
object = "object",
|
|
63
|
+
number = "number",
|
|
64
|
+
string = "string",
|
|
65
|
+
integer = "integer",
|
|
66
|
+
enum = "enum",
|
|
67
|
+
null = "null",
|
|
68
|
+
union = "union",
|
|
69
|
+
file = "file"
|
|
70
|
+
}
|
|
71
|
+
export type ISchemaObjectType = keyof typeof SchemaObjectType;
|
|
72
|
+
export {};
|
package/dist/type.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SchemaObjectType = exports.SchemaObjectFormat = void 0;
|
|
4
|
+
var SchemaObjectFormat;
|
|
5
|
+
(function (SchemaObjectFormat) {
|
|
6
|
+
SchemaObjectFormat["int32"] = "int32";
|
|
7
|
+
SchemaObjectFormat["int64"] = "int64";
|
|
8
|
+
SchemaObjectFormat["float"] = "float";
|
|
9
|
+
SchemaObjectFormat["double"] = "double";
|
|
10
|
+
SchemaObjectFormat["byte"] = "byte";
|
|
11
|
+
SchemaObjectFormat["binary"] = "binary";
|
|
12
|
+
SchemaObjectFormat["date"] = "date";
|
|
13
|
+
SchemaObjectFormat["dateTime"] = "date-time";
|
|
14
|
+
SchemaObjectFormat["password"] = "password";
|
|
15
|
+
SchemaObjectFormat["base64"] = "base64";
|
|
16
|
+
})(SchemaObjectFormat || (exports.SchemaObjectFormat = SchemaObjectFormat = {}));
|
|
17
|
+
var SchemaObjectType;
|
|
18
|
+
(function (SchemaObjectType) {
|
|
19
|
+
SchemaObjectType["array"] = "array";
|
|
20
|
+
SchemaObjectType["stringArray"] = "string[]";
|
|
21
|
+
SchemaObjectType["boolean"] = "boolean";
|
|
22
|
+
SchemaObjectType["object"] = "object";
|
|
23
|
+
SchemaObjectType["number"] = "number";
|
|
24
|
+
SchemaObjectType["string"] = "string";
|
|
25
|
+
SchemaObjectType["integer"] = "integer";
|
|
26
|
+
SchemaObjectType["enum"] = "enum";
|
|
27
|
+
SchemaObjectType["null"] = "null";
|
|
28
|
+
SchemaObjectType["union"] = "union";
|
|
29
|
+
SchemaObjectType["file"] = "file";
|
|
30
|
+
})(SchemaObjectType || (exports.SchemaObjectType = SchemaObjectType = {}));
|
package/dist/util.d.ts
ADDED
package/dist/util.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getOpenAPIConfig = exports.getImportStatement = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
6
|
+
const http_1 = tslib_1.__importDefault(require("http"));
|
|
7
|
+
const https_1 = tslib_1.__importDefault(require("https"));
|
|
8
|
+
const swagger2openapi_1 = tslib_1.__importDefault(require("swagger2openapi"));
|
|
9
|
+
const log_1 = tslib_1.__importDefault(require("./log"));
|
|
10
|
+
const getImportStatement = (requestLibPath) => {
|
|
11
|
+
if (requestLibPath) {
|
|
12
|
+
if (requestLibPath.startsWith('import')) {
|
|
13
|
+
return requestLibPath;
|
|
14
|
+
}
|
|
15
|
+
return `import request from '${requestLibPath}';`;
|
|
16
|
+
}
|
|
17
|
+
return `import request from 'axios';`;
|
|
18
|
+
};
|
|
19
|
+
exports.getImportStatement = getImportStatement;
|
|
20
|
+
function getSchema(schemaPath) {
|
|
21
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
if (schemaPath.startsWith('http')) {
|
|
23
|
+
const isHttps = schemaPath.startsWith('https:');
|
|
24
|
+
const protocol = isHttps ? https_1.default : http_1.default;
|
|
25
|
+
try {
|
|
26
|
+
const agent = new protocol.Agent({
|
|
27
|
+
rejectUnauthorized: false,
|
|
28
|
+
});
|
|
29
|
+
const config = isHttps ? { httpsAgent: agent } : { httpAgent: agent };
|
|
30
|
+
const json = yield axios_1.default
|
|
31
|
+
.get(schemaPath, config)
|
|
32
|
+
.then((res) => res.data);
|
|
33
|
+
return json;
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
console.log('fetch openapi error:', error);
|
|
37
|
+
}
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (require.cache[schemaPath]) {
|
|
41
|
+
delete require.cache[schemaPath];
|
|
42
|
+
}
|
|
43
|
+
const schema = (yield require(schemaPath));
|
|
44
|
+
return schema;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function converterSwaggerToOpenApi(swagger) {
|
|
48
|
+
if (!swagger.swagger) {
|
|
49
|
+
return swagger;
|
|
50
|
+
}
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
const convertOptions = {
|
|
53
|
+
patch: true,
|
|
54
|
+
warnOnly: true,
|
|
55
|
+
resolveInternal: true,
|
|
56
|
+
};
|
|
57
|
+
// options.patch = true; // fix up small errors in the source definition
|
|
58
|
+
// options.warnOnly = true; // Do not throw on non-patchable errors
|
|
59
|
+
// options.warnOnly = true; // enable resolution of internal $refs, also disables deduplication of requestBodies
|
|
60
|
+
swagger2openapi_1.default.convertObj(swagger, convertOptions, (err, options) => {
|
|
61
|
+
(0, log_1.default)(['💺 将 Swagger 转化为 openAPI']);
|
|
62
|
+
if (err) {
|
|
63
|
+
return reject(err);
|
|
64
|
+
}
|
|
65
|
+
resolve(options.openapi);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
const getOpenAPIConfig = (schemaPath) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
70
|
+
const schema = yield getSchema(schemaPath);
|
|
71
|
+
if (!schema) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const openAPI = yield converterSwaggerToOpenApi(schema);
|
|
75
|
+
return openAPI;
|
|
76
|
+
});
|
|
77
|
+
exports.getOpenAPIConfig = getOpenAPIConfig;
|
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "openapi-ts-request",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Swagger2/OpenAPI3 to TypeScript, Request Client, Display Label",
|
|
5
|
+
"packageManager": "pnpm@9.4.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+git@github.com:openapi-ui/openapi-ts-request.git"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": "rookie-luochao",
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"types": "index.d.ts",
|
|
14
|
+
"bin": {
|
|
15
|
+
"openapi-ts-request": "dist/cli.js"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"templates",
|
|
20
|
+
"prettier.config.cjs"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"start": "tsc -w",
|
|
24
|
+
"build": "tsc",
|
|
25
|
+
"changeset": "changeset",
|
|
26
|
+
"lint": "eslint ./src --report-unused-disable-directives --max-warnings=0",
|
|
27
|
+
"lint:fix": "eslint ./src --report-unused-disable-directives --max-warnings=0 --fix",
|
|
28
|
+
"test": "rm -rf ./test/apis/ ./test/mocks && npm run build && cd ./test && node ./test.js && cd ..",
|
|
29
|
+
"test:windows": "rimraf ./test/apis/ ./test/mocks && npm run build && cd ./test && node ./test.js && cd ..",
|
|
30
|
+
"prepare": "husky"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@prettier/sync": "^0.5.2",
|
|
34
|
+
"axios": "^1.7.2",
|
|
35
|
+
"chalk": "^4.1.2",
|
|
36
|
+
"cosmiconfig": "^9.0.0",
|
|
37
|
+
"glob": "^10.4.2",
|
|
38
|
+
"lodash": "^4.17.21",
|
|
39
|
+
"memoizee": "^0.4.17",
|
|
40
|
+
"mockjs": "^1.1.0",
|
|
41
|
+
"nunjucks": "^3.2.4",
|
|
42
|
+
"prettier": "^3.3.2",
|
|
43
|
+
"reserved-words": "^0.1.2",
|
|
44
|
+
"rimraf": "^5.0.7",
|
|
45
|
+
"swagger2openapi": "^7.0.8",
|
|
46
|
+
"tiny-pinyin": "^1.3.2"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@changesets/changelog-github": "^0.5.0",
|
|
50
|
+
"@changesets/cli": "^2.27.6",
|
|
51
|
+
"@commitlint/cli": "^19.2.1",
|
|
52
|
+
"@commitlint/config-conventional": "^19.2.2",
|
|
53
|
+
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
|
|
54
|
+
"@types/lodash": "^4.17.5",
|
|
55
|
+
"@types/memoizee": "^0.4.11",
|
|
56
|
+
"@types/mockjs": "^1.0.10",
|
|
57
|
+
"@types/node": "^20.14.6",
|
|
58
|
+
"@types/nunjucks": "^3.2.6",
|
|
59
|
+
"@types/reserved-words": "^0.1.4",
|
|
60
|
+
"@types/swagger2openapi": "^7.0.4",
|
|
61
|
+
"@typescript-eslint/eslint-plugin": "^7.9.0",
|
|
62
|
+
"@typescript-eslint/parser": "^7.9.0",
|
|
63
|
+
"eslint": "^8.56.0",
|
|
64
|
+
"husky": "^9.0.11",
|
|
65
|
+
"lint-staged": "^15.2.5",
|
|
66
|
+
"np": "^10.0.6",
|
|
67
|
+
"openapi-types": "^12.1.3",
|
|
68
|
+
"tslib": "^2.6.3",
|
|
69
|
+
"typescript": "5.4.5"
|
|
70
|
+
},
|
|
71
|
+
"keywords": [
|
|
72
|
+
"openapi",
|
|
73
|
+
"openapi3",
|
|
74
|
+
"swagger",
|
|
75
|
+
"openapi to ts",
|
|
76
|
+
"openapi to request client",
|
|
77
|
+
"openapi to axios client",
|
|
78
|
+
"openapi to fetch client",
|
|
79
|
+
"openapi to uni.request client"
|
|
80
|
+
]
|
|
81
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
import * as {{ namespace }} from './{{ interfaceFileName }}';
|
|
4
|
+
|
|
5
|
+
{% for type in list -%}
|
|
6
|
+
export function {{ type.displayLabelFuncName }}(field: {{ namespace }}.I{{ type.typeName }}) {
|
|
7
|
+
return ({{ type.enumLabelType }})[field]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
{% endfor -%}
|