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

@zhejianglab-datahub/ai-mock

v0.2.1

Published

AI驱动的场景化mock数据生成引擎

Downloads

165

Readme

AI Mock - AI驱动的场景化mock数据生成引擎

项目简介

AI Mock是一个AI驱动的场景化mock数据生成引擎,在不与接口平台深度绑定的情况下,前端通过这个插件进行本地化mock实现数据自治,通过LLM生成mock数据,减少工作量。

目前试用阶段

通过gen-api指令自动化生成axios封装模板与api函数签名模板,后续可在此基础上进行前端业务types定义与标准化数据流管理。后续开放mock相关功能

已支持命令

gen-api 命令 - API函数自动生成

gen-api 命令实现了从配置到代码的自动化,以 apiMapping 为单一数据源生成标准API函数。从后端数据源到前端,只需要在api中实现胶水函数即可

功能特点
  • 单一数据源:只需维护 apiMapping 配置,自动生成API函数
  • 类型安全:同步生成 TypeScript 类型定义
  • 复杂类型支持:智能解析数组、泛型、联合类型等复杂 TypeScript 类型
  • 增量更新:检测已存在的函数和类型,避免覆盖手动修改
  • 模块化组织:通过 dir 字段自动分组管理
  • 标准化代码:生成统一格式的API调用函数
配置示例
{
  "apiMappings": [
    {
      "name": "getTaskInfo",
      "dir": "data-process",
      "path": "/api/datahub/database/dataProcess/getTaskDetail",
      "method": "GET",
      "responseType": "BasicInfo",
      "description": "获取任务详情"
    }
  ],
  "typesDir": "./src/types",
  "requestConfig": {
    "mode": "minimal"
  }
}

配置字段说明

  • name: 生成的API函数名称,配合gen-api命令使用
  • dir: 文件分组名称,用于组织API和类型文件结构
  • path: API接口路径
  • method: HTTP方法, 'GET' | 'POST' | 'DELETE' | 'PUT'
  • requestType (可选): 请求参数类型名称,支持复杂类型
  • responseType: 响应类型名称,支持复杂类型
  • description(可选): 接口描述
  • requestConfig(可选): request 模块生成配置,不填使默认为full模式

requestConfig 配置说明

  • mode: request 文件生成模式,支持三种模式:
    • full(默认): 生成完整的 axios 封装代码,包含拦截器、错误处理等,适合新项目
    • minimal: 生成最小模板,只包含基础结构,需要手动补充业务逻辑,适合需要自定义的场景
    • custom: 不生成 request 文件,使用项目已有的 request 模块,适合旧项目集成
  • importPath(mode 为 custom 时必填): 自定义 request 模块的导入路径,如 @/utils/request

自定义 request 模块要求(mode 为 custom 时):

运行 gen-api 命令时,会在控制台显示详细的接口规范提示。

自定义的 request 模块必须导出以下方法:

export default {
    get: <T = any>(url: string, params?: any, config?: any): Promise<T>    // ⚠️ 第二个参数是 params
    post: <T = any>(url: string, data?: any, config?: any): Promise<T>     // ⚠️ 第二个参数是 data
    put: <T = any>(url: string, data?: any, config?: any): Promise<T>      // ⚠️ 第二个参数是 data
    delete: <T = any>(url: string, config?: any): Promise<T>               // ⚠️ 第二个参数是 config
}

关键注意事项

  • get 方法:第二个参数是 params(查询参数),内部需要处理为 { params }
  • post/put 方法:第二个参数是 data(请求体),直接传递给 axios
  • delete 方法:第二个参数是 config(配置对象)
使用方式
  1. 在项目根目录下新建 .ai-mock.config.json 文件
  2. 配置api数据源
  3. 运行生成命令
ai-mock gen-api
生成结果

src/api/request.ts (axios 封装,根据 requestConfig.mode 决定):

  • full 模式:生成完整的 axios 封装,包含拦截器、Token 处理、错误处理等
  • minimal 模式:生成基础模板,包含 TODO 注释提示需要补充的内容
  • custom 模式:不生成此文件,使用项目已有的 request 模块

src/api/interface/data-process.ts:

import request from '../request';
import { BasicInfo } from '../../types/data-process';

// 获取任务详情
export const getTaskInfo = (req: any): Promise<BasicInfo> => {
    return request.get(`/api/datahub/database/dataProcess/getTaskDetail`, req);
};

src/api/index.ts (统一导出,采用追加模式):

export * from './interface/data-process';
// 再次运行 gen-api 时,会追加新的 export 而不是覆盖
// export * from './interface/user';
// export * from './interface/product';

src/types/data-process.ts:

export type BasicInfo = any;