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

auto-generate-type

v1.0.10

Published

一个基于 Swagger/OpenAPI 文档自动生成 TypeScript 类型定义和 API 请求方法的 CLI 工具

Readme

Auto Generate Type (AGT)

一个基于 Swagger/OpenAPI 文档自动生成 TypeScript 类型定义和 API 请求方法的 CLI 工具。

✨ 功能特性

  • 🚀 自动生成 TypeScript 类型 - 基于 Swagger 文档自动生成精确的类型定义
  • 📡 自动生成 API 请求方法 - 生成对应的请求函数,包含完整的类型支持
  • 🔄 缓存机制 - 支持本地缓存 Swagger 文档,提高生成效率
  • 🛠️ 交互式配置 - 友好的命令行交互界面
  • 📁 灵活的文件组织 - 支持自定义类型文件和 API 文件的存放位置
  • 🎯 多服务支持 - 支持管理多个微服务的 API 文档

📦 安装

全局安装

npm install -g auto-generate-type

项目内安装

npm install auto-generate-type --save-dev

🚀 快速开始

1. 初始化配置文件

npx agt init

2. 配置 agt.config.js

export default {
  // 服务列表
  serverList: ['user-service', 'order-service'],
  // 服务器地址
  baseUrl: 'http://localhost:8080',
  // ts类型文件存放目录
  typesDir: 'src/types',
  // 请求api存放目录
  apiDir: 'src/api',
  // 缓存文件目录
  storeDir: '.agt-cache',
  // 公共参数(生成类型时会忽略这些参数)
  commonParams: ['timestamp', 'signature']
}

3. 更新 API 文档缓存

npx agt update

4. 生成类型和 API

npx agt create

📖 命令详解

agt init

初始化配置文件,在项目根目录创建 agt.config.js 配置文件。

npx agt init

agt update

更新指定服务的 Swagger 文档缓存。

npx agt update

执行后会提示选择要更新的服务,工具会从配置的 baseUrl 下载最新的 API 文档。

agt create

交互式生成 TypeScript 类型定义和 API 请求方法。

npx agt create [--no-cache]

选项:

  • --no-cache: 不使用缓存文件,强制重新下载 API 文档

交互流程:

  1. 选择服务名称
  2. 输入 API 路径(如:/api/user/login
  3. 选择请求方法(GET/POST/PUT/DELETE)
  4. 输入类型文件名(如:user.ts
  5. 输入 API 文件名(如:userApi.ts
  6. 输入接口名称(如:LoginApi
  7. 输入方法名称(如:login

⚙️ 配置文件说明

agt.config.js 配置文件各字段说明:

| 字段 | 类型 | 必填 | 说明 | |------|------|------|------| | serverList | string[] | ✅ | 服务列表,对应 Swagger 文档的服务名 | | baseUrl | string | ✅ | API 服务器基础地址 | | typesDir | string | ✅ | TypeScript 类型文件存放目录 | | apiDir | string | ✅ | API 请求方法文件存放目录 | | storeDir | string | ✅ | 缓存文件存放目录 | | commonParams | string[] | ❌ | 公共参数列表,生成类型时会忽略 |

💡 使用示例

示例 1:生成用户登录 API

  1. 运行 npx agt create
  2. 选择服务:user-service
  3. 输入 API 路径:/api/user/login
  4. 选择请求方法:POST
  5. 输入类型文件名:user.ts
  6. 输入 API 文件名:userApi.ts
  7. 输入接口名称:LoginApi
  8. 输入方法名称:login

生成的类型文件 (src/types/user.ts):

export interface LoginApi {
  req: {
    username: string;
    password: string;
  };
  res: {
    token: string;
    userInfo: {
      id: number;
      username: string;
      email: string;
    };
  };
}

生成的 API 文件 (src/api/userApi.ts):

import type { LoginApi } from './types/user.ts'

export const login = async (data: LoginApi['req']) => REQ<LoginApi['res']>({
  url: '/user-service/api/user/login',
  method: 'POST',
  data,
})

📄 许可证

ISC

👨‍💻 作者

IO.oT


如果您觉得这个工具有用,请给个 ⭐️ 支持一下!