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

@postar/yapi-sdk-gen

v1.2.3

Published

从 YAPI 接口文档自动生成 TypeScript 类型定义、请求函数、Mock 文件的 SDK 工具,帮助前端团队实现接口类型安全与自动化对接。

Readme

YAPI SDK 生成器

npm version license

一个基于 YAPI 接口文档的自动化 SDK 生成工具,帮助前端团队提升开发效率和代码质量。

✨ 特性

  • 🚀 自动生成 - 从 YAPI 接口文档自动生成 TypeScript 类型定义、请求函数和 Mock 文件
  • 🔒 类型安全 - 完整的 TypeScript 类型支持,提供代码补全和类型检查
  • 📦 零配置 - 开箱即用的默认配置,满足大多数项目需求
  • 🎨 高扩展性 - 支持自定义模板,灵活适配不同项目规范
  • 🔄 实时同步 - 监听接口文档变更,自动更新本地代码
  • 🎯 开发提效 - 告别手写接口代码,专注业务逻辑开发

🎯 功能

  • TypeScript 类型定义生成
  • 请求函数自动生成
  • Mock 数据文件生成
  • 支持自定义模板
  • 支持批量生成和单个生成
  • 支持实时预览

📦 安装

npm install @postar/yapi-sdk-gen --save-dev
# 或者
pnpm install @postar/yapi-sdk-gen --save-dev
# 或者
yarn add @postar/yapi-sdk-gen -D

使用示例

  1. 根目录创建 yapi-sdk-gen.js 文件。
  2. 在 yapi-sdk-gen.js 中配置:
import startYapiSdkGenServer from '@postar/yapi-sdk-gen'
// 可选mock配置
const mockConfig = {
  enable: true, // 启用 mock 生成
  mockPath: 'apps/marketing_ai/mock', // mock 文件输出路径
  mockFolderName: '/', // mock 文件夹名称
  delay: 300, // 默认延迟时间
  prefix: '/dev-api/aiMarket', // API 前缀
  // 添加默认响应配置(可不配置,按默认)
  defaultResponse: {
    codeKey: 'code', // 状态码的字段名
    messageKey: 'msg', // 消息的字段名
    codeValue: 200, // 状态码的默认值
    messageValue: '操作成功' // 消息的默认值
  },
  // 添加模板配置(可不配置,按默认)
  template: {
    // 文件头部导入语句
    imports: `import { defineMock } from 'vite-plugin-mock-dev-server';\nimport Mock from 'mockjs';`,
    // 文件导出模板
    exportTemplate: configs => `export default defineMock([\n  ${configs}\n]);`,
    // mock 配置模板
    mockConfigTemplate: (config, formatJson) => `{
  url: '${config.url}',
  method: '${config.method.toUpperCase()}',
  delay: ${config.delay},
  body: Mock.mock(${formatJson(config.body)})
}`
 }
}
// 主要配置
const config = {
  mockConfig,
  port: 3456, // 启动端口
  serverUrl: 'http://xxx.xxx.xxx.xxx:xxx', // 服务器地址
  token: 'xxx', // 登录token
  projectId: '295', // 项目id
  outputConfig: {
    requestPath: 'apps/marketing_ai/src/apis', // 生成的请求路径
    requestFolderName: 'api', // 生成的请求文件夹名
    typePath: 'apps/marketing_ai/src/apis', // 生成的类型路径
    typeFolderName: 'types' // 生成的类型文件夹名
  },
  importTypePath: `@/apis/types`, // 导入的类型路径
  importTemplate: `import { request } from "../../index";\nconst baseUrl = 'aiMarket';`, // 导入模板
  importTypeTemplate: ({ importTypes, importPath }) =>
    `import type { ${importTypes.join(', ')} } from '${importPath}';`, // 自定义类型导入模板
  requestTemplate: ({
    functionName, // 生成的函数名
    functionParams, // 参数数组
    reqInterfaceName, // 请求类型
    resInterfaceName, // 返回类型
    urlWithParams, // url字符串,带模板变量
    method, // 请求方法
    hasQuery, // 是否有query参数
    hasBody // 是否有body参数
  }) => `export const ${functionName} = (${functionParams.join(', ')}) =>
  request<${reqInterfaceName}, ${resInterfaceName}>({
    url: \`/\${baseUrl}${urlWithParams}\`,
    method: "${method.toUpperCase()}"${hasQuery ? ',\n    params: query' : ''}${hasBody ? ',\n    data' : ''}
  });`,
}

startYapiSdkGenServer(config)
  1. 根目录运行:
node yapi-sdk-gen.js

YAPI SDK 生成器