openapi-ts-request 1.3.1 → 1.3.2

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/README.md CHANGED
@@ -264,17 +264,22 @@ openapi --i ./spec.json --o ./apis
264
264
  | customClassName | (tagName: string) => string | 自定义标签名 |
265
265
  | customType | ({<br>schemaObject: SchemaObject \| ReferenceObject,<br>namespace: string,<br>originGetType:(schemaObject: SchemaObject \| ReferenceObject, namespace: string, schemas?: ComponentsObject['schemas']) => string,<br>schemas?: ComponentsObject['schemas'],<br>}) => string | 自定义类型 <br> _返回非字符串将使用默认方法获取type_ |
266
266
  | customFileNames | (<br>operationObject: OperationObject,<br>apiPath: string,<br>apiMethod: string,<br>) => string[] | 自定义生成的请求客户端文件名称,可以返回多个文件名称的数组(表示生成多个文件). <br> _返回为空,则使用默认的方法获取_ |
267
+ | customTemplates | {<br>[TypescriptFileType.serviceController]?: <T, U>(item: T, context: U) => string;<br>} | 自定义模板,详情请看源码 |
267
268
 
268
269
  ## Apifox-Config
269
270
 
270
271
  | 属性 | 类型 | 说明 | 必填 |
271
272
  | --- | --- | --- | --- |
272
273
  | projectId | string | 项目id | true |
