openapi-ts-request 1.8.4 → 1.9.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/README.md CHANGED
@@ -267,6 +267,7 @@ openapi --i ./spec.json --o ./apis
267
267
  | 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_ |
268
268
  | customFileNames | (<br>operationObject: OperationObject,<br>apiPath: string,<br>apiMethod: string,<br>) => string[] | 自定义生成的请求客户端文件名称,可以返回多个文件名称的数组(表示生成多个文件). <br> _返回为空,则使用默认的方法获取_ |
269
269
  | customTemplates | {<br>[TypescriptFileType.serviceController]?: <T, U>(item: T, context: U) => string;<br>} | 自定义模板,详情请看源码 |
270
+ | customRenderTemplateData | {<br>[TypescriptFileType]?: (list: any[], context: {fileName: string, params: Record<string, unknown>}) => any[]<br>} | 自定义文件生成时的 list 参数处理,支持对不同文件类型进行精细化控制 |
270
271
 
271
272
  ## Apifox-Config
272
273
 
@@ -531,15 +531,75 @@ class ServiceGenerator {
531
531
  .filter((item) => { var _a; return !!((_a = item === null || item === void 0 ? void 0 : item.list) === null || _a === void 0 ? void 0 : _a.length); });
532
532
  }
533
533
  genFileFromTemplate(fileName, type, params) {
534
+ var _a;
534
535
  try {
535
536
  const template = this.getTemplate(type);
537
+ // 应用 customRenderTemplateData hook (如果存在)
538
+ let processedParams = Object.assign({}, params);
539
+ const customListHooks = (_a = this.config.hook) === null || _a === void 0 ? void 0 : _a.customRenderTemplateData;
540
+ if (customListHooks && params.list) {
541
+ try {
542
+ const context = {
543
+ fileName,
544
+ params: processedParams,
545
+ };
546
+ let processedList = params.list;
547
+ // 根据不同的文件类型调用相应的 hook 函数
548
+ switch (type) {
549
+ case config_2.TypescriptFileType.serviceController:
550
+ if (customListHooks.serviceController) {
551
+ processedList = customListHooks.serviceController(params.list, context);
552
+ }
553
+ break;
554
+ case config_2.TypescriptFileType.reactQuery:
555
+ if (customListHooks.reactQuery) {
556
+ processedList = customListHooks.reactQuery(params.list, context);
557
+ }
558
+ break;
559
+ case config_2.TypescriptFileType.interface:
560
+ if (customListHooks.interface) {
561
+ processedList = customListHooks.interface(params.list, context);
562
+ }
563
+ break;
564
+ case config_2.TypescriptFileType.displayEnumLabel:
565
+ if (customListHooks.displayEnumLabel) {
566
+ processedList = customListHooks.displayEnumLabel(params.list, context);
567
+ }
568
+ break;
569
+ case config_2.TypescriptFileType.displayTypeLabel:
570
+ if (customListHooks.displayTypeLabel) {
571
+ processedList = customListHooks.displayTypeLabel(params.list, context);
572
+ }
573
+ break;
574
+ case config_2.TypescriptFileType.schema:
575
+ if (customListHooks.schema) {
576
+ processedList = customListHooks.schema(params.list, context);
577
+ }
578
+ break;
579
+ case config_2.TypescriptFileType.serviceIndex:
580
+ if (customListHooks.serviceIndex) {
581
+ processedList = customListHooks.serviceIndex(params.list, context);
582
+ }
583
+ break;
584
+ }
585
+ if (processedList !== params.list) {
586
+ processedParams = Object.assign(Object.assign({}, processedParams), { list: processedList });
587
+ this.log(`customRenderTemplateData hook applied for ${type}: ${fileName}`);
588
+ }
589
+ }
590
+ catch (error) {
591
+ console.error(`[GenSDK] customRenderTemplateData hook error for ${type}:`, error);
592
+ this.log(`customRenderTemplateData hook failed for ${type}, using original list`);
593
+ // 发生错误时使用原始参数继续执行
594
+ }
595
+ }
536
596
  // 设置输出不转义
537
597
  const env = nunjucks_1.default.configure({
538
598
  autoescape: false,
539
599
  });
540
600
  env.addFilter('capitalizeFirst', util_2.capitalizeFirstLetter);
541
601
  const destPath = (0, path_1.join)(this.config.serversPath, fileName);
542
- const destCode = nunjucks_1.default.renderString(template, Object.assign({ disableTypeCheck: false }, params));
602
+ const destCode = nunjucks_1.default.renderString(template, Object.assign({ disableTypeCheck: false }, processedParams));
543
603
  let mergerProps = {};
544
604
  if ((0, fs_1.existsSync)(destPath)) {
545
605
  mergerProps = {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { TypescriptFileType } from './generator/config';
2
- import type { APIDataType, ITypeItem } from './generator/type';
2
+ import type { APIDataType, ControllerType, ISchemaItem, ITypeItem } from './generator/type';
3
3
  import type { ComponentsObject, IPriorityRule, IReactQueryMode, OpenAPIObject, OperationObject, ReferenceObject, SchemaObject } from './type';
4
4
  import { type GetSchemaByApifoxProps } from './type';
5
5
  export * from './generator/patchSchema';
@@ -217,6 +217,61 @@ export type GenerateServiceProps = {
217
217
  */
218
218
  [TypescriptFileType.displayTypeLabel]?: <T, U>(types: T[], config: U) => ITypeItem[];
219
219
  };
220
+ /**
221
+ * 自定义 genFileFromTemplate 的 list 参数 hook
222
+ * 可以在模板渲染前对 list 参数进行自定义处理
223
+ */
224
+ customRenderTemplateData?: {
225
+ /**
226
+ * 自定义 serviceController 文件的 list 参数
227
+ */
228
+ [TypescriptFileType.serviceController]?: (list: APIDataType[], context: {
229
+ fileName: string;
230
+ params: Record<string, unknown>;
231
+ }) => APIDataType[];
232
+ /**
233
+ * 自定义 interface 文件的 list 参数
234
+ */
235
+ [TypescriptFileType.interface]?: (list: ITypeItem[], context: {
236
+ fileName: string;
237
+ params: Record<string, unknown>;
238
+ }) => ITypeItem[];
239
+ /**
240
+ * 自定义 displayEnumLabel 文件的 list 参数
241
+ */
242
+ [TypescriptFileType.displayEnumLabel]?: (list: ITypeItem[], context: {
243
+ fileName: string;
244
+ params: Record<string, unknown>;
245
+ }) => ITypeItem[];
246
+ /**
247
+ * 自定义 displayTypeLabel 文件的 list 参数
248
+ */
249
+ [TypescriptFileType.displayTypeLabel]?: (list: ITypeItem[], context: {
250
+ fileName: string;
251
+ params: Record<string, unknown>;
252
+ }) => ITypeItem[];
253
+ /**
254
+ * 自定义 schema 文件的 list 参数
255
+ */
256
+ [TypescriptFileType.schema]?: (list: ISchemaItem[], context: {
257
+ fileName: string;
258
+ params: Record<string, unknown>;
259
+ }) => ISchemaItem[];
260
+ /**
261
+ * 自定义 serviceIndex 文件的 list 参数
262
+ */
263
+ [TypescriptFileType.serviceIndex]?: (list: ControllerType[], context: {
264
+ fileName: string;
265
+ params: Record<string, unknown>;
266
+ }) => ControllerType[];
267
+ /**
268
+ * 自定义 reactQuery 文件的 list 参数
269
+ */
270
+ [TypescriptFileType.reactQuery]?: (list: APIDataType[], context: {
271
+ fileName: string;
272
+ params: Record<string, unknown>;
273
+ }) => APIDataType[];
274
+ };
220
275
  };
221
276
  };
222
277
  export declare function generateService({ requestLibPath, schemaPath, mockFolder, includeTags, excludeTags, includePaths, excludePaths, authorization, isTranslateToEnglishTag, priorityRule, timeout, reactQueryMode, apifoxConfig, ...rest }: GenerateServiceProps): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "1.8.4",
3
+ "version": "1.9.0",
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",