npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@pnt-team/yapi-gen

v1.0.2

Published

根据 YApi 接口定义自动生成 TypeScript 类型声明和 API 函数

Readme

@pnt-team/yapi-gen

根据 YApi 接口定义,自动生成 TypeScript 类型声明和 API 函数。

功能

  • 从 API 文件中自动提取 axios 调用的 URL,匹配 YApi 接口并生成 TypeScript 类型
  • 自动给 axios 调用补全参数类型和返回值类型
  • 支持多项目配置,跨项目搜索接口
  • 支持单文件、批量、按 URL 三种运行模式
  • 自动清理未使用的手写内联类型

安装

# 全局安装
npm install -g @pnt-team/yapi-gen

# 或作为项目开发依赖
npm install -D @pnt-team/yapi-gen

配置

在项目根目录创建 yapi-config.json(可参考 yapi-config.example.json):

{
  "yapiBaseUrl": "https://your-yapi-domain.com",
  "outputDir": "src/api",
  "projects": [
    {
      "name": "项目名称",
      "token": "your-yapi-project-token"
    },
    {
      "name": "另一个项目",
      "token": "another-project-token"
    }
  ]
}

| 字段 | 说明 | |------|------| | yapiBaseUrl | YApi 服务地址 | | outputDir | 类型文件输出目录,类型会写入 {outputDir}/types/{module}.ts | | projects | YApi 项目列表,每个项目需要 nametoken |

token 获取方式:进入 YApi 项目 → 设置 → token 配置 → 复制 token

兼容旧格式

也支持单项目的简写格式:

{
  "yapiBaseUrl": "https://your-yapi-domain.com",
  "outputDir": "src/api",
  "token": "your-yapi-project-token"
}

使用

模式一:按文件生成(推荐)

扫描指定 API 文件中的 axios 调用,自动匹配 YApi 接口并生成类型:

yapi-gen --file src/api/account.ts
# 简写
yapi-gen -f src/api/account.ts

效果:

  1. 提取文件中所有 axios.get('/xxx')axios.post('/xxx') 等调用的 URL
  2. 在配置的所有 YApi 项目中搜索匹配的接口
  3. 生成类型文件到 src/api/types/account.ts
  4. 自动更新源文件:给 axios 调用补上参数类型和返回值类型

模式二:批量生成

自动扫描 src/api/ 目录下所有 .ts 文件并批量处理:

yapi-gen --all
# 简写
yapi-gen -a

模式三:按接口 URL 生成

指定 YApi 接口路径,生成类型并追加 API 函数到对应模块文件:

yapi-gen --url /sacc/find --module account
# 简写
yapi-gen -u /sacc/find -m account

效果:

  1. 在 YApi 中搜索路径为 /sacc/find 的接口
  2. 生成类型到 src/api/types/account.ts
  3. src/api/account.ts 中追加对应的 API 函数

指定配置文件

默认读取项目根目录的 yapi-config.json,也可以手动指定:

yapi-gen -f src/api/account.ts --config ./config/yapi.json
# 简写
yapi-gen -f src/api/account.ts -c ./config/yapi.json

查看帮助

yapi-gen --help

生成示例

假设 src/api/account.ts 中有:

export const getAccountInfo = (params) =>
  axios.get('/api/account/info', { params });

运行 yapi-gen -f src/api/account.ts 后:

生成类型文件 src/api/types/account.ts

/**
 * 本文件由 yapi-gen 自动生成,请勿手动修改
 */

export type AccountInfoQuery = {
  id: number;
};

export interface AccountInfoResponse {
  name: string;
  email?: string;
  avatar?: string;
}

自动更新源文件 src/api/account.ts

import { AccountInfoQuery, AccountInfoResponse } from './types/account';

export const getAccountInfo = (params: AccountInfoQuery): Promise<commonEesType<AccountInfoResponse>> =>
  axios.get('/api/account/info', { params });

输出结构

src/api/
├── account.ts          ← API 函数文件(自动补全类型)
├── user.ts
└── types/
    ├── account.ts      ← 自动生成的类型声明
    └── user.ts

作为 Library 使用

除了 CLI,也可以在代码中导入核心函数:

import { findApisInYapi, processApi, schemaToTs } from '@pnt-team/yapi-gen';

CLI 参数一览

| 参数 | 简写 | 类型 | 说明 | |------|------|------|------| | --file | -f | string | 指定 API 文件路径 | | --all | -a | boolean | 批量处理 src/api 下所有文件 | | --url | -u | string | 指定 YApi 接口路径 | | --module | -m | string | 指定模块名(配合 --url) | | --config | -c | string | 配置文件路径(默认 yapi-config.json) | | --help | -h | - | 查看帮助 | | --version | -v | - | 查看版本号 |

注意事项

  • 配置文件 yapi-config.json 包含 token 敏感信息,不要提交到 Git 仓库
  • 类型文件由工具自动生成,请勿手动修改(再次运行会被覆盖)
  • URL 匹配时会自动忽略 /api/v1//api/v2/ 等前缀
  • 已使用 axios 泛型(如 axios.post<A, B, C>(...))的调用不会被自动替换

License

MIT