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

lzyapi2js

v0.3.7

Published

将 YAPI 导出的 JSON 转为符合项目规范的 Axios 请求 JS 代码

Readme

YAPI JSON → Axios 代码生成器

将 YAPI 导出的接口 JSON 批量转换为符合项目约定的 Axios JS/TS 代码。

快速开始

安装

pnpm install -g lzyapi2js

基本使用

# 生成 JavaScript 代码
lzyyapi --input JSON/*.json --out-dir dist

# 生成 TypeScript 代码
lzyyapi --input JSON/*.json --ext ts --out-dir dist

# 预览生成内容(不写盘)
lzyyapi --input JSON/park-energy-condition.json --dry-run

特性

  • 灵活输入:支持本地文件(通配符、Windows 路径)和远程 URL
  • 类型增强:智能推导 Request/Response/Params 类型,生成详细 JSDoc
  • Mock.js 支持:自动处理 @string@integer 等占位符
  • 注释保留:提取 JSON 注释并保留到类型定义
  • TypeScript 支持:生成 .ts.d.ts 文件,支持模块化类型组织
  • 自动化处理:自动解析 YAPI 结构,生成 REST 方法调用
  • 高度可配置:自定义模板、baseUrl、格式化等
  • proBasepath 支持:优先使用 JSON 中的 proBasepath 作为 baseUrl
  • HTTP 下载命名优化:URL 输入的输出文件统一命名为 index.js/index.ts

命令行选项

| 选项 | 简写 | 说明 | 默认值 | |------|------|------|--------| | --input | -i | 输入源(文件或 URL) | JSON/*.json | | --out-dir | -o | 输出目录 | dist | | --config | -c | 配置文件路径 | - | | --template | -t | 自定义模板文件路径 | - | | --ext | -x | 输出扩展名(js\|ts) | js | | --export-types | -e | 是否导出类型定义 | true | | --type-organization | -O | 类型组织方式(unified\|modular) | unified | | --base-url | - | 固定 baseUrl 前缀 | 自动推断 | | --log-level | - | 日志等级(info\|warn\|error\|debug) | info | | --dry-run | - | 预览不写盘 | false | | --format | -f | 生成后格式化 | true |

生成规则

方法映射

| 方法 | 有查询参数 | 无查询参数 | |------|-----------|-----------| | GET/DELETE | api.get(url, { params }) | api.get(url) | | POST/PUT/PATCH | api.post(url, body, { params }) | api.post(url, body) |

函数命名

method + 路径分段(驼峰) 生成,重名自动去重(a, a2, a3

路径参数

/:id{id}${params.id}

配置文件

创建 yapi-gen.config.json

{
  "importApi": "@/libs/HttpRequest",
  "baseUrlStrategy": "infer",
  "enablePrettier": true,
  "outputExt": "js",
  "exportTypes": true,
  "generateDts": true,
  "typeOrganization": "unified",
  "extractSharedTypes": true,
  "sharedTypeThreshold": 0.8,
  "typesDir": "types",
  "createTypesIndex": true
}

配置说明

| 配置项 | 说明 | 默认值 | |--------|------|--------| | importApi | API 请求模块导入路径 | @/libs/HttpRequest | | baseUrlStrategy | baseUrl 推断策略(proBasepath > customBaseUrl > infer) | infer | | enablePrettier | 是否启用 Prettier 格式化 | true | | outputExt | 输出文件扩展名(jsts) | js | | exportTypes | 是否导出类型定义 | true | | generateDts | 是否生成 .d.ts 声明文件 | true | | typeOrganization | 类型组织方式(unified 统一或 modular 模块化) | unified | | extractSharedTypes | 是否提取共享类型 | true | | sharedTypeThreshold | 共享类型相似度阈值(0-1) | 0.8 | | typesDir | 类型文件目录名 | types | | createTypesIndex | 是否创建类型索引文件 | true |

示例输出

JavaScript

import api from "@/libs/HttpRequest";
const baseUrl = "/park-energy-condition";

export const getEleDashboardGetRentMonthUseWithComparePrev = () => {
  const url = `${baseUrl}/eleDashboard/getRentMonthUseWithComparePrev`;
  return api.get(url);
};

export const postElectricOverviewGetElectricControlDeviceOverview = (body) => {
  const url = `${baseUrl}/electricOverview/getElectricControlDeviceOverview`;
  return api.post(url, body);
};

TypeScript

import api from "@/libs/HttpRequest";
const baseUrl = "/park-energy-condition";

export type postElectricOverviewGetStatisticsRequest = {
  startTime?: string;
  endTime?: string;
  enterpriseCode?: string;
};

export const postElectricOverviewGetStatistics = (
  body: postElectricOverviewGetStatisticsRequest,
): Promise<postElectricOverviewGetStatisticsResponse> => {
  const url = `${baseUrl}/electricOverview/getStatistics`;
  return api.post(url, body);
};

TypeScript 高级用法

类型文件分离

使用 --type-organization modular 将类型定义提取到独立文件:

lzyyapi --input JSON/park-energy-condition.json -x ts -O modular -o dist

生成目录结构:

dist/
├── park-energy-condition.ts
├── park-energy-condition.d.ts
└── types/
    └── types.ts

主文件通过 import type 从类型文件导入:

import { postElectricMeterGetPage } from './park-energy-condition';
import type { GetElectricMeterPageRequest } from './types';

类型推导规则

  • Response:基于 res_body 的 JSON Schema
  • Request:基于 req_body_other 的 JSON Schema
  • Params:基于 req_queryreq_params
  • 支持嵌套对象、数组、联合类型、可选属性

本地开发

# 安装依赖
pnpm install

# 构建
pnpm build

# 运行测试
pnpm test

# 格式化代码
pnpm format

日志与错误

  • 日志等级:info(进度)、warn(警告)、error(错误)、debug(调试)
  • Schema 校验失败时自动降级轻量校验
  • 路径参数缺失时需在调用时传入对应字段

许可

MIT