12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import { ChangeCase, defineConfig, Interface } from "yapi-to-typescript";
- export default defineConfig([
- {
- serverUrl: "https://yapi.macrocura.com/",
- // 是否只生成接口请求内容和返回内容的 TypeSript 类型,是则请求文件和请求函数都不会生成。
- typesOnly: true,
- target: "typescript",
- reactHooks: {
- enabled: false,
- },
- // 接口输出路径
- outputFilePath: (
- interfaceInfo: Interface & { _project: { group_id?: string } },
- changeCase
- ) => {
- const regex = /cat_(\d+)/;
- const match = interfaceInfo._category._url.match(regex);
- let id = "0";
- if (match) {
- id = match[1];
- }
- // 结构:types/分组id/接口分类id.ts
- return `types/${interfaceInfo._project.group_id ?? "public"}/${
- interfaceInfo._project._id
- }/cat_${id}.ts`;
- },
- requestFunctionFilePath: "src/api/request.ts",
- dataKey: ["data"],
- comment: {
- extraTags: (ii) => {
- return [];
- },
- },
- projects: [
- {
- // 项目Token yapi->项目->设置->token
- token:"项目token",
- categories: [
- {
- // 分类 ID,可以设置多个。设为 0 时表示全部分类。
- // 如果需要获取全部分类,同时排除指定分类,可以这样:[0, -20, -21],分类 ID 前面的负号表示排除。
- id: [0],
- getRequestFunctionName(interfaceInfo, changeCase) {
- // 以接口全路径生成请求函数名
- return changeCase.camelCase(
- interfaceInfo.path.replace("/api/", "I/")
- );
- // 若生成的请求函数名存在语法关键词报错、或想通过某个关键词触发 IDE 自动引入提示,可考虑加前缀,如:
- // return changeCase.camelCase(`api_${interfaceInfo.path}`)
- // 若生成的请求函数名有重复报错,可考虑将接口请求方式纳入生成条件,如:
- // return changeCase.camelCase(`${interfaceInfo.method}_${interfaceInfo.path}`)
- },
- },
- ],
- },
- ],
- },
- ]);
|