西瓜° 1 month ago
commit
e2397e3e62
5 changed files with 86 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 8 0
      README.MD
  3. 15 0
      package.json
  4. BIN
      token.png
  5. 61 0
      ytt.config.ts

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+types
+node_modules

+ 8 - 0
README.MD

@@ -0,0 +1,8 @@
+1、npm install 
+
+2、修改ytt.config.ts中的项目token
+yapi->项目->设置->token配置
+![项目token获取](token.png)
+其余配置修改参考:https://github.com/fjc0k/yapi-to-typescript
+
+3、执行 npm run ytt

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
+{
+  "name": "yapi-to-ts",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+"scripts": {
+    "ytt": "ytt"
+  },
+  "keywords": [],
+  "author": "",
+  "license": "ISC",
+  "devDependencies": {
+    "yapi-to-typescript": "^3.38.0"
+  }
+}

BIN
token.png


+ 61 - 0
ytt.config.ts

@@ -0,0 +1,61 @@
+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:"6fc643b80f506ab1b77b12760ce87a4699a60386dc1fb85e3e51b34976c210ed",
+        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}`)
+            },
+          },
+        ],
+      },
+    ],
+  },
+]);