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 🙏

© 2025 – Pkg Stats / Ryan Hefner

lzyapi2js

v0.2.0

Published

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

Readme

YAPI JSON → Axios 代码生成器

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

特性

  • 灵活输入
    • 支持本地文件(通配符 JSON/*.json、Windows 反斜杠路径、带引号路径)
    • 支持远程 URL(如 http://yapi.example.com/export.json
  • 类型增强
    • 智能推导 Request Body、Response Body 及 Query/Path 参数类型
    • 生成详细的 JSDoc 注释(支持嵌套对象、数组、枚举等),告别 any
    • 自动区分有无 Body 的接口,避免生成多余的参数注释
  • 自动化处理
    • 自动解析 path/method/req_query/req_params/req_body_other/res_body/title/desc
    • 根据 REST 方法生成 api.get/post/put/delete/patch
    • 路径占位参数自动替换(/:id{id}${params.id}
  • 高度可配置
    • 可自定义模板与 baseUrl 规则
    • 支持 Prettier 格式化

npm 安装后使用

  • 安装后可直接命令行调用:
    • lzyyapi --input JSON/*.json --out-dir dist --format
    • lzyyapi -i "http://yapi.demo/export.json" -o dist

命令行选项

  • --input, -i:输入源(默认:JSON/*.json
    • 本地文件:支持 Glob 模式(如 *.json)或绝对/相对路径
    • 远程 URL:以 http://https:// 开头的地址
  • --out-dir, -o:输出目录(默认:dist
  • --config, -c:配置文件路径(与默认配置合并)
  • --template, -t:自定义模板文件路径(EJS)
  • --ext:输出扩展名(js|ts,默认 js + JSDoc 提示)
  • --base-url:固定 baseUrl 前缀(默认推断自文件名,如 park-energy-condition/park-energy-condition
  • --log-level:日志等级(info|warn|error|debug
  • --dry-run:预览生成内容不写盘
  • --format:生成后格式化(依赖 Prettier 配置)

生成规则与模板约定

  • 头部:
    • import api from "@/libs/HttpRequest";
    • const baseUrl = "/<由文件名或配置决定>";
  • 方法映射:
    • GET/DELETE:有查询参数 → api.get(url, { params });无 → api.get(url)
    • POST/PUT/PATCH:
      • 同时有 bodyparamsapi.<method>(url, body, { params })
      • bodyapi.<method>(url, body)
      • paramsapi.<method>(url, undefined, { params })
      • 都没有 → api.<method>(url)
  • 函数命名:按 method + 路径分段(驼峰) 生成;遇到重名自动去重
  • JSDoc:包含 @description@param(精准类型)与 @returns(精准类型)

配置文件示例(yapi-gen.config.json)

{
  "importApi": "@/libs/HttpRequest",
  "baseUrlStrategy": "infer",
  "groupBy": "file",
  "fileNameCase": "kebab",
  "methodStyle": "api",
  "enablePrettier": true,
  "outputExt": "js",
  "templatePath": null
}

示例输出(节选)

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

/**
 * @description 查询企业月度用电量,对比上一个月
 * @returns {Promise<{ retCode?: number, msg?: string, data?: Array<{ time?: string, enterpriseCode?: string, common?: number }> }>} 响应数据
 */
export const getEleDashboardGetRentMonthUseWithComparePrev = () => {
  const url = `${baseUrl}/eleDashboard/getRentMonthUseWithComparePrev`;
  return api.get(url);
};

/**
 * @description 获取电表集中器统计
 * @param {{ id?: number, pageNum?: number, pageSize?: number, searchKey?: string }} [body] 请求体
 * @returns {Promise<{ retCode?: number, data?: Object }>} 响应数据
 */
export const postElectricOverviewGetElectricControlDeviceOverview = (body) => {
  const url = `${baseUrl}/electricOverview/getElectricControlDeviceOverview`;
  return api.post(url, body);
};

日志与错误

  • 日志等级:info|warn|error|debug,构建时输出 CLEAN/BUILD/CHMOD/SIZE/DONE/OUTPUTS
  • 常见异常:
    • Schema 校验(draft-04)缺失 → 自动降级轻量校验并记录日志
    • 路径参数缺失 → 生成函数调用需传入 params 对应字段