@dazhicheng/openapi 1.0.0 → 1.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.
@@ -0,0 +1,478 @@
1
+ import type { ContentObject, OpenAPIObject, OperationObject, ParameterObject, ReferenceObject, RequestBodyObject, ResponsesObject, SchemaObject } from "openapi3-ts";
2
+ import type { GenerateServiceProps } from "./index";
3
+ export type TypescriptFileType = "interface" | "serviceController" | "serviceIndex";
4
+ export interface APIDataType extends OperationObject {
5
+ path: string;
6
+ method: string;
7
+ }
8
+ export type TagAPIDataType = Record<string, APIDataType[]>;
9
+ export interface MappingItemType {
10
+ antTechApi: string;
11
+ popAction: string;
12
+ popProduct: string;
13
+ antTechVersion: string;
14
+ }
15
+ export interface ControllerType {
16
+ fileName: string;
17
+ controllerName: string;
18
+ }
19
+ export declare const getPath: () => string;
20
+ export declare const getGenInfo: (isDirExist: boolean, appName: string, absSrcPath: string) => boolean[];
21
+ declare class ServiceGenerator {
22
+ protected apiData: TagAPIDataType;
23
+ protected classNameList: ControllerType[];
24
+ protected version: string;
25
+ protected mappings: MappingItemType[];
26
+ protected finalPath: string;
27
+ protected config: GenerateServiceProps;
28
+ protected openAPIData: OpenAPIObject;
29
+ constructor(config: GenerateServiceProps, openAPIData: OpenAPIObject);
30
+ genFile(): Promise<void>;
31
+ concatOrNull: (...arrays: any[]) => never[] | null;
32
+ getFuncationName(data: APIDataType): string;
33
+ getTypeName(data: APIDataType): string;
34
+ getServiceTP(): {
35
+ genType: string;
36
+ className: string;
37
+ instanceName: string;
38
+ list: {
39
+ functionName: string;
40
+ typeName: string;
41
+ path: string;
42
+ pathInComment: string;
43
+ hasPathVariables: boolean;
44
+ hasApiPrefix: boolean;
45
+ method: string;
46
+ desc: string | undefined;
47
+ hasHeader: boolean;
48
+ params: Record<string, ParameterObject[]> | {
49
+ path: {
50
+ alias: string;
51
+ name: string;
52
+ in: import("openapi3-ts").ParameterLocation;
53
+ description?: string;
54
+ required?: boolean;
55
+ deprecated?: boolean;
56
+ allowEmptyValue?: boolean;
57
+ style?: import("openapi3-ts").ParameterStyle;
58
+ explode?: boolean;
59
+ allowReserved?: boolean;
60
+ schema?: SchemaObject | ReferenceObject;
61
+ examples?: {
62
+ [param: string]: import("openapi3-ts").ExampleObject | ReferenceObject;
63
+ };
64
+ example?: any;
65
+ content?: ContentObject;
66
+ }[];
67
+ };
68
+ hasParams: boolean;
69
+ options: Record<string, any>;
70
+ body: {
71
+ required: any;
72
+ propertiesList: ({
73
+ key: string;
74
+ schema: {
75
+ type: string;
76
+ required: boolean;
77
+ $ref: string;
78
+ } | {
79
+ type: string;
80
+ required: boolean;
81
+ nullable?: boolean;
82
+ discriminator?: import("openapi3-ts").DiscriminatorObject;
83
+ readOnly?: boolean;
84
+ writeOnly?: boolean;
85
+ xml?: import("openapi3-ts").XmlObject;
86
+ externalDocs?: import("openapi3-ts").ExternalDocumentationObject;
87
+ example?: any;
88
+ examples?: any[];
89
+ deprecated?: boolean;
90
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
91
+ allOf?: (SchemaObject | ReferenceObject)[];
92
+ oneOf?: (SchemaObject | ReferenceObject)[];
93
+ anyOf?: (SchemaObject | ReferenceObject)[];
94
+ not?: SchemaObject | ReferenceObject;
95
+ items?: SchemaObject | ReferenceObject;
96
+ properties?: {
97
+ [propertyName: string]: SchemaObject | ReferenceObject;
98
+ };
99
+ additionalProperties?: SchemaObject | ReferenceObject | boolean;
100
+ description?: string;
101
+ default?: any;
102
+ title?: string;
103
+ multipleOf?: number;
104
+ maximum?: number;
105
+ exclusiveMaximum?: boolean;
106
+ minimum?: number;
107
+ exclusiveMinimum?: boolean;
108
+ maxLength?: number;
109
+ minLength?: number;
110
+ pattern?: string;
111
+ maxItems?: number;
112
+ minItems?: number;
113
+ uniqueItems?: boolean;
114
+ maxProperties?: number;
115
+ minProperties?: number;
116
+ enum?: any[];
117
+ };
118
+ } | undefined)[];
119
+ nullable?: boolean;
120
+ discriminator?: import("openapi3-ts").DiscriminatorObject;
121
+ readOnly?: boolean;
122
+ writeOnly?: boolean;
123
+ xml?: import("openapi3-ts").XmlObject;
124
+ externalDocs?: import("openapi3-ts").ExternalDocumentationObject;
125
+ example?: any;
126
+ examples?: any[];
127
+ deprecated?: boolean;
128
+ type?: "integer" | "number" | "string" | "boolean" | "object" | "null" | "array";
129
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
130
+ allOf?: (SchemaObject | ReferenceObject)[];
131
+ oneOf?: (SchemaObject | ReferenceObject)[];
132
+ anyOf?: (SchemaObject | ReferenceObject)[];
133
+ not?: SchemaObject | ReferenceObject;
134
+ items?: SchemaObject | ReferenceObject;
135
+ properties?: {
136
+ [propertyName: string]: SchemaObject | ReferenceObject;
137
+ };
138
+ additionalProperties?: SchemaObject | ReferenceObject | boolean;
139
+ description?: string;
140
+ default?: any;
141
+ title?: string;
142
+ multipleOf?: number;
143
+ maximum?: number;
144
+ exclusiveMaximum?: boolean;
145
+ minimum?: number;
146
+ exclusiveMinimum?: boolean;
147
+ maxLength?: number;
148
+ minLength?: number;
149
+ pattern?: string;
150
+ maxItems?: number;
151
+ minItems?: number;
152
+ uniqueItems?: boolean;
153
+ maxProperties?: number;
154
+ minProperties?: number;
155
+ enum?: any[];
156
+ mediaType: string | undefined;
157
+ } | {
158
+ mediaType: string | undefined;
159
+ required: any;
160
+ type: string;
161
+ } | null;
162
+ file: any;
163
+ hasFormData: boolean;
164
+ response: {
165
+ mediaType: string;
166
+ type: string;
167
+ };
168
+ tags?: string[];
169
+ summary?: string;
170
+ description?: string;
171
+ externalDocs?: import("openapi3-ts").ExternalDocumentationObject;
172
+ operationId?: string;
173
+ parameters?: (ParameterObject | ReferenceObject)[];
174
+ requestBody?: RequestBodyObject | ReferenceObject;
175
+ responses: ResponsesObject;
176
+ callbacks?: import("openapi3-ts").CallbacksObject;
177
+ deprecated?: boolean;
178
+ security?: import("openapi3-ts").SecurityRequirementObject[];
179
+ servers?: import("openapi3-ts").ServerObject[];
180
+ }[];
181
+ }[];
182
+ getBodyTP(requestBody?: any): {
183
+ required: any;
184
+ propertiesList: ({
185
+ key: string;
186
+ schema: {
187
+ type: string;
188
+ required: boolean;
189
+ $ref: string;
190
+ } | {
191
+ type: string;
192
+ required: boolean;
193
+ nullable?: boolean;
194
+ discriminator?: import("openapi3-ts").DiscriminatorObject;
195
+ readOnly?: boolean;
196
+ writeOnly?: boolean;
197
+ xml?: import("openapi3-ts").XmlObject;
198
+ externalDocs?: import("openapi3-ts").ExternalDocumentationObject;
199
+ example?: any;
200
+ examples?: any[];
201
+ deprecated?: boolean;
202
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
203
+ allOf?: (SchemaObject | ReferenceObject)[];
204
+ oneOf?: (SchemaObject | ReferenceObject)[];
205
+ anyOf?: (SchemaObject | ReferenceObject)[];
206
+ not?: SchemaObject | ReferenceObject;
207
+ items?: SchemaObject | ReferenceObject;
208
+ properties?: {
209
+ [propertyName: string]: SchemaObject | ReferenceObject;
210
+ };
211
+ additionalProperties?: SchemaObject | ReferenceObject | boolean;
212
+ description?: string;
213
+ default?: any;
214
+ title?: string;
215
+ multipleOf?: number;
216
+ maximum?: number;
217
+ exclusiveMaximum?: boolean;
218
+ minimum?: number;
219
+ exclusiveMinimum?: boolean;
220
+ maxLength?: number;
221
+ minLength?: number;
222
+ pattern?: string;
223
+ maxItems?: number;
224
+ minItems?: number;
225
+ uniqueItems?: boolean;
226
+ maxProperties?: number;
227
+ minProperties?: number;
228
+ enum?: any[];
229
+ };
230
+ } | undefined)[];
231
+ nullable?: boolean;
232
+ discriminator?: import("openapi3-ts").DiscriminatorObject;
233
+ readOnly?: boolean;
234
+ writeOnly?: boolean;
235
+ xml?: import("openapi3-ts").XmlObject;
236
+ externalDocs?: import("openapi3-ts").ExternalDocumentationObject;
237
+ example?: any;
238
+ examples?: any[];
239
+ deprecated?: boolean;
240
+ type?: "integer" | "number" | "string" | "boolean" | "object" | "null" | "array";
241
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
242
+ allOf?: (SchemaObject | ReferenceObject)[];
243
+ oneOf?: (SchemaObject | ReferenceObject)[];
244
+ anyOf?: (SchemaObject | ReferenceObject)[];
245
+ not?: SchemaObject | ReferenceObject;
246
+ items?: SchemaObject | ReferenceObject;
247
+ properties?: {
248
+ [propertyName: string]: SchemaObject | ReferenceObject;
249
+ };
250
+ additionalProperties?: SchemaObject | ReferenceObject | boolean;
251
+ description?: string;
252
+ default?: any;
253
+ title?: string;
254
+ multipleOf?: number;
255
+ maximum?: number;
256
+ exclusiveMaximum?: boolean;
257
+ minimum?: number;
258
+ exclusiveMinimum?: boolean;
259
+ maxLength?: number;
260
+ minLength?: number;
261
+ pattern?: string;
262
+ maxItems?: number;
263
+ minItems?: number;
264
+ uniqueItems?: boolean;
265
+ maxProperties?: number;
266
+ minProperties?: number;
267
+ enum?: any[];
268
+ mediaType: string | undefined;
269
+ } | {
270
+ mediaType: string | undefined;
271
+ required: any;
272
+ type: string;
273
+ } | null;
274
+ getFileTP(requestBody?: any): any;
275
+ resolveFileTP(obj: any): any;
276
+ getResponseTP(responses?: ResponsesObject): {
277
+ mediaType: string;
278
+ type: string;
279
+ };
280
+ getParamsTP(parameters?: (ParameterObject | ReferenceObject)[], path?: string): Record<string, ParameterObject[]>;
281
+ getInterfaceTP(): {
282
+ typeName: string;
283
+ type: any;
284
+ parent: any;
285
+ props: any;
286
+ isEnum: any;
287
+ }[] | undefined;
288
+ private genFileFromTemplate;
289
+ private getTemplate;
290
+ getProps(schemaObject: SchemaObject): {
291
+ name: string;
292
+ type: string;
293
+ desc: string;
294
+ required: boolean;
295
+ nullable?: boolean;
296
+ discriminator?: import("openapi3-ts").DiscriminatorObject;
297
+ readOnly?: boolean;
298
+ writeOnly?: boolean;
299
+ xml?: import("openapi3-ts").XmlObject;
300
+ externalDocs?: import("openapi3-ts").ExternalDocumentationObject;
301
+ example?: any;
302
+ examples?: any[];
303
+ deprecated?: boolean;
304
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
305
+ allOf?: (SchemaObject | ReferenceObject)[];
306
+ oneOf?: (SchemaObject | ReferenceObject)[];
307
+ anyOf?: (SchemaObject | ReferenceObject)[];
308
+ not?: SchemaObject | ReferenceObject;
309
+ items?: SchemaObject | ReferenceObject;
310
+ properties?: {
311
+ [propertyName: string]: SchemaObject | ReferenceObject;
312
+ };
313
+ additionalProperties?: SchemaObject | ReferenceObject | boolean;
314
+ description?: string;
315
+ default?: any;
316
+ title?: string;
317
+ multipleOf?: number;
318
+ maximum?: number;
319
+ exclusiveMaximum?: boolean;
320
+ minimum?: number;
321
+ exclusiveMinimum?: boolean;
322
+ maxLength?: number;
323
+ minLength?: number;
324
+ pattern?: string;
325
+ maxItems?: number;
326
+ minItems?: number;
327
+ uniqueItems?: boolean;
328
+ maxProperties?: number;
329
+ minProperties?: number;
330
+ enum?: any[];
331
+ }[];
332
+ getType(schemaObject: SchemaObject | undefined, namespace?: string): string;
333
+ resolveObject(schemaObject: SchemaObject): any;
334
+ resolveArray(schemaObject: SchemaObject): "any[]" | {
335
+ type: string;
336
+ };
337
+ resolveProperties(schemaObject: SchemaObject): {
338
+ props: {
339
+ name: string;
340
+ type: string;
341
+ desc: string;
342
+ required: boolean;
343
+ nullable?: boolean;
344
+ discriminator?: import("openapi3-ts").DiscriminatorObject;
345
+ readOnly?: boolean;
346
+ writeOnly?: boolean;
347
+ xml?: import("openapi3-ts").XmlObject;
348
+ externalDocs?: import("openapi3-ts").ExternalDocumentationObject;
349
+ example?: any;
350
+ examples?: any[];
351
+ deprecated?: boolean;
352
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
353
+ allOf?: (SchemaObject | ReferenceObject)[];
354
+ oneOf?: (SchemaObject | ReferenceObject)[];
355
+ anyOf?: (SchemaObject | ReferenceObject)[];
356
+ not?: SchemaObject | ReferenceObject;
357
+ items?: SchemaObject | ReferenceObject;
358
+ properties?: {
359
+ [propertyName: string]: SchemaObject | ReferenceObject;
360
+ };
361
+ additionalProperties?: SchemaObject | ReferenceObject | boolean;
362
+ description?: string;
363
+ default?: any;
364
+ title?: string;
365
+ multipleOf?: number;
366
+ maximum?: number;
367
+ exclusiveMaximum?: boolean;
368
+ minimum?: number;
369
+ exclusiveMinimum?: boolean;
370
+ maxLength?: number;
371
+ minLength?: number;
372
+ pattern?: string;
373
+ maxItems?: number;
374
+ minItems?: number;
375
+ uniqueItems?: boolean;
376
+ maxProperties?: number;
377
+ minProperties?: number;
378
+ enum?: any[];
379
+ }[][];
380
+ };
381
+ resolveEnumObject(schemaObject: SchemaObject): {
382
+ isEnum: boolean;
383
+ type: string | undefined;
384
+ };
385
+ resolveAllOfObject(schemaObject: SchemaObject): {
386
+ props: ({
387
+ name: string;
388
+ type: string;
389
+ desc: string;
390
+ required: boolean;
391
+ nullable?: boolean;
392
+ discriminator?: import("openapi3-ts").DiscriminatorObject;
393
+ readOnly?: boolean;
394
+ writeOnly?: boolean;
395
+ xml?: import("openapi3-ts").XmlObject;
396
+ externalDocs?: import("openapi3-ts").ExternalDocumentationObject;
397
+ example?: any;
398
+ examples?: any[];
399
+ deprecated?: boolean;
400
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
401
+ allOf?: (SchemaObject | ReferenceObject)[];
402
+ oneOf?: (SchemaObject | ReferenceObject)[];
403
+ anyOf?: (SchemaObject | ReferenceObject)[];
404
+ not?: SchemaObject | ReferenceObject;
405
+ items?: SchemaObject | ReferenceObject;
406
+ properties?: {
407
+ [propertyName: string]: SchemaObject | ReferenceObject;
408
+ };
409
+ additionalProperties?: SchemaObject | ReferenceObject | boolean;
410
+ description?: string;
411
+ default?: any;
412
+ title?: string;
413
+ multipleOf?: number;
414
+ maximum?: number;
415
+ exclusiveMaximum?: boolean;
416
+ minimum?: number;
417
+ exclusiveMinimum?: boolean;
418
+ maxLength?: number;
419
+ minLength?: number;
420
+ pattern?: string;
421
+ maxItems?: number;
422
+ minItems?: number;
423
+ uniqueItems?: boolean;
424
+ maxProperties?: number;
425
+ minProperties?: number;
426
+ enum?: any[];
427
+ }[] | ({
428
+ type: string | undefined;
429
+ $ref: string;
430
+ } | {
431
+ type: string | undefined;
432
+ nullable?: boolean;
433
+ discriminator?: import("openapi3-ts").DiscriminatorObject;
434
+ readOnly?: boolean;
435
+ writeOnly?: boolean;
436
+ xml?: import("openapi3-ts").XmlObject;
437
+ externalDocs?: import("openapi3-ts").ExternalDocumentationObject;
438
+ example?: any;
439
+ examples?: any[];
440
+ deprecated?: boolean;
441
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
442
+ allOf?: (SchemaObject | ReferenceObject)[];
443
+ oneOf?: (SchemaObject | ReferenceObject)[];
444
+ anyOf?: (SchemaObject | ReferenceObject)[];
445
+ not?: SchemaObject | ReferenceObject;
446
+ items?: SchemaObject | ReferenceObject;
447
+ properties?: {
448
+ [propertyName: string]: SchemaObject | ReferenceObject;
449
+ };
450
+ additionalProperties?: SchemaObject | ReferenceObject | boolean;
451
+ description?: string;
452
+ default?: any;
453
+ title?: string;
454
+ multipleOf?: number;
455
+ maximum?: number;
456
+ exclusiveMaximum?: boolean;
457
+ minimum?: number;
458
+ exclusiveMinimum?: boolean;
459
+ maxLength?: number;
460
+ minLength?: number;
461
+ pattern?: string;
462
+ maxItems?: number;
463
+ minItems?: number;
464
+ uniqueItems?: boolean;
465
+ maxProperties?: number;
466
+ minProperties?: number;
467
+ required?: string[];
468
+ enum?: any[];
469
+ })[])[];
470
+ };
471
+ private genDefaultFunctionName;
472
+ private getBasePrefix;
473
+ private resolveRefObject;
474
+ private getFinalFileName;
475
+ private replaceDot;
476
+ private resolveFunctionName;
477
+ }
478
+ export { ServiceGenerator };
package/dist/util.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ export declare const getAbsolutePath: (filePath: string) => string;
2
+ export declare const mkdir: (dir: string) => void;
3
+ export declare const prettierFile: (content: string) => Promise<[string, boolean]>;
4
+ export declare const writeFile: (folderPath: string, fileName: string, content: string) => Promise<boolean>;
5
+ export declare const getTagName: (name: string) => string | undefined;
6
+ /**
7
+ * 根据当前的数据源类型,对请求回来的 apiInfo 进行格式化
8
+ * 如果是 op 数据源,对 tags 以及 path 中的 tags 进行处理
9
+ * - before: 前缀(产品集.产品码) + 操作对象(必填)+ 子操作对象(可选)+ 动作(必填)
10
+ * - after: 操作对象(必填)+ 子操作对象(可选) ==> 驼峰
11
+ */
12
+ export declare const formatApiInfo: (apiInfo: Record<string, any>) => any;
13
+ type serviceParam = {
14
+ title: string;
15
+ type: string;
16
+ description: string;
17
+ default: string;
18
+ [key: string]: any;
19
+ };
20
+ type serviceParams = Record<string, serviceParam>;
21
+ /**
22
+ * 一方化场景下,由于 onex 会对请求的响应做处理
23
+ * 1. 将 Response & Request 中的参数字段会变更为小驼峰写法
24
+ * onex 相关代码 : http://gitlab.alipay-inc.com/one-console/sdk/blob/master/src/request.ts#L110
25
+ * 2. 另外要注意:
26
+ * op 返回的数据,请求参数的类型格式 需要做额外的处理
27
+ * - (name) key.n, (type) string ==> key: string []
28
+ * - (name) key.m, (type) string ===> key: string []
29
+ * - (name) key.key1 , (type) string ==> key: {key1:string}
30
+ * - (name) key.n.key1 ,(type) string => key:{ key1 :string}[]
31
+ * - (name) key.n.key1.m,(type) string ==> key:{key1: string[]}[]
32
+ */
33
+ export declare function formatParamsForYFH(params: serviceParams, paramsObject?: serviceParams): serviceParams;
34
+ export declare const stripDot: (str: string) => string;
35
+ export {};
package/package.json CHANGED
@@ -1,8 +1,17 @@
1
1
  {
2
2
  "name": "@dazhicheng/openapi",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "根据 Swagger/OpenAPI 文档自动生成 TypeScript 接口",
5
5
  "type": "module",
6
+ "main": "./src/index.ts",
7
+ "module": "./src/index.ts",
8
+ "types": "./src/index.ts",
9
+ "exports": {
10
+ ".": "./src/index.ts"
11
+ },
12
+ "bin": {
13
+ "openapi2ts": "./src/cli.ts"
14
+ },
6
15
  "keywords": [
7
16
  "openapi",
8
17
  "swagger",
@@ -11,14 +20,42 @@
11
20
  ],
12
21
  "author": "",
13
22
  "license": "ISC",
23
+ "buildOptions": {
24
+ "name": "TtMonorepoOpenapi",
25
+ "formats": [
26
+ "esm"
27
+ ]
28
+ },
14
29
  "devDependencies": {
30
+ "@types/glob": "^9.0.0",
31
+ "@types/lodash": "4.17.23",
32
+ "@types/memoizee": "^0.4.12",
33
+ "@types/mockjs": "^1.0.10",
15
34
  "@types/node": "^24.5.2",
35
+ "@types/node-fetch": "^2.6.13",
36
+ "@types/number-to-words": "^1.2.3",
37
+ "@types/nunjucks": "^3.2.6",
38
+ "@types/rimraf": "^4.0.5",
39
+ "@types/swagger2openapi": "^7.0.4",
16
40
  "typescript": "^5.9.2"
17
41
  },
18
- "paths": {},
19
42
  "dependencies": {
20
- "tsx": "^4.19.4",
21
- "prettier": "^3.6.2"
43
+ "chalk": "^4.1.2",
44
+ "cosmiconfig": "^9.0.0",
45
+ "glob": "^7.2.3",
46
+ "lodash-es": "^4.17.21",
47
+ "memoizee": "^0.4.17",
48
+ "mockjs": "^1.1.0",
49
+ "node-fetch": "^2.7.0",
50
+ "number-to-words": "^1.2.4",
51
+ "nunjucks": "^3.2.4",
52
+ "openapi3-ts": "^2.0.2",
53
+ "prettier": "^3.6.2",
54
+ "reserved-words": "^0.1.2",
55
+ "rimraf": "^3.0.2",
56
+ "swagger2openapi": "^7.0.8",
57
+ "tiny-pinyin": "^1.3.2",
58
+ "tsx": "^4.19.4"
22
59
  },
23
60
  "scripts": {
24
61
  "generate": "tsx src/cli.ts"