273
- | local | string | 语言(默认:zh-CN) | false |
274
+ | apifoxToken | string | [获取](https://docs.apifox.com/doc-5723694) | true |
275
+ | local | string | 语言(默认: zh-CN) | false |
274
276
  | apifoxVersion | string | 默认: 2024-03-28, [获取当前版本](https://api.apifox.com/v1/versions) | false |
275
277
  | includeTags | \* 或 string[] | 默认: \* | false |
276
278
  | excludeTags | string[] | 默认: [] | false |
277
- | apifoxToken | string | [获取](https://docs.apifox.com/doc-5723694) | true |
279
+ | oasVersion | string | 指定用于导出的 OpenAPI 规范的版本,可以有值如 "2.0"、"3.0"、"3.1" | '3.0' |
280
+ | exportFormat | string | 指定导出的 OpenAPI 文件的格式,可以有值如 'JSON' 或 'YAML' | 'JSON' |
281
+ | includeApifoxExtensionProperties | boolean | 指定是否包含 Apifox 的 OpenAPI 规范扩展字段 `x-apifox` | false |
282
+ | addFoldersToTags | boolean | 指定是否在标签字段中包含接口的目录名称 | false |
278
283
 
279
284
  ## JSON Schemas
280
285
 
@@ -183,9 +183,21 @@ class ServiceGenerator {
183
183
  const prettierError = [];
184
184
  // 生成 service controller 文件
185
185
  this.getServiceTPConfigs().forEach((tp) => {
186
+ var _a, _b;
187
+ const { list } = tp, restTp = tslib_1.__rest(tp, ["list"]);
188
+ const payload = Object.assign({ namespace: this.config.namespace, requestOptionsType: this.config.requestOptionsType, requestImportStatement: this.config.requestImportStatement, interfaceFileName: config_2.interfaceFileName, list }, restTp);
189
+ const hookCustomTemplateService = (_b = (_a = this.config.hook) === null || _a === void 0 ? void 0 : _a.customTemplates) === null || _b === void 0 ? void 0 : _b[config_2.TypescriptFileType.serviceController];
190
+ if (hookCustomTemplateService) {
191
+ payload.list = list.map((item) => {
192
+ return {
193
+ customTemplate: true,
194
+ data: hookCustomTemplateService(item, payload),
195
+ };
196
+ });
197
+ }
186
198
  const hasError = this.genFileFromTemplate(isGenJavaScript
187
199
  ? (0, util_2.getFinalFileName)(`${tp.className}.js`)
188
- : (0, util_2.getFinalFileName)(`${tp.className}.ts`), config_2.TypescriptFileType.serviceController, Object.assign({ namespace: this.config.namespace, requestOptionsType: this.config.requestOptionsType, requestImportStatement: this.config.requestImportStatement, interfaceFileName: config_2.interfaceFileName }, tp));
200
+ : (0, util_2.getFinalFileName)(`${tp.className}.ts`), config_2.TypescriptFileType.serviceController, payload);
189
201
  prettierError.push(hasError);
190
202
  if (this.config.isGenReactQuery) {
191
203
  this.genFileFromTemplate(isGenJavaScript
@@ -50,4 +50,14 @@ type MergerOptionProps = {
50
50
  projectOptions: ProjectOptions;
51
51
  };
52
52
  export type MergerOptions = MergeOption & Partial<MergerOptionProps>;
53
+ export type IServiceControllerPayload<T> = {
54
+ namespace: string;
55
+ requestOptionsType: string;
56
+ requestImportStatement: string;
57
+ interfaceFileName: string;
58
+ list: {
59
+ customTemplate: boolean;
60
+ data: string;
61
+ }[] | T;
62
+ };
53
63
  export {};
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { TypescriptFileType } from './generator/config';
1
2
  import { APIDataType } from './generator/type';
2
3
  import { ComponentsObject, type GetSchemaByApifoxProps, IPriorityRule, IReactQueryMode, OpenAPIObject, OperationObject, ReferenceObject, SchemaObject } from './type';
3
4
  export * from './generator/patchSchema';
@@ -194,6 +195,15 @@ export type GenerateServiceProps = {
194
195
  * }
195
196
  */
196
197
  customFileNames?: (operationObject: OperationObject, apiPath: string, apiMethod: string) => string[] | null;
198
+ /**
199
+ * 自定义模板
200
+ */
201
+ customTemplates?: {
202
+ /**
203
+ * 自定义 serviceController 模板
204
+ */
205
+ [TypescriptFileType.serviceController]?: <T, U>(item: T, context: U) => string;
206
+ };
197
207
  };
198
208
  };
199
209
  export declare function generateService({ requestLibPath, schemaPath, mockFolder, includeTags, excludeTags, authorization, isTranslateToEnglishTag, priorityRule, timeout, reactQueryMode, apifoxConfig, ...rest }: GenerateServiceProps): Promise<void>;
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ exports.generateService = generateService;
4
4
  const tslib_1 = require("tslib");
5
5
  const lodash_1 = require("lodash");
6
6
  const config_1 = require("./config");
7
+ const config_2 = require("./generator/config");
7
8
  const mockGenarator_1 = require("./generator/mockGenarator");
8
9
  const serviceGenarator_1 = tslib_1.__importDefault(require("./generator/serviceGenarator"));
9
10
  const util_1 = require("./util");
@@ -22,7 +22,6 @@ const readConfig = (_a) => tslib_1.__awaiter(void 0, [_a], void 0, function* ({
22
22
  else {
23
23
  return (yield ((_c = explorerSync.search()) === null || _c === void 0 ? void 0 : _c.config));
24
24
  }
25
- // eslint-disable-next-line no-unused-vars
26
25
  }
27
26
  catch (error) {
28
27
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "Swagger2/OpenAPI3/Apifox to TypeScript/JavaScript, request client(support any client), request mock service, enum and enum translation, react-query/vue-query, type field label, JSON Schemas",
5
5
  "engines": {
6
6
  "node": ">=18.0.0",
@@ -8,166 +8,169 @@
8
8
  {% endif %}
9
9
 
10
10
  {% for api in list -%}
11
- /** {{ api.desc if api.desc else '此处后端没有提供注释' }} {{ api.method | upper }} {{ api.pathInComment | safe }}{{ ' ' if api.apifoxRunLink else '' }}{{ api.apifoxRunLink }} */
12
- export async function {{ api.functionName }}({
13
- {%- if api.params and api.hasParams %}
14
- params
15
- {%- if api.hasParams -%}
16
- {{ "," if api.body or api.file }}
17
- {%- endif -%}
18
- {%- endif -%}
19
- {%- if api.body -%}
20
- body
21
- {{ "," if api.file }}
22
- {%- endif %}
23
- {%- if api.file -%}
24
- {%- for file in api.file -%}
25
- {{ file.title | safe }}
26
- {{ "," if not loop.last }}
27
- {%- endfor -%}
28
- {%- endif -%}
29
- {{ "," if api.body or api.hasParams or api.file }}
30
- options
31
- }
32
- {%- if genType === "ts" -%}
33
- : {
11
+ {% if api.customTemplate %}
12
+ {{ api.data }}
13
+ {% else %}
14
+ /** {{ api.desc if api.desc else '此处后端没有提供注释' }} {{ api.method | upper }} {{ api.pathInComment | safe }}{{ ' ' if api.apifoxRunLink else '' }}{{ api.apifoxRunLink }} */
15
+ export async function {{ api.functionName }}({
34
16
  {%- if api.params and api.hasParams %}
35
- // 叠加生成的Param类型 (非body参数openapi默认没有生成对象)
36
- params: {{ namespace }}.{{ api.typeName }}
37
- {# header 入参 -#}
38
- {% if api.params.header -%}
39
- & { // header
40
- {% for param in api.params.header -%}
41
- {% if param.description -%}
42
- /** {{ param.description }} */
43
- {% endif -%}
44
- '{{ param.name }}'
45
- {{- "?" if not param.required }}
46
- {{- (": " + param.type + ";") | safe }}
47
- {% endfor -%}
48
- }
49
- {%- endif -%}
17
+ params
50
18
  {%- if api.hasParams -%}
51
- {{ ";" if api.body or api.file }}
19
+ {{ "," if api.body or api.file }}
52
20
  {%- endif -%}
53
21
  {%- endif -%}
54
-
55
22
  {%- if api.body -%}
56
- body:
57
- {% if api.body.propertiesList %}
58
- {
59
- {%- for prop in api.body.propertiesList %}
60
- {% if prop.schema.description -%}
61
- /** {{ prop.schema.description }} */
62
- {% endif -%}
63
- '{{ prop.key }}'{{ "?" if not prop.schema.required }}: {{ prop.schema.type }},
64
- {%- endfor %}
65
- }
66
- {%- else -%}
67
- {{ api.body.type }}
68
- {%- endif -%}
69
- {{ ";" if api.file }}
23
+ body
24
+ {{ "," if api.file }}
70
25
  {%- endif %}
71
-
72
26
  {%- if api.file -%}
73
27
  {%- for file in api.file -%}
74
28
  {{ file.title | safe }}
75
- {{- "?" if not api.file.required -}}
76
- : File {{ "[]" if file.multiple }}
77
- {{ ";" if not loop.last }}
29
+ {{ "," if not loop.last }}
78
30
  {%- endfor -%}
79
31
  {%- endif -%}
80
- {{ ";" if api.body or api.hasParams or api.file }}
81
- options?: {{ requestOptionsType }}
32
+ {{ "," if api.body or api.hasParams or api.file }}
33
+ options
82
34
  }
83
- {%- endif -%}
84
- )
85
- {
86
- {% if api.params and api.params.path %}
87
- const { {% for param in api.params.path %}'{{ param.name }}': {{ param.alias }}, {% endfor %}
88
- {% if api.params.path -%}
89
- ...queryParams
90
- {% endif -%}
91
- } = params;
92
- {% endif %}
93
- {%- if api.hasFormData -%}
94
- const formData = new FormData();
35
+ {%- if genType === "ts" -%}
36
+ : {
37
+ {%- if api.params and api.hasParams %}
38
+ // 叠加生成的Param类型 (非body参数openapi默认没有生成对象)
39
+ params: {{ namespace }}.{{ api.typeName }}
40
+ {# header 入参 -#}
41
+ {% if api.params.header -%}
42
+ & { // header
43
+ {% for param in api.params.header -%}
44
+ {% if param.description -%}
45
+ /** {{ param.description }} */
46
+ {% endif -%}
47
+ '{{ param.name }}'
48
+ {{- "?" if not param.required }}
49
+ {{- (": " + param.type + ";") | safe }}
50
+ {% endfor -%}
51
+ }
52
+ {%- endif -%}
53
+ {%- if api.hasParams -%}
54
+ {{ ";" if api.body or api.file }}
55
+ {%- endif -%}
56
+ {%- endif -%}
57
+
58
+ {%- if api.body -%}
59
+ body:
60
+ {% if api.body.propertiesList %}
61
+ {
62
+ {%- for prop in api.body.propertiesList %}
63
+ {% if prop.schema.description -%}
64
+ /** {{ prop.schema.description }} */
65
+ {% endif -%}
66
+ '{{ prop.key }}'{{ "?" if not prop.schema.required }}: {{ prop.schema.type }},
67
+ {%- endfor %}
68
+ }
69
+ {%- else -%}
70
+ {{ api.body.type }}
71
+ {%- endif -%}
72
+ {{ ";" if api.file }}
73
+ {%- endif %}
74
+
75
+ {%- if api.file -%}
76
+ {%- for file in api.file -%}
77
+ {{ file.title | safe }}
78
+ {{- "?" if not api.file.required -}}
79
+ : File {{ "[]" if file.multiple }}
80
+ {{ ";" if not loop.last }}
81
+ {%- endfor -%}
82
+ {%- endif -%}
83
+ {{ ";" if api.body or api.hasParams or api.file }}
84
+ options?: {{ requestOptionsType }}
85
+ }
86
+ {%- endif -%}
87
+ )
88
+ {
89
+ {% if api.params and api.params.path %}
90
+ const { {% for param in api.params.path %}'{{ param.name }}': {{ param.alias }}, {% endfor %}
91
+ {% if api.params.path -%}
92
+ ...queryParams
93
+ {% endif -%}
94
+ } = params;
95
+ {% endif %}
96
+ {%- if api.hasFormData -%}
97
+ const formData = new FormData();
98
+
99
+ {% if api.file -%}
100
+ {% for file in api.file %}
101
+ if({{ file.title | safe }}) {
102
+ {% if file.multiple %}
103
+ {{ file.title | safe }}.forEach(f => formData.append('{{ file.title | safe }}', f || ''));
104
+ {% else %}
105
+ formData.append('{{ file.title | safe }}', {{ file.title | safe }})
106
+ {% endif %}
107
+ }
108
+ {% endfor %}
109
+ {%- endif -%}
95
110
 
96
- {% if api.file -%}
97
- {% for file in api.file %}
98
- if({{ file.title | safe }}) {
99
- {% if file.multiple %}
100
- {{ file.title | safe }}.forEach(f => formData.append('{{ file.title | safe }}', f || ''));
111
+ {% if api.body %}
112
+ Object.keys(body).forEach(ele => {
113
+ {% if genType === "ts" %}
114
+ const item = (body as { [key: string]: any })[ele];
101
115
  {% else %}
102
- formData.append('{{ file.title | safe }}', {{ file.title | safe }})
116
+ const item = body[ele];
103
117
  {% endif %}
104
- }
105
- {% endfor %}
106
- {%- endif -%}
107
-
108
- {% if api.body %}
109
- Object.keys(body).forEach(ele => {
110
- {% if genType === "ts" %}
111
- const item = (body as { [key: string]: any })[ele];
112
- {% else %}
113
- const item = body[ele];
114
- {% endif %}
115
- if (item !== undefined && item !== null) {
116
- {% if genType === "ts" %}
117
- if (typeof item === 'object' && !(item instanceof File)) {
118
- if (item instanceof Array) {
119
- item.forEach((f) => formData.append(ele, f || ''));
118
+ if (item !== undefined && item !== null) {
119
+ {% if genType === "ts" %}
120
+ if (typeof item === 'object' && !(item instanceof File)) {
121
+ if (item instanceof Array) {
122
+ item.forEach((f) => formData.append(ele, f || ''));
123
+ } else {
124
+ formData.append(ele, JSON.stringify(item));
125
+ }
120
126
  } else {
121
- formData.append(ele, JSON.stringify(item));
127
+ formData.append(ele, item);
122
128
  }
123
- } else {
124
- formData.append(ele, item);
125
- }
126
- {% else %}
127
- formData.append(ele, typeof item === 'object' ? JSON.stringify(item) : item);
128
- {% endif %}
129
- }
130
- });
129
+ {% else %}
130
+ formData.append(ele, typeof item === 'object' ? JSON.stringify(item) : item);
131
+ {% endif %}
132
+ }
133
+ });
134
+ {% endif %}
131
135
  {% endif %}
132
- {% endif %}
133
136
 
134
- {% if api.hasPathVariables or api.hasApiPrefix -%}
135
- return request{{ ("<" + api.response.type + ">") | safe if genType === "ts" }}(`{{ api.path | safe }}`, {
136
- {% else -%}
137
- return request{{ ("<" + api.response.type + ">") | safe if genType === "ts" }}('{{ api.path }}', {
138
- {% endif -%}
139
- method: '{{ api.method | upper }}',
140
- {%- if api.hasHeader and api.body.mediaType %}
141
- headers: {
142
- {%- if api.body.mediaType %}
143
- 'Content-Type': '{{ api.body.mediaType | safe }}',
144
- {%- endif %}
145
- },
146
- {%- endif %}
147
- {%- if api.params and api.hasParams %}
148
- params: {
149
- {%- for query in api.params.query %}
150
- {% if query.schema.default -%}
151
- // {{ query.name | safe }} has a default value: {{ query.schema.default | safe }}
152
- '{{ query.name | safe }}': '{{query.schema.default | safe}}',
153
- {%- endif -%}
154
- {%- endfor -%}
155
- ...{{ 'queryParams' if api.params and api.params.path else 'params' }},
156
- {%- for query in api.params.query %}
157
- {%- if query.isComplexType %}
158
- '{{ query.name | safe }}': undefined,
159
- ...{{ 'queryParams' if api.params and api.params.path else 'params' }}['{{ query.name | safe }}'],
137
+ {% if api.hasPathVariables or api.hasApiPrefix -%}
138
+ return request{{ ("<" + api.response.type + ">") | safe if genType === "ts" }}(`{{ api.path | safe }}`, {
139
+ {% else -%}
140
+ return request{{ ("<" + api.response.type + ">") | safe if genType === "ts" }}('{{ api.path }}', {
141
+ {% endif -%}
142
+ method: '{{ api.method | upper }}',
143
+ {%- if api.hasHeader and api.body.mediaType %}
144
+ headers: {
145
+ {%- if api.body.mediaType %}
146
+ 'Content-Type': '{{ api.body.mediaType | safe }}',
160
147
  {%- endif %}
161
- {%- endfor -%}
162
- },
163
- {%- endif %}
164
- {%- if api.hasFormData %}
165
- data: formData,
166
- {%- elseif api.body %}
167
- data: body,
168
- {%- endif %}
169
- ...(options || {{ api.options | dump }}),
170
- });
171
- }
172
-
148
+ },
149
+ {%- endif %}
150
+ {%- if api.params and api.hasParams %}
151
+ params: {
152
+ {%- for query in api.params.query %}
153
+ {% if query.schema.default -%}
154
+ // {{ query.name | safe }} has a default value: {{ query.schema.default | safe }}
155
+ '{{ query.name | safe }}': '{{query.schema.default | safe}}',
156
+ {%- endif -%}
157
+ {%- endfor -%}
158
+ ...{{ 'queryParams' if api.params and api.params.path else 'params' }},
159
+ {%- for query in api.params.query %}
160
+ {%- if query.isComplexType %}
161
+ '{{ query.name | safe }}': undefined,
162
+ ...{{ 'queryParams' if api.params and api.params.path else 'params' }}['{{ query.name | safe }}'],
163
+ {%- endif %}
164
+ {%- endfor -%}
165
+ },
166
+ {%- endif %}
167
+ {%- if api.hasFormData %}
168
+ data: formData,
169
+ {%- elseif api.body %}
170
+ data: body,
171
+ {%- endif %}
172
+ ...(options || {{ api.options | dump }}),
173
+ });
174
+ }
175
+ {% endif %}
173
176
  {% endfor -%}