@finley_ge/ezmock
v1.0.0
Published
Easy API mocking tool built with Bun
Readme
EzMock
一个基于 Bun + TypeScript 的简单易用的 API Mock 工具。通过 CLI 的方式快速启动一个 Mock 服务器,支持 JSON 配置和交互式配置。
功能特性
- 🚀 基于 Bun 构建,启动速度快
- 📋 支持 JSON 配置文件加载
- 🖥️ 交互式 CLI 配置
- 🎯 支持多种响应类型(JSON、文本、文件)
- 🔀 支持参数化路由(如
/api/users/:id) - ⏱️ 支持响应延迟配置
- 🌐 内置 CORS 支持
- 📝 请求日志记录
安装和使用
前置要求
确保已安装 Bun:
curl -fsSL https://bun.sh/install | bash安装依赖
bun install编译二进制文件
编译为本机可执行文件:
# 编译为当前平台的二进制文件
bun run build
# 编译为特定平台
bun run build:macos # macOS (Apple Silicon)
bun run build:linux # Linux (x64)
bun run build:windows # Windows (x64)
# 编译所有平台
bun run build:all编译后可以直接运行二进制文件,无需安装 Bun:
./ezmock -c example.json使用方法
1. 使用配置文件启动
bun start -c example.json2. 交互式配置
bun start3. 命令行参数
使用源码:
# 查看帮助
bun start --help
# 指定端口和主机
bun start -p 8080 -h 0.0.0.0
# 加载配置文件并覆盖端口
bun start -c config.json -p 8080
# 启用请求日志(包含 URL、headers 和 payload)
bun start -c config.json --logging使用编译后的二进制:
# 查看帮助
./ezmock --help
# 指定端口和主机
./ezmock -p 8080 -h 0.0.0.0
# 加载配置文件并覆盖端口
./ezmock -c config.json -p 8080
# 启用请求日志(包含 URL、headers 和 payload)
./ezmock -c config.json --logging请求日志默认关闭,只能通过启动参数 -l 或 --logging 开启。日志可能包含认证 header 和请求 payload 中的敏感信息,请仅在可信的调试环境中使用。
配置文件格式
{
"port": 3000,
"host": "localhost",
"cors": true,
"routes": [
{
"method": "GET",
"path": "/api/users",
"type": "json",
"statusCode": 200,
"response": [
{ "id": 1, "name": "John Doe", "email": "[email protected]" }
]
},
{
"method": "GET",
"path": "/api/users/:id",
"type": "json",
"statusCode": 200,
"response": { "id": 1, "name": "John Doe", "email": "[email protected]" }
},
{
"method": "POST",
"path": "/api/users",
"type": "json",
"statusCode": 201,
"response": { "id": 2, "name": "New User" },
"delay": 500
},
{
"method": "GET",
"path": "/hello",
"type": "text",
"response": "Hello World!"
}
]
}路由配置参数
method: HTTP 方法(GET, POST, PUT, DELETE, PATCH)path: 路由路径,支持参数化(如/api/users/:id)type: 响应类型(json、text、file、stream)response: 响应内容statusCode: HTTP 状态码(可选,默认 200)delay: 响应延迟(毫秒,可选)headers: 自定义响应头(可选)stream: 流式响应配置(仅当type为stream时使用)mode: 流式模式(line为 SSE 格式,chunk为原始块)chunks: 数据块数组(字符串或对象)chunkDelay: 块之间的延迟(毫秒,可选,默认 100)
示例
启动示例服务器
# 启动标准示例
bun start -c example.json
# 启动流式响应示例
bun start -c stream-example.json测试 API
# 获取用户列表
curl http://localhost:3000/api/users
# 获取特定用户
curl http://localhost:3000/api/users/123
# 创建用户
curl -X POST http://localhost:3000/api/users \
-H "Content-Type: application/json" \
-d '{"name":"Test User"}'
# 健康检查
curl http://localhost:3000/health
# 文本响应
curl http://localhost:3000/hello测试流式响应
# 测试 Server-Sent Events (SSE)
curl -N http://localhost:3000/stream/sse
# 测试 JSON 流
curl -N http://localhost:3000/stream/json
# 测试原始数据块流
curl -N http://localhost:3000/stream/chunks
# 测试模拟 LLM 响应
curl -N http://localhost:3000/stream/llm-response
# 测试聊天流式响应
curl -N -X POST http://localhost:3000/stream/chat流式响应配置示例
{
"method": "GET",
"path": "/stream/sse",
"type": "stream",
"statusCode": 200,
"stream": {
"mode": "line",
"chunkDelay": 500,
"chunks": [
"First message",
"Second message",
"Third message"
]
}
}SSE 模式会自动格式化为 data: <content>\n\n 格式,适用于 Server-Sent Events 客户端。
开发
开发模式运行
bun run dev运行测试
bun test许可证
MIT
