@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.
package/src/cli.ts CHANGED
@@ -1,28 +1,28 @@
1
- #!/usr/bin/env node
2
-
3
- import chalk from "chalk";
4
- import { cosmiconfigSync } from "cosmiconfig";
5
- import { generateService, GenerateServiceProps } from "./index";
6
-
7
- const explorerSync = cosmiconfigSync("openapi2ts");
8
- const searchedFor = explorerSync.search();
9
-
10
- async function run() {
11
- try {
12
- if (searchedFor?.config) {
13
- const configs: GenerateServiceProps[] = Array.isArray(searchedFor.config)
14
- ? searchedFor.config
15
- : [searchedFor.config];
16
-
17
- for (const config of configs) {
18
- await generateService(config);
19
- }
20
- } else {
21
- throw new Error("config is not found");
22
- }
23
- } catch (error) {
24
- console.log(chalk.red(error));
25
- }
26
- }
27
-
28
- run();
1
+ #!/usr/bin/env node
2
+
3
+ import chalk from "chalk";
4
+ import { cosmiconfigSync } from "cosmiconfig";
5
+ import { ttOpenAPI, GenerateServiceProps } from "./index";
6
+
7
+ const explorerSync = cosmiconfigSync("openapi2ts");
8
+ const searchedFor = explorerSync.search();
9
+
10
+ async function run() {
11
+ try {
12
+ if (searchedFor?.config) {
13
+ const configs: GenerateServiceProps[] = Array.isArray(searchedFor.config)
14
+ ? searchedFor.config
15
+ : [searchedFor.config];
16
+
17
+ for (const config of configs) {
18
+ await ttOpenAPI(config);
19
+ }
20
+ } else {
21
+ throw new Error("config is not found");
22
+ }
23
+ } catch (error) {
24
+ console.log(chalk.red(error));
25
+ }
26
+ }
27
+
28
+ run();
package/src/index.d.ts ADDED
@@ -0,0 +1,141 @@
1
+ import type { OpenAPIObject, OperationObject, SchemaObject } from "openapi3-ts";
2
+ import type { APIDataType } from "./serviceGenerator";
3
+ export type GenerateServiceProps = {
4
+ requestLibPath?: string;
5
+ requestOptionsType?: string;
6
+ requestImportStatement?: string;
7
+ declareType?: "type" | "interface";
8
+ /**
9
+ * api 的前缀
10
+ */
11
+ apiPrefix?: string | ((params: {
12
+ path: string;
13
+ method: string;
14
+ namespace: string;
15
+ functionName: string;
16
+ autoExclude?: boolean;
17
+ }) => string);
18
+ /**
19
+ * 生成的文件夹的路径
20
+ */
21
+ serversPath?: string;
22
+ /**
23
+ * Swagger 2.0 或 OpenAPI 3.0 的地址
24
+ */
25
+ schemaPath?: string;
26
+ /**
27
+ * 项目名称
28
+ */
29
+ projectName?: string;
30
+ /**
31
+ * 文档登录凭证
32
+ */
33
+ authorization?: string;
34
+ hook?: {
35
+ /** change open api data after constructor */
36
+ afterOpenApiDataInited?: (openAPIData: OpenAPIObject) => OpenAPIObject;
37
+ /** 自定义函数名称 */
38
+ customFunctionName?: (data: APIDataType) => string;
39
+ /** 自定义类型名称 */
40
+ customTypeName?: (data: APIDataType) => string;
41
+ /** 自定义 options 默认值 */
42
+ customOptionsDefaultValue?: (data: OperationObject) => Record<string, any> | undefined;
43
+ /** 自定义类名 */
44
+ customClassName?: (tagName: string) => string;
45
+ /**
46
+ * 自定义获取type hook
47
+ * 返回非字符串将使用默认方法获取type
48
+ * @example set number to string
49
+ * function customType(schemaObject,namespace){
50
+ * if(schemaObject.type==='number' && !schemaObject.format){
51
+ * return 'BigDecimalString';
52
+ * }
53
+ * }
54
+ */
55
+ customType?: (schemaObject: SchemaObject | undefined, namespace: string, originGetType: (schemaObject: SchemaObject | undefined, namespace: string) => string) => string;
56
+ /**
57
+ * 自定义生成文件名,可返回多个,表示生成多个文件
58
+ * 返回为空,则使用默认的获取方法获取
59
+ * @example 使用operationId生成文件名
60
+ * function customFileNames(operationObject,apiPath){
61
+ * const operationId=operationObject.operationId;
62
+ * if (!operationId) {
63
+ * console.warn('[Warning] no operationId', apiPath);
64
+ * return;
65
+ * }
66
+ * const res = operationId.split('_');
67
+ * if (res.length > 1) {
68
+ * res.shift();
69
+ * if (res.length > 2) {
70
+ * console.warn('[Warning] operationId has more than 2 part', apiPath);
71
+ * }
72
+ * return [res.join('_')];
73
+ * } else {
74
+ * const controllerName = (res || [])[0];
75
+ * if (controllerName) {
76
+ * return [controllerName];
77
+ * }
78
+ * return;
79
+ * }
80
+ * }
81
+ */
82
+ customFileNames?: (operationObject: OperationObject, apiPath: string, _apiMethod: string) => string[];
83
+ };
84
+ namespace?: string;
85
+ /**
86
+ * 默认为false,true时使用null代替可选
87
+ */
88
+ nullable?: boolean;
89
+ mockFolder?: string;
90
+ /**
91
+ * 模板文件的文件路径
92
+ */
93
+ templatesFolder?: string;
94
+ /**
95
+ * 枚举样式
96
+ */
97
+ enumStyle?: "string-literal" | "enum";
98
+ /**
99
+ * response中数据字段
100
+ * example: ['result', 'res']
101
+ */
102
+ dataFields?: string[];
103
+ /**
104
+ * 模板文件、请求函数采用小驼峰命名
105
+ */
106
+ isCamelCase?: boolean;
107
+ /**
108
+ * 指定生成的接口路径列表
109
+ * 传入则只生成这些接口,不传则全量生成
110
+ * @example specifiedPaths: ["/user/list", "/user/detail"]
111
+ */
112
+ specifiedPaths?: string[];
113
+ /**
114
+ * mock配置
115
+ */
116
+ mockConfig?: {
117
+ /**
118
+ * msw类型mock文件格式. 直接返回对象
119
+ * 举例:
120
+ * // @ts-ignore
121
+
122
+ export default {
123
+ 'DELETE /mydata/delete': { message: { message: 'Mydata successfully deleted' } },
124
+ };
125
+
126
+
127
+ 原文件:
128
+ // @ts-ignore
129
+ import { Request, Response } from 'express';
130
+
131
+ export default {
132
+ 'DELETE /mydata/delete': (req: Request, res: Response) => {
133
+ res.status(200).send({ message: { message: 'Mydata successfully deleted' } });
134
+ },
135
+ };
136
+ */
137
+ msw?: boolean;
138
+ };
139
+ };
140
+ export declare const getSchema: (schemaPath: string, authorization?: string) => Promise<any>;
141
+ export declare const ttOpenAPI: (configs: GenerateServiceProps[] | GenerateServiceProps) => Promise<void>;