Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
emock-service
Advanced tools
emock-service提供模拟REST API服务,仅通过一些简单逻辑,就可以实现一个比较『完整、真实』的后端。在前后端分离开发的项目中,帮助前端人员快速开发。
npm install emock-service
项目下新建db.json
文件,增加以下内容:
{
"users": [
{
"id": 1,
"name": "jack"
},
{
"id": 2,
"name": "rose"
}
]
}
import {server} from 'emock-service';
let options = {
source: 'db.json',
host: '127.0.0.1',
port: 3000
};
server(options);
emock-service自动生成以下服务:
GET /users
支持任意字段排序、分页、全字段搜索
desc
、asc
GET /users/:id
POST /users
PUT /users/:id
DELETE /users/:id
浏览器地址栏输入http://127.0.0.1:3000/users
,可看到如下结果:
emock-service可根据实体的json schema定义自动生成测试数据,目前json schema需满足以下要求:
.json
id
标识资源名称(待改进,json schema规范中id为当前json schema的资源标识符)faker, chance
mock-service使用json-schema-faker生成实体数据,因此,需要在json schema的字段定义中引入faker, chance,调用fakerjs/chancejs
对应的方法随机生成数据。
{
"id": "user",
"type": "object",
"properties": {
"name": {
"type": "string",
"maxLength": "100",
"minLength": "2",
"faker": "name.findName"
}
},
"required": ["name"]
}
Notes: json schema中无需定义id
,emock-service会生成唯一的id。
修改server启动配置:
import {server} from 'emock-service';
let options = {
source: 'db.json',
host: '127.0.0.1',
port: 3000,
// 自动生成数据使用的json schema所在路径
schemaDir: 'path/to/schemas',
// 为每个资源自动生成的数据条数,默认值10
count: 20
};
server(options);
资源之间一般存在关联关系,json schema不能描述资源之间的这种关系,缺少关联关系可能导致自动生成的数据不可用。因此emock-service了一种描述简单关联关系的方法,具体参见示例:
export default {
user: {
companyId: {
resource: 'company',
field: 'id'
},
companyIds: {
resource: 'company',
field: 'id'
},
}
};
上述对象表达的关联关系为:
import {server} from 'emock-service';
import relations from './relations';
let options = {
source: 'db.json',
host: '127.0.0.1',
port: 3000,
// 自动生成数据使用的json schema所在路径
schemaDir: 'path/to/schemas',
// 为每个资源自动生成的数据条数,默认值10
count: 20,
relations: relations
};
server(options);
自定义route中可以使用lowdb实例,个性化处理请求。
import {server} from 'emock-service';
let options = {
source: 'db.json',
host: '127.0.0.1',
port: 3000,
// 自动生成数据使用的json schema所在路径
schemaDir: 'path/to/schemas',
// 为每个资源自动生成的数据条数,默认值10
count: 20,
routes: [
{
url: '/api/js/users/verify',
method: 'GET',
handler(db, req, res, next) {
}
}
]
};
server(options);
其中req、res为express中的request/response实例,如何使用可参考express文档;
db为lowdb实例,lowdb实例上有lodash API,可以借助lodash api便利的操作集合,lowdb文档。
import {server} from 'emock-service';
let options = {
source: 'db.json',
host: '127.0.0.1',
port: 3000,
// 自动生成数据使用的json schema所在路径
schemaDir: 'path/to/schemas',
// 为每个资源自动生成的数据条数,默认值10
count: 20,
pluralOverrides: {
list(db, name, request, response, next) {},
get(db, name, request, response, next) {},
create(db, name, request, response, next) {},
update(db, name, request, response, next) {},
delete(db, name, request, response, next) {}
}
};
server(options);
参数说明:
有些常量数据,无法或者不需要使用json schema生成,可将其放在一个json文件中,emock-server启动时,从该文件中读取数据,copy到数据库。
为所有自动生成的api增加urlPrefix
前缀。
MIT
FAQs
emock
We found that emock-service 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
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.