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

mock-h3

v0.0.1-beta.12

Published

this is vite plugin by mock-h3

Downloads

232

Readme

Mock H3

这是一个专门为vite实现的一个mock server插件。

使用

pnpm add mock-h3

vite.config.ts中导入:

import { mockH3 } from 'mock-h3/vite'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    mockH3()
  ]
})

结构

默认情况下,你直接在配置vite.config的同级目录下,创建一个servers文件夹,其中servers文件夹下的目录结构大致如下:

servers/                            # 项目根目录
├── middleware/                     # 中间件目录
│   ├── test1.ts                    # 中间件1
│   ├── test2.ts                    # 中间件2
│   └── ...                         # 更多的中间件
├── plugins/                        # 创建目录
│   ├── plugin1.ts                  # 插件1
│   ├── plugin2.ts                  # 插件2
│   └── ...                         # 更多的插件
└── routes/                         # 路由目录
    ├── index.ts                    # 主路页面
    ├── user.get.ts                 # 定义get请求
    ├── edit.post.ts                # 定义post请求
    └── ...                         # 更多的请求方式

中间件

中间件的调用顺序是与文件的顺序一致的,所以你要保证中间件的加载顺序的话,那么你需要确保你的文件顺序是正确的。

具体使用请参考h3中间件部分的文档。

插件

插件是h3中的插件功能,具体请参考H3插件部分的文档。

这里需要注意的是,建议使用mock-h3来引用插件的定义,如下:

// logger.ts

import { definePlugin } from 'mock-h3'

export default definePlugin((h3, _options) => {
  if (h3.config.debug) {
    h3.use((req) => {
      console.log(`[${req.method}] ${req.url}`)
    })
  }
})

属性

  • prefix - 请求前缀,默认情况下是/api
  • srcDir - 配置扫描资源的路径,默认是servers
  • build - 配置是否启用在生产环境下打包mock服务,默认是true
  • outputDir - 构建输出的目录,默认是dist/servers,这个只会在build属性为true的时候生效。