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

@foxman/plugin-mock-control

v1.2.3

Published

> 扩展 Foxman 基础的 Mock 方式

Downloads

8

Readme

MockControl

扩展 Foxman 基础的 Mock 方式

插件作用

Foxman 基础的 Mock 方式,是返回静态的 json。但是,我们终究会遇到一些特殊的需求:

  1. 根据请求对响应数据进行定制;
  2. 模拟请求超时
  3. Mock 文件下载功能
  4. JSONP 等其他非 JSON 格式的 异步数据响应

而这个插件的作用,就是扩展 Foxman 的 Mock 数据加工的能力。

DEMO 演示

$ git clone https://github.com/kaola-fed/foxman/
$ cd packages/foxman-plugin-mockcontrol/example/
$ npm i # 无梯子用户推荐 cnpm
$ npm start

⚠️ 以上都是本地链接

如何引入

  1. cd path/to/project (已启用 foxman 的工程)
  2. npm i --save-dev @foxman/plugin-mock-control
  3. foxman.config.js 中新增 plugins 的配置项
const MockControl = require('@foxman/plugin-mock-control');
module.exports = {
    ...
    plugins: [
        new MockControl({
            mapJS: function (dataPath) { // (mockDATA) => mockJS
                return dataPath.replace(/\.json$/, '.js');
            }
        })
    ],
    ...
}
  • mapJS - 从 Mock Data 文件到 Mock JS 文件的映射方式,缺省为与 .json 同名 .js 文件

Mock JS 能力说明

  • this 指向 koa 当前请求的 context
module.exports = function () {
    console.log(this);
}
  • 第一个参数为 mock 数据
module.exports = function (data) {
    console.log('data', data);
}
  • 当方法体含 return 语句时,会将 return 的对象用于后续的页面渲染
module.exports = function (data) {
    return data;
}
  • 当方法体不含 return 语句,则不再执行后续中间件。此时,可以指定 this.body
module.exports = function (data) {
    this.body = 'without return.'
}
  • 支持 generator,用于处理异步的场景
module.exports = function * (data) {
    yield new Promise((resolve) => {
        setTimeout(resolve, 3000);
    });
    return data;
}