openapi.config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { generateService } from '@umijs/openapi';
  2. import type { RequestOptions } from './src/utils/request';
  3. const re = /controller[-_ .](\w)/gi;
  4. // swagger-typescript-api
  5. generateService({
  6. schemaPath: 'http://127.0.0.1:7501/api-docs-json',
  7. serversPath: './src/api/backend',
  8. requestOptionsType: 'RequestOptions',
  9. // 自定义网络请求函数路径
  10. requestImportStatement: `
  11. /**
  12. * 该文件为 @umijs/openapi 插件自动生成,请勿随意修改。如需修改请通过配置 openapi.config.ts 进行定制化。
  13. * */
  14. import { request, type RequestOptions } from "@/utils/request";
  15. `,
  16. hook: {
  17. afterOpenApiDataInited(openAPIData) {
  18. const schemas = openAPIData.components?.schemas;
  19. if (schemas) {
  20. Object.values(schemas).forEach((schema) => {
  21. if ('$ref' in schema) {
  22. return;
  23. }
  24. if (schema.properties) {
  25. Object.values(schema.properties).forEach((prop) => {
  26. if ('$ref' in prop) {
  27. return;
  28. }
  29. // 匡正文件上传的参数类型
  30. if (prop.format === 'binary') {
  31. prop.type = 'object';
  32. }
  33. });
  34. }
  35. });
  36. }
  37. Object.keys(openAPIData.paths).forEach((path) => {
  38. // 屏蔽特定接口生成
  39. if (path.startsWith('/api/forward') || path.startsWith('/api/client')) {
  40. // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
  41. delete openAPIData.paths[path];
  42. }
  43. });
  44. return openAPIData;
  45. },
  46. // @ts-ignore
  47. customFunctionName(operationObject, apiPath) {
  48. const { operationId } = operationObject;
  49. if (!operationId) {
  50. console.warn('[Warning] no operationId', apiPath);
  51. return;
  52. }
  53. const funcName = operationId.replace(re, (_all, letter) => letter.toUpperCase());
  54. operationObject.operationId = funcName;
  55. return funcName;
  56. },
  57. // @ts-ignore
  58. customFileNames(operationObject, apiPath) {
  59. const { operationId } = operationObject;
  60. if (!operationId) {
  61. console.warn('[Warning] no operationId', apiPath);
  62. return;
  63. }
  64. const controllerName = operationId.split(re)[0];
  65. const moduleName = operationObject.tags?.[0].split(' - ')[0];
  66. // 移除 query 参数的默认值
  67. operationObject.parameters?.forEach((param) => {
  68. if ('in' in param && param.in === 'query' && param.schema) {
  69. if (!('$ref' in param.schema) && param.schema.default) {
  70. Reflect.deleteProperty(param.schema, 'default');
  71. }
  72. }
  73. });
  74. if (moduleName === controllerName) {
  75. return [controllerName];
  76. } else if (moduleName && moduleName !== controllerName) {
  77. return [`${moduleName}_${controllerName}`];
  78. }
  79. return;
  80. },
  81. customType(schemaObject, namespace, defaultGetType) {
  82. const type = defaultGetType(schemaObject, namespace);
  83. // 提取出 data 的类型
  84. const regex = /API\.ResOp & { 'data'\?: (.+); }/;
  85. return type.replace(regex, '$1');
  86. },
  87. customOptionsDefaultValue(data): RequestOptions {
  88. const { summary } = data;
  89. if (summary?.startsWith('创建') || summary?.startsWith('新增')) {
  90. return { successMsg: '创建成功' };
  91. } else if (summary?.startsWith('更新')) {
  92. return { successMsg: '更新成功' };
  93. } else if (summary?.startsWith('删除')) {
  94. return { successMsg: '删除成功' };
  95. }
  96. return {};
  97. },
  98. },
  99. });