Skip to content

Commit d30dcd3

Browse files
perf: perf customTemplates code and docs
1 parent e404129 commit d30dcd3

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

README-en_US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ openapi -i ./spec.json -o ./apis
262262
| customClassName | (tagName: string) => string | custom tag name |
263263
| 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 | custom type <br> _returning a non-string will use the default method to get the type_ |
264264
| customFileNames | (<br>operationObject: OperationObject,<br>apiPath: string,<br>apiMethod: string,<br>) => string[] | custom generate request client controller file name, can return multiple: generate multiple files. <br> _if the return value is empty, the default getFileNames is used_ |
265+
| customTemplates | {<br>[TypescriptFileType.serviceController]?: <T, U>(item: T, context: U) => string;<br>} | custom template, details see source code |
265266

266267
## Apifox-Config
267268

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ openapi --i ./spec.json --o ./apis
264264
| customClassName | (tagName: string) => string | 自定义标签名 |
265265
| 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_ |
266266
| 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>} | 自定义模板,详情请看源码 |
267268

268269
## Apifox-Config
269270

src/generator/serviceGenarator.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import {
7070
ICustomSchemaObject,
7171
IPropObject,
7272
ISchemaItem,
73+
IServiceControllerPayload,
7374
ITypeItem,
7475
ITypescriptFileType,
7576
type MergeOption,
@@ -330,27 +331,30 @@ export default class ServiceGenerator {
330331
// 生成 service controller 文件
331332
this.getServiceTPConfigs().forEach((tp) => {
332333
const { list, ...restTp } = tp;
333-
const payload: Record<string, any> = {
334+
const payload: Omit<typeof tp, 'list'> &
335+
IServiceControllerPayload<typeof list> = {
334336
namespace: this.config.namespace,
335337
requestOptionsType: this.config.requestOptionsType,
336338
requestImportStatement: this.config.requestImportStatement,
337339
interfaceFileName: interfaceFileName,
340+
list,
338341
...restTp,
339342
};
340-
341343
const hookCustomTemplateService =
342344
this.config.hook?.customTemplates?.[
343345
TypescriptFileType.serviceController
344346
];
347+
345348
if (hookCustomTemplateService) {
346349
payload.list = list.map((item) => {
347350
return {
348351
customTemplate: true,
349-
data: hookCustomTemplateService(item, payload),
352+
data: hookCustomTemplateService<typeof item, typeof payload>(
353+
item,
354+
payload
355+
),
350356
};
351357
});
352-
} else {
353-
payload.list = list;
354358
}
355359

356360
const hasError = this.genFileFromTemplate(

src/generator/type.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,16 @@ type MergerOptionProps = {
6969
};
7070

7171
export type MergerOptions = MergeOption & Partial<MergerOptionProps>;
72+
73+
export type IServiceControllerPayload<T> = {
74+
namespace: string;
75+
requestOptionsType: string;
76+
requestImportStatement: string;
77+
interfaceFileName: string;
78+
list:
79+
| {
80+
customTemplate: boolean;
81+
data: string;
82+
}[]
83+
| T;
84+
};

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ export type GenerateServiceProps = {
241241
/**
242242
* 自定义 serviceController 模板
243243
*/
244-
[TypescriptFileType.serviceController]?: (item: any, ctx: any) => string;
244+
[TypescriptFileType.serviceController]?: <T, U>(
245+
item: T,
246+
context: U
247+
) => string;
245248
};
246249
};
247250
};

0 commit comments

Comments
 (0)