Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
stone-mock (Smock) 是一款基于配置的简单易扩展的mock工具🔧
npm install stone-mock
git clone https://github.com/yuanhaoyu/stone-mock.git
cd stone-mock && npm install
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
npm run start
smock接受一个数组作为datas用来生成apis,datas的每一项,Smock也有严格的规定,他必须是一个对象并且拥有以下属性
path就是你定义的api地址
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的那条内容
模拟接口返回的值
method即调用接口的方法,默认为get,如果想支持所有方法,请设置为"all"
即是否开启mock模式,他是一个Boolean,默认为false,当为true即开启mock模式,可以使用mockjs的语法进行mock。
smock接受一个对象作为config用来配置,其中包括
Smock服务使用的端口,默认为3003
所有接口的前置url,默认为空
接口可视化页面的路由,默认为/apis
统一处理接口返回的格式,默认为
{
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 字段**。
完成Smock的实例化后,我们可以用init方法来开启服务。
const smock = new Smock(datas, config);
smock.init();
为了更好的管理接口,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
);
Smock支持可视化查看所有接口
默认情况访问127.0.0.1:端口号/apis,即可查看你设置的Smock。
FAQs
A happy and easy mock tool
We found that stone-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.