ytt.config.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { defineConfig } from 'yapi-to-typescript';
  2. interface IConfig {
  3. ids: number[]; // 分类 ids,可以将多个分类中的接口信息生成到同一个 ts 文件中
  4. name: string; // 输出的 ts 文件存储模块文件夹名称,对应的文件夹名称为 src/ytt-type/[name]
  5. token?: string; // 项目 Token, 如果不填就用公共的 Token
  6. }
  7. const TOKEN = 'xxx';
  8. const config: IConfig[] = [
  9. { ids: [11474], name: 'assistant' },
  10. { ids: [11476], name: 'threads' },
  11. { ids: [11478], name: 'messages' },
  12. { ids: [11480], name: 'runs' },
  13. { ids: [11482], name: 'files' }
  14. ];
  15. const createCategories = function (ids: number[]) {
  16. return ids.map((id) => ({
  17. id,
  18. // 自定义ts中interface名称生成规则
  19. getRequestFunctionName(interfaceInfo: any, changeCase: any) {
  20. // path肯定是唯一的
  21. const list = interfaceInfo.path.split('/') as string[];
  22. // 添加method用于区分post,get,put请求可能path相同
  23. const firstWord = list[0].toLowerCase();
  24. if (!/^get|^post|^put|^delete/.test(firstWord)) {
  25. list.unshift(interfaceInfo.method);
  26. }
  27. return changeCase.camelCase(list.join(' '));
  28. }
  29. }));
  30. };
  31. export default defineConfig(
  32. config.map((item) => ({
  33. // 1. 此处配置yapi的访问地址
  34. serverUrl: 'https://yapi.com',
  35. typesOnly: false,
  36. target: 'typescript',
  37. reactHooks: {
  38. enabled: false
  39. },
  40. prodEnvName: 'production',
  41. outputFilePath: `src/ytt-type/${item.name}/index.ts`,
  42. requestFunctionFilePath: `src/ytt-type/${item.name}/request.ts`,
  43. // dataKey: 'data',
  44. projects: [
  45. {
  46. // 2. 此处配置yapi项目的访问token
  47. token: item.token ?? TOKEN,
  48. categories: createCategories(item.ids)
  49. }
  50. ]
  51. }))
  52. );