1.0.2 • Published 2 years ago

@breezez/m2mock v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

m2mock

m2mock 即markdown to mock,本项目是解析markdown接口文档为mock json的一个命令行工具。接口文档必须满足一定规则,并不能灵活处理各种格式。

该项目旨在分享一下思路: 1. 将markdown转换为html字符,这里用markdown-it; 2. html字符利用DOM解析工具处理,这里用cheerio; 3. 核心内容是根据DOM树规律,提取以下信息:请求方法、请求路径、请求参数、响应参数、参数类型等信息;对于mock数据,只关注请求方法、请求路径、响应参数和参数类型等; 4. 取得需要的信息后,再利用mock.js即可生成一个json假数据。

使用方法

m2mock -f example.md -n mock.js
  • -f, --from <file> : 原始mackdown文件
  • -n, --name <filename>: 输出文件名,默认为./mock.json

接口文档示例

接口文件markdown文件,需要满足以下规则:

通话明细搜索接口
"""
GET '/api/search'

PARAMS:
* `keyword` (str) - 关键词、句,用空格' '隔开,不用逗号是防止句子中出现逗号。
* `caller` (str) **optional** - 拨打人\部门id,用英文半角逗号','隔开,部门id前加字母D
* `call_time_begin` (timestamp) **optional** - 呼叫时间起始值(13位,含)
* `call_time_end` (timestamp) **optional** - 呼叫时间结束值(13位,含)
* `duration_begin` (int) **optional** - 通话时长起始值(秒、含)
* `duration_end` (int) **optional** - 通话时长结束值(秒、含)
* `rank_model` (str) **optional** - 通话评级模型id
* `rank_value` (str) **optional** - 通话模型评级id
* `callee_number` (str) **optional** - 被叫号码
* `company_name` (str) **optional** - 公司名称
* `call_id` (str) **optional** - 通话ID
* `sort` (int) **optional** - 数据排序: 1-呼叫时间升序;2-呼叫时间降序;3-通话时长升序;4-通话时长降序
    * `1` (int) - 呼叫时间升序
    * `2` (int) - 呼叫时间降序 (默认)
    * `3` (int) - 通话时长升序
    * `4` (int) - 通话时长降序
* `page` (int) - 页码,默认1, 这里还没有确定,可能要采用缓存数据的方法
* `limit` (int) - 每页返回数据量,默认10
* `search_after` (str) - 如果翻页前的返回是非算法类型的,翻页时带上这个参数

RETURNS:
* `total` (int) - 共有多少条数据,虚数
* `type` (int) - 搜索类型
    * `0` (int) - 非算法
    * `1` (int) - 算法
* `data` (list of dict)
    * `_id` (str) - 实际存储id
    * `call_id` (str) - 通话ID
    * `tags` (list) - 标签列表
        * `tag_name` (str) - 标签名
        * `count` (int) - 出现次数
        * `color` (int) - 标签颜色:1-蓝色;2-红色
            * `1` (int) - 蓝色
            * `2` (int) - 红色
    * `rank` (list of dict) - 通话评级列表
        * `_id` (str) - 评级id
        * `name` (str) - 评级名
    * `caller_id` (str) - 拨打人id
    * `caller_name` (str) - 拨打人名
    * `department_id` (str) - 拨打人所属部门id
    * `department_name` (str) - 拨打人所属部门名
    * `callee_number` (str) - 被叫号码
    * `callee_operater` (str) - 被叫号码归属运营商
    * `callee_company_name` (str) - 被叫号所属公司名
    * `callee_user_name` (str) - 被叫号所属人
    * `call_time` (timestamp) - 呼叫时间(13位)
    * `duration` (int) - 通话时长(秒)
    * `voice_oss_key` (str) - 录音播放地址
    * `hit_text` (list of dict) - 命中文本
        * `start` (int) - 命中对话开始时间(秒)
        * `text` (str) - 命中对话文本
* `search_after` (str) - 非算法类翻页参数
"""
  • GET '/api/search' 请求方法大写 + 请求路径在同一行;
  • PARAMS 单独一行,用来识别参数
  • RETURNS 单独一行,用来识别响应
  • 参数通过缩进表示父子关系
  • `total` (int) - 共有多少条数据,虚数: 每个参数包括:包裹的属性名、括号包裹的类型,以及中华线后面的说明。

解析结果示例

{
  "GET /api/search": {
    "total": 147,
    "type": "1",
    "data": [
      {
        "_id": "任娜",
        "call_id": "钱丽",
        "tags": [
          { "tag_name": "程丽", "count": 38, "color": "2" },
          { "tag_name": "程丽", "count": 38, "color": "2" }
        ],
        "rank": "name",
        "caller_id": "田勇",
        "caller_name": "沈勇",
        "department_id": "姜强",
        "department_name": "范磊",
        "callee_number": "黄磊",
        "callee_operater": "周洋",
        "callee_company_name": "秦娜",
        "callee_user_name": "王娜",
        "call_time": 1668658968916,
        "duration": 16,
        "voice_oss_key": "唐平",
        "hit_text": [
          { "start": 128, "text": "方敏" },
          { "start": 128, "text": "方敏" },
          { "start": 128, "text": "方敏" },
          { "start": 128, "text": "方敏" },
          { "start": 128, "text": "方敏" }
        ]
      },
      {
        "_id": "任娜",
        "call_id": "钱丽",
        "tags": [
          { "tag_name": "程丽", "count": 38, "color": "2" },
          { "tag_name": "程丽", "count": 38, "color": "2" }
        ],
        "rank": "name",
        "caller_id": "田勇",
        "caller_name": "沈勇",
        "department_id": "姜强",
        "department_name": "范磊",
        "callee_number": "黄磊",
        "callee_operater": "周洋",
        "callee_company_name": "秦娜",
        "callee_user_name": "王娜",
        "call_time": 1668658968916,
        "duration": 16,
        "voice_oss_key": "唐平",
        "hit_text": [
          { "start": 128, "text": "方敏" },
          { "start": 128, "text": "方敏" },
          { "start": 128, "text": "方敏" },
          { "start": 128, "text": "方敏" },
          { "start": 128, "text": "方敏" }
        ]
      }
    ],
    "search_after": "傅娟"
  }
}
  • 最终会生成mock.json文件,对应的类型会随机生成对应原始值
  • 枚举会随机返回枚举值
  • 数组会随机返回2-5组值