Skip to content

插件Github https://github.com/openapi-ui/openapi-ts-request

使用手册 https://github.com/openapi-ui/openapi-ts-request/issues/100

1.安装插件

bash
pnpm add -D openapi-ts-request

2.项目根目录新建 openapi-ts-request.config.ts

ts
import type { GenerateServiceProps } from "openapi-ts-request";
import type { APIDataType } from "openapi-ts-request/dist/generator/type";
import { genDefaultFunctionName, resolveFunctionName, stripDot } from "openapi-ts-request/dist/generator/util";

export default [{
  schemaPath: "http://localhost:5169/tiku/v3/api-docs", // openapi文件地址
  serversPath: "./src/apis/tiku", // 接口存放路径
  requestLibPath: "@/utils/request/index.ts",
  hook: {
    customFunctionName(data: APIDataType, prefix: string) {
      if (data.operationId) return resolveFunctionName(stripDot(data.operationId), data.method);
      return data.method + genDefaultFunctionName(data.path, prefix);
    }
  },
  includeTags: ["LlmPromptController"] //只生成筛选
}] as GenerateServiceProps[];

3.在 package.jsonscript 中添加命令: "openapi": "openapi-ts",

bash
pnpm openapi