ytt.config.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { ChangeCase, defineConfig, Interface } from "yapi-to-typescript";
  2. export default defineConfig([
  3. {
  4. serverUrl: "https://yapi.macrocura.com/",
  5. // 是否只生成接口请求内容和返回内容的 TypeSript 类型,是则请求文件和请求函数都不会生成。
  6. typesOnly: true,
  7. target: "typescript",
  8. reactHooks: {
  9. enabled: false,
  10. },
  11. // 接口输出路径
  12. outputFilePath: (
  13. interfaceInfo: Interface & { _project: { group_id?: string } },
  14. changeCase
  15. ) => {
  16. const regex = /cat_(\d+)/;
  17. const match = interfaceInfo._category._url.match(regex);
  18. let id = "0";
  19. if (match) {
  20. id = match[1];
  21. }
  22. // 结构:types/分组id/接口分类id.ts
  23. return `types/${interfaceInfo._project.group_id ?? "public"}/${
  24. interfaceInfo._project._id
  25. }/cat_${id}.ts`;
  26. },
  27. requestFunctionFilePath: "src/api/request.ts",
  28. dataKey: ["data"],
  29. comment: {
  30. extraTags: (ii) => {
  31. return [];
  32. },
  33. },
  34. projects: [
  35. {
  36. // 项目Token yapi->项目->设置->token
  37. token:"6fc643b80f506ab1b77b12760ce87a4699a60386dc1fb85e3e51b34976c210ed",
  38. categories: [
  39. {
  40. // 分类 ID,可以设置多个。设为 0 时表示全部分类。
  41. // 如果需要获取全部分类,同时排除指定分类,可以这样:[0, -20, -21],分类 ID 前面的负号表示排除。
  42. id: [0],
  43. getRequestFunctionName(interfaceInfo, changeCase) {
  44. // 以接口全路径生成请求函数名
  45. return changeCase.camelCase(
  46. interfaceInfo.path.replace("/api/", "I/")
  47. );
  48. // 若生成的请求函数名存在语法关键词报错、或想通过某个关键词触发 IDE 自动引入提示,可考虑加前缀,如:
  49. // return changeCase.camelCase(`api_${interfaceInfo.path}`)
  50. // 若生成的请求函数名有重复报错,可考虑将接口请求方式纳入生成条件,如:
  51. // return changeCase.camelCase(`${interfaceInfo.method}_${interfaceInfo.path}`)
  52. },
  53. },
  54. ],
  55. },
  56. ],
  57. },
  58. ]);