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

ykit-config-mock

v1.1.5

Published

ykit mock plugin

Downloads

18

Readme

ykit-config-mock

需要 ykit 版本 >= 0.5.0

Features

安装

在项目中执行:

$ npm install ykit-config-mock --save

编辑 ykit.js,引入插件:

module.exports = {
    plugins: ['mock']
    // ...
};

或者如果需要添加插件的选项,也可以传入一个对象:

module.exports = {
    plugins: [{
        name: 'mock',
        options: {
            // 插件选项
        }
    }]
    // ...
};

如何配置 mock 规则

在项目根目录下创建 mock.js:

module.exports = [
	{
        pattern: /\/testA\.com\/(.*)\.do/, // 匹配一类接口,如 /test.com/list.do -> ./mockData/list.json
        responder: './mockData/$1.json'
    },
	{
        pattern: /\/testB\.com\/person\.do/, // 直接返回数据
        responder: {id: 'abc'}
    },
	{
        pattern: '/testC.com/query.do', // pattern 为字符串
        responder: {id: 'abc'}
    }
];

动态数据

可以通过定义数据模板来返回动态 mock 数据,具体规则查看这里

{
    pattern: '/test.com/query.do',
    responder: {
        "array|1-10": [
            "Mock.js"
        ]
    }
}

// 返回数据:
// {
//   "array": [
//     "Mock.js",
//     "Mock.js",
//     "Mock.js",
//     "Mock.js"
//   ]
// }

获取远程数据(Map Remote)

将接口转到远端来获取数据。

{
    pattern: '/test.com/query.do',
    responder: 'http://yapi.corp.qunar.com/mock/58/getItems'
}

这意味着也可以用它来做类似 Map Remote 的功能:

{
    // 添加了 .json 后缀是为了防止 .js 等资源也被 Mock 服务处理。
    pattern: /\/(.*)\.json/,
    responder: 'http://yapi.corp.qunar.com/$1.json'
}

上面通过正则匹配,将所有接口转到 http://yapi.corp.qunar.com 上,比如 http://localhost/getItems.json 会成为 http://yapi.corp.qunar.com/getItems.json

通过函数处理返回

{
    pattern: '/test.com/query.do',
    responder: function(req, res) { // 两个参数,req 为请求对象,res 为返回对象
        // 这里可以有更多其他的处理过程
        // 从 req 中可以获取 req.cookies\req.query\req.body 等
        res.end(JSON.stringify({
            "ret": true,
            "data": {
                "name": "Li Lei",
                "email": "[email protected]",
                "registerDateTime": "2020-10-01 22:11:11"
            }
        }))
    }
}

jsonp

{
    pattern: '/api/list.json',
    responder: './data/listData.json',
    jsonp: 'jsCallback' // jsonp 请求的回调函数名,默认为 "callback"
}

options

mock 插件会自动寻找项目根目录下的 mock.js 或者 mock.json,你也可以通过 options 手动指定一个 mock 配置的路径:

module.exports = {
    plugins: [{
        name: 'mock',
        options: {
            confPath: './src/mock/mockconf.js'
        }
    }],
    // ...
};

关闭 mock

启动 ykit server 时追加参数即可:

$ sudo ykit s mock=false

示例

https://github.com/roscoe054/ykit-starter-mock