import { defineConfig, ExtendedInterface, ChangeCase } from 'yapi-to-typescript'; const questionMarkReg = /\?.*$/; // 匹配结果 问号 后面的字符 const yapiFolderName = 'yapi'; // api 格式化 去除共同的字段 const handleFunctionName = (interfaceInfo: ExtendedInterface, changeCase: ChangeCase, regexpStr: string): string => { const reg = new RegExp(regexpStr); const newPath = interfaceInfo.path.replace(reg, '').replace(questionMarkReg, '').replace(/\//g, '_'); return changeCase.camelCase(`${interfaceInfo.method}_${newPath}`); }; export default defineConfig([ { serverUrl: 'https://yapi.toc.test.shopee.io/', // 服务器地址。 typesOnly: false, // 是否只生成接口请求内容和返回内容的 TypeScript 类型,是则请求文件和请求函数都不会生成。 target: 'typescript', // 要生成的目标代码类型。 reactHooks: { // 支持生成 React Hooks 代码的相关配置。 enabled: true, requestHookMakerFilePath: `src/${yapiFolderName}/make-request-hook.ts`, }, devEnvName: 'dev', // 测试环境名称。用于获取测试环境域名。 prodEnvName: 'prod', // 生产环境名称。用于获取生产环境域名。 // outputFilePath: 'src/api/index.ts', // 输出文件路径。可以是 相对路径 或 绝对路径。如 'src/api/index.ts'。 outputFilePath: (interfaceInfo, changeCase) => { return `src/${yapiFolderName}/${changeCase.paramCase(interfaceInfo._project.name)}/${changeCase.paramCase( interfaceInfo._category.name )}.ts`; }, requestFunctionFilePath: `src/${yapiFolderName}/request.ts`, // 请求函数文件路径。如 'src/api/request.ts'。 dataKey: ['data', 'code', 'msg'], projects: [ { token: '360253d81108de2d0fa3903adb7b0d2c1f77d93c34d8c98b693e8e76e60a90cf', // op 项目 token categories: [ { id: 0, // 分类 ID,可以设置多个。设为 0 时表示全部分类。 getRequestFunctionName: (...args) => { if(!new RegExp('^.*(operation)/api/v1/').test(args[0].path)){ args[0].path = `/operation/api/v1${args[0].path}`; } return handleFunctionName(...args, '^.*[operation]/api/v1/'); }, }, ], }, { token: 'fc00368a72c38886dfa8434f957f1c30ef5c892d1ea8b8a9635686aea502a425', // chatbot 主项目 token categories: [ { id: 0, // 分类 ID,可以设置多个。设为 0 时表示全部分类。 getRequestFunctionName: (...args) => { // 有的接口路径时 /apiName 而不是 chatbot/api/v1/apiName if(!new RegExp('^.*(chatbot|chat)/api/v[12]/').test(args[0].path)){ args[0].path = `/chatbot/api/v1${args[0].path}`; } return handleFunctionName(...args, '^.*(chatbot|chat)/api/v[12]/'); }, }, ], }, ], }, ]);