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

@scxfe/api-tool

v0.5.3

Published

根据 Swagger/OpenAPI 3.0 和 Apifox 的接口定义生成 TypeScript/JavaScript 的接口类型及其请求函数代码。

Readme

@scx/api-tool

一个强大的 Node.js CLI 工具,专门用于从各种 API 管理平台(如 Swagger/OpenAPI 3.0、Apifox)自动生成 TypeScript/JavaScript 代码。

主要特性

  • 多平台支持: 支持 Swagger、Apifox 等主流 API 管理平台
  • 类型生成: 自动生成完整的 TypeScript 类型定义,支持 TypeScript 和 Zod 两种模式
  • 代码生成: 自动生成 HTTP 请求函数和接口代码
  • 灵活配置: 支持自定义命名策略、输出目录、代码风格等
  • 钩子系统: 在代码生成过程的不同阶段执行自定义操作
  • Watch 模式: 监视配置文件变化,自动重新生成代码

快速开始

安装

# 全局安装(推荐)
pnpm install -g @scxfe/api-tool

# 或项目本地安装
pnpm install --save-dev @scxfe/api-tool

# 或使用 npx
npx @scxfe/api-tool

基本使用

# 1. 初始化配置文件
npx api-power init

# 2. 编辑配置文件 api-power.config.ts / api-power.config.js

# 3. 生成代码
npx api-power

文档

配置示例

Zod 模式(运行时验证)

import { defineConfig } from '@scxfe/api-tool';

export default defineConfig({
  source: 'https://api.apifox.com/v1/projects/YOUR_PROJECT_ID/export-openapi',
  token: 'YOUR_ACCESS_TOKEN',

  // 使用 Zod Schema 进行类型定义和运行时验证
  typesFormat: 'zod',

  // 输出配置
  outputDir: 'src/service',
  generateApi: true,
  generateTypes: true,
});

TypeScript 模式(编译时类型检查)

import { defineConfig } from '@scxfe/api-tool';

export default defineConfig({
  source: 'https://api.apifox.com/v1/projects/YOUR_PROJECT_ID/export-openapi',
  token: 'YOUR_ACCESS_TOKEN',

  // 使用 TypeScript 接口进行类型定义
  typesFormat: 'typescript',

  // 输出配置
  outputDir: 'src/service',
  generateApi: true,
  generateTypes: true,
});

完整配置

import { defineConfig } from '@scxfe/api-tool';

export default defineConfig({
  // API 数据源
  source: 'https://api.apifox.com/v1/projects/YOUR_PROJECT_ID/export-openapi',
  token: 'YOUR_ACCESS_TOKEN',

  // 输出配置
  outputDir: 'src/service',
  generateApi: true,
  generateTypes: true,

  // 类型生成格式
  typesFormat: 'zod', // 或 'typescript'

  // 代码生成选项
  target: 'typescript',
  indentSize: 2,
  comment: true,
  pathPrefix: '/api/v1',

  // 请求函数配置
  requestFunctionName: 'request',
  requestParamName: 'params',
  requestMethodStyle: 'config',

  // 自定义命名策略
  namingStrategy: {
    interfaceName: (info) => {
      const method = info.method.charAt(0).toUpperCase() + info.method.slice(1).toLowerCase();
      const pathName = info.path
        .replace(/\{[^}]+\}/g, '')
        .replace(/^\//, '')
        .replace(/\//g, '-')
        .replace(/^-+|-+$/g, '');
      return `${method}${pathName}`;
    },
  },

  // 钩子函数
  hooks: {
    beforeGenerate: () => {
      console.log('开始生成代码...');
    },
    afterGenerate: () => {
      console.log('代码生成完成');
    },
  },
});

项目结构

Zod 模式输出结构

src/service/
├── request.ts                      # 请求函数
├── index.ts                        # 根导出文件
├── AIFuWu/                        # 分类目录
│   ├── index.ts                    # API 函数(从 schema 导入类型)
│   └── schema.ts                   # 合并的 Schema 文件(包含所有接口的 Schema + 推导类型)
├── YongHuGuanLi/                  # 分类目录
│   ├── index.ts
│   └── schema.ts
└── schemas/                       # 类型 Schema 目录
    ├── UserSchema.ts               # 类型 Schema(包含 Schema + 推导类型)
    ├── RoleSchema.ts
    └── index.ts                   # Schema 索引文件

TypeScript 模式输出结构

src/service/
├── request.ts                      # 请求函数
├── index.ts                        # 根导出文件
├── AIFuWu/                        # 分类目录
│   └── index.ts                    # API 函数
├── YongHuGuanLi/                  # 分类目录
│   └── index.ts
└── types/                         # 类型定义目录
    ├── index.ts                   # 类型索引文件
    ├── User.ts
    └── Role.ts

开发

# 安装依赖
pnpm install

# 构建项目
pnpm run build

# 生成代码(开发模式)
pnpm run dev

# 格式化并修复代码
pnpm run lint:fix

# 构建文档
pnpm run docs:build

# 预览构建结果
pnpm run docs:preview

许可证

MIT License - 详见 LICENSE 文件

贡献

欢迎提交 Issue 和 Pull Request!

联系方式