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

emock-service

v0.1.7

Published

emock

Downloads

4

Readme

emock-service

Downloads Downloads npm version dependencies dev dependencies License

emock-service提供模拟REST API服务,仅通过一些简单逻辑,就可以实现一个比较『完整、真实』的后端。在前后端分离开发的项目中,帮助前端人员快速开发。

特点

  • 调试数据
    • 根据资源的json schema描述随机生成调试需要的数据;
    • 开发人员手工构造json数据;
  • 使用json文件存储数据,模拟增删改查;
  • 自动生成资源的restful api;
  • 自定义路由,通过简单的代码处理请求;

安装

npm install emock-service

使用

  1. 撰写json schema或者构造json数据;
  2. 自定义路由(可跳过);
  3. 启动server。

示例

构造json数据

项目下新建db.json文件,增加以下内容:

{
    "users": [
        {
            "id": 1,
            "name": "jack"
        },
        {
            "id": 2,
            "name": "rose"
        }
    ]
}

启动server

import {server} from 'emock-service';

let options = {
    source: 'db.json',
    host: '127.0.0.1',
    port: 3000
};
server(options);

emock-service自动生成以下服务:

  • GET /users

    支持任意字段排序、分页、全字段搜索

    • order: descasc
    • orderBy: 字段名
    • page: 页码
    • pageSize: 每页显示条数
    • keyword: 搜索关键字
  • GET /users/:id

  • POST /users

  • PUT /users/:id

  • DELETE /users/:id

浏览器地址栏输入http://127.0.0.1:3000/users,可看到如下结果: 图片

利用json schema自动生成数据

emock-service可根据实体的json schema定义自动生成测试数据,目前json schema需满足以下要求:

  • 所有资源的json schema文件放在同一目录
  • 文件扩展名必须为.json
  • 使用id标识资源名称(待改进,json schema规范中id为当前json schema的资源标识符)
  • 为了生成看起来合法的数据,需要利用json schema关键字尽可能全面描述资源

json schema扩展字段

faker, chance

mock-service使用json-schema-faker生成实体数据,因此,需要在json schema的字段定义中引入faker, chance,调用fakerjs/chancejs对应的方法随机生成数据。

json schema示例

{
    "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'
        },
    }
};

上述对象表达的关联关系为:

  • 一个usercompanyId字段关联一个companyid字段;
  • 一个usercompanyIds字段关联多个companyid字段

建立关联关系的要求

  • 关联其他资源的资源,必须提供schema定义
  • 非必需字段,不作关联

关联关系的使用

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);

参数说明:

  • db: 数据库实例
  • name: 资源名称,复数
  • request/response/next: express中相应对象

其他配置

constant:常量数据文件路径

有些常量数据,无法或者不需要使用json schema生成,可将其放在一个json文件中,emock-server启动时,从该文件中读取数据,copy到数据库。

urlPrefix

为所有自动生成的api增加urlPrefix前缀。

License

MIT