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

vite-plugin-mock-zhiniu

v0.0.4

Published

A Vite plugin for mocking API responses with TypeScript support

Readme

vite-plugin-mock-zhiniu

一个用于 Vite 的 API 模拟插件,支持 TypeScript 和多种响应格式。

特性

  • 🚀 支持 TypeScript 类型定义
  • 🔄 支持多种响应格式(对象、函数)
  • 📦 零配置,开箱即用
  • 🛡️ 内置错误处理
  • 🔍 自动添加 mock 标记

安装

# 使用 npm
npm install vite-plugin-mock-zhiniu --save-dev

# 使用 yarn
yarn add vite-plugin-mock-zhiniu --dev

# 使用 pnpm
pnpm add vite-plugin-mock-zhiniu --save-dev

使用方法

  1. vite.config.ts 中配置插件:
import { defineConfig } from 'vite';
import mockPlugin from 'vite-plugin-mock-zhiniu';

export default defineConfig({
  plugins: [
    mockPlugin({
      routerMap: {
        '/api/user': {
          default: {
            name: 'John Doe',
            age: 30
          }
        },
        '/api/login': {
          default: async (req, res) => {
            // 处理登录逻辑
            return {
              token: 'mock-token',
              user: {
                id: 1,
                name: 'John Doe'
              }
            };
          }
        }
      }
    })
  ]
});
  1. 在代码中使用:
// 发起请求
const response = await fetch('/api/user');
const data = await response.json();

console.log(data); // { name: 'John Doe', age: 30, isMock: true }

配置选项

routerMap

类型:Record<string, RouterMapValue>

路由映射配置,key 为 API 路径,value 为响应内容。

响应内容可以是以下格式:

export default {
    // 直接对象:
    '/api/user/login': {
        code: 0,
        data: {
            token: '1234567890',
        },
    },
    // 函数
    '/api/user/login1': {
        default:()=>{
            return {
                code: 0,
                data: {
                    token: '11111',
                },
            }
        }
    },
    // 异步函数
    '/api/user/login2': {
        default:async()=>{
            return new Promise((resolve)=>{
                setTimeout(()=>{
                    resolve({
                        code: 0,
                        data: {
                            token: '22222',
                        },
                    })
                },1000)
            })
        }
    },
    // 获取请求的body,和url中的query数据
    '/api/user/login3': {
        default:(req)=>{
            return {
                code: 0,
                data: {
                    token: '33333',
                    // POST 请求 body 体参数
                    ...req.body,
                    // 获得 url query 参数
                    ...req.query
                },
            }
        }
    },
    "/api/user/login4": require('./api/user/login4.js'),
}

开发

# 安装依赖
bun install

# 开发模式(监听文件变化)
bun run dev

# 构建
bun run build

# 类型检查
bun run type-check

许可证

MIT