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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bty/vite-plugin-file-mock

v0.0.1

Published

> 基于文件系统的mock插件,基于[vite-plugin-file-mock](https://npmjs.com/package/vite-plugin-file-mock) 拓展,支持restful接口形式

Downloads

12

Readme

@keyu/vite-plugin-mock

基于文件系统的mock插件,基于vite-plugin-file-mock 拓展,支持restful接口形式

目录

安装与使用

// vite.config.js
import mockPlugin from 'vite-plugin-file-mock'

export default {
  plugins: [
    mockPlugin(),
  ]
}
interface MockPluginOptions {
  dir?: string
  enable?: boolean
  refreshOnSave?: boolean
  noRefreshUrlList?: Array<string | RegExp>
}

可以看看这个例子

选项

dir

  • Type: string
  • Default: mock

本地mock文件所在的路径, 相对于vite root, 默认是vite root下的mock文件夹

enable

  • Type: boolean
  • Default: true

是否开启mock功能 此插件只在serve阶段生效

refreshOnSave

  • Type: boolean
  • Default: true

当mock文件变更, 是否刷新浏览器

noRefreshUrlList

  • Type: Array<string | RegExp>
  • Default: []

refreshOnSave 会自动开启,当有些接口不想自动刷新页面时,可以放这里,支持正则

概览

插件会默认选择根目录/mock文件夹下所有的.js.ts(也可以是.mjs .cjs, 暂不支持.mts .cts)文件来生成mock数据, 文件路径即接口

mock/
  ├── api/
  │  ├── home.js
  │  ├── user.js

如上目录结构将生成两条接口/api/home/api/user

// home.js
export default {
  result: 1,
}
fetch('/api/home')
  .then(response => response.json())
  .then(data => console.log(data)) // { result: 1}

mock文件可以直接返回数据, 这样任何请求/api/home都将返回相同的数据

自定义内容

有时候我们需要自定义返回的内容, 比如

  • mock rest接口
  • 根据参数来动态返回内容
  • 使用faker.js来辅助内容生成
  • ...

这时候, 可以让mock文件返回一个函数, 函数里再返回我们需要自定义的内容 也可以直接使用 response 来定义 statusCode, header, data

如果函数里没有调用 response.end, 则函数的返回值会作为最终 response 返回的值

// user.js
export default (request, response) => {
  if (request.method === 'GET') {
    return {
      result: 1,
      method: request.method,
    }
  }
  else if (request.method === 'POST') {
    return {
      result: 2,
      method: request.method,
    }
  }
  else {
    response.statusCode = 500
    response.end(JSON.stringify({
      result: 3,
      method: request.method,
    }))
  }
}

同路径接口不同method

// get方法
export function GET() {
  return {
    result: 1,
    method: 'GET',
  }
}
// post方法
export function POST() {
  return {
    result: 1,
    method: 'POST',
  }
}

restful接口模糊匹配

示例:api/v1/flow/:flowId

文件路径:api/v1/flow/[flowId].ts

mock文件以[xxx]命名,即可实现模糊匹配,[xxx]将会被替换为实际的值

export default () => {
  return {
    result: 1,
  }
}

TypeScript 和 ESM 支持

mock 文件同时.js.ts, .js既可以是 commonjs, 也可以是 esm

// home.js commonjs
module.exports = {
  result: 1,
}
// home.js esm
export default {
  result: 1,
}
// home.ts
export default () => {
  return {
    result: 1
  }
}

异步函数

mock 文件也支持异步函数, 这样允许用更多的自定义

async function delay(time) {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, time)
  })
}

// 5秒后返回数据
export default async () => {
  const data = {
    result: 1,
  }

  await delay(5000)

  return data
}

忽略指定接口

忽略指定接口有两种做法:

1.把整个文件注释

// home.js
// export default {
//     result: 1,
// };

2.返回 undefined

// home.js
export default {
  result: 1,
} && undefined