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

stone-mock

v2.0.1

Published

A happy and easy mock tool

Downloads

9

Readme

stone-mock

stone-mock

stone-mock (Smock) 是一款基于配置的简单易扩展的mock工具🔧

Installation

NPM

npm install stone-mock

Git

git clone https://github.com/yuanhaoyu/stone-mock.git

cd stone-mock && npm install

Usage

NPM

new a file namedapp.js

const { Smock } = require('stone-mock');

const config = {
    port: 3005,
    baseUrl: '',
    preview: '/preview'
}

const datas = [
  {
    path: '/test',
    type: 'data',
    value: {
     test: 'test'
    }
  }
]

const smock = new Smock(datas, config);
smock.init();

then happy start 🚗

node app.js

Git

npm run start

Smock(datas, config)

datas

smock接受一个数组作为datas用来生成apis,datas的每一项,Smock也有严格的规定,他必须是一个对象并且拥有以下属性

  • path
  • type
  • vaule
  • method
  • mock

path

path就是你定义的api地址

type

type是一个可选择的属性,他支持"function" || "store" || "data" || "proxy" 四种类型。

  • function 默认参数为ex,你可以用ex.ctx访问koa的上下文,也可以ex.Mock自定义mock,为了你更方便的在function里随心所欲,我们同样封装了ex.query 可获取get请求参数,ex.body 可获取post请求参数,ex.params 可获取url参数。[ex.query/ex.body/ex.params 均返回一个对象]

  • store 使用store后,会以restful的模式自动创建,一级子资源接口。如下:

{
  'path': '/user',
  'type': 'store',
  'value': [
    {
      id: 1,
      name: 'sam'
    },
    {
     id: 2,
     name: 'amy'
    }
  ]
}

当你访问/user的时候就得到上面value的结果,当你访问/user/1 就自动获取id=1的那条内容

  • proxy: 将value中填入你要代理的url(当前只支持get方式的)
  • data: 模拟数据,type的默认值。

value

模拟接口返回的值

method

method即调用接口的方法,默认为get,如果想支持所有方法,请设置为"all"

mock

即是否开启mock模式,他是一个Boolean,默认为false,当为true即开启mock模式,可以使用mockjs的语法进行mock。


config

smock接受一个对象作为config用来配置,其中包括

  • port : Number
  • baseUrl : String
  • preview : String
  • setResponse : Function

port

Smock服务使用的端口,默认为3003

baseUrl

所有接口的前置url,默认为空

preview

接口可视化页面的路由,默认为/apis

setResponse

统一处理接口返回的格式,默认为

 {
      code: 200,
      msg: "success",
      data: "mock value"
  }

当然你或许想要根据不同的输入得到不同的code返回,这里就需要@error配合type: 'function' 使用,如下面这个登录的例子。

{
  path: 'login',
  method: 'post',
  type: 'function',
  value: function(ex) {
    if (ex.body.username === 'houn' && ex.body.password === '123') {
      return {
        username: 'houn',
        userId: '102123122'
       }
     } else {
       return {
         '@error': {
           code: '203',
           msg: '登录失败'
          }
        }
     }
   }
}

只要你的返回中含有@error时,我们会直接获取@error的值作为你的返回,所以一定要谨慎使用**@error 字段**。

init

完成Smock的实例化后,我们可以用init方法来开启服务。

const smock = new Smock(datas, config);
smock.init();

composeFactory

为了更好的管理接口,Smock建议将相关接口作为一个单独文件,然后用module.exports = [] 的方法将其导出,然后Smock提供composeFactory方法将多个数组合并成一个。

const { composeFactory } = new require('../src/index');

// apis
const nav = require('./api/nav');
const login = require('./api/user/login');
const kind = require('./api/user/kind');
const luck = require('./api/user/luck');

// stores
const topic = require('./store/topic');
const indexPhoto = require('./store/index/photo');

module.exports = composeFactory(
    nav,
    login,
    luck,
    kind,
    topic,
    indexPhoto
);

Visualization

Smock支持可视化查看所有接口

默认情况访问127.0.0.1:端口号/apis,即可查看你设置的Smock。