Loading...
Loading...
当用户需要模拟API、新增接口mock、启动mock服务器或管理模拟数据时,使用此技能。支持REST和SSE端点
npx skill4agent add gellenliu/devhelper api-mock-serverscripts# Start server from skill directory (default port: 3000)
node /scripts/server.js
# Or with custom port
node /scripts/server.jshttp://localhost:3000/http://localhost:3000/_listhttp://localhost:3000/_health/_api/routesGET /_health{
"status": "ok",
"timestamp": 1773384296000,
"uptime": 123.456
}POST /_api/shutdown{
"success": true,
"message": "Server shutting down..."
}http://localhost:3000/_api/routesGET /_api/routes{
"routes": [
{
"path": "/api/user",
"method": "GET",
"delay": 500,
"response": { "code": 200, "data": {} },
"createdAt": 1772527195265,
"updatedAt": 1772527195265
}
]
}POST /_api/routes
Content-Type: application/json
{
"path": "/api/users",
"method": "GET",
"delay": 200,
"response": {
"code": 200,
"message": "success",
"data": [{ "id": 1, "name": "John" }]
}
}GET /_api/routes/:keyMETHOD:PATHGET:/api/userSSE:METHOD:PATHSSE:GET:/sse/chatGET /_api/routes/GET:/api/usersPUT /_api/routes/:key
Content-Type: application/json
{
"delay": 1000,
"response": {
"code": 200,
"data": [{ "id": 1, "name": "Updated" }]
}
}DELETE /_api/routes/:keyDELETE /_api/routes/GET:/api/usersPOST /_api/routes/clearGET /_api/routes/exportPOST /_api/routes/import
Content-Type: application/json
{
"routes": [
{
"path": "/api/imported",
"method": "GET",
"response": { "code": 200 }
}
]
}{
"path": "/api/example",
"method": "GET",
"delay": 0,
"headers": { "X-Custom": "value" },
"response": {
"code": 200,
"message": "success",
"data": {}
},
"conditions": [
{
"match": {
"body.action": { "operator": "equals", "value": "login" }
},
"response": { "code": 200, "data": { "token": "xxx" } }
}
]
}{
"path": "/sse/stream",
"method": "GET",
"type": "sse",
"delay": 0,
"interval": 100,
"repeat": false,
"events": [
{ "data": "{\"type\":\"start\"}" },
{ "data": "{\"type\":\"message\",\"content\":\"Hello\"}" },
{ "data": "{\"type\":\"end\"}" }
]
}| Condition Type | Description |
|---|---|
| Match request body field |
| Match URL query parameter |
| Match request header |
| Match path parameter (for dynamic paths) |
equalsnotEqualscontainsstartsWithendsWithregexexistsinnotIngtgteltlte{
"path": "/api/order",
"method": "POST",
"conditions": [
{
"match": {
"body.status": { "operator": "equals", "value": "pending" }
},
"response": { "code": 200, "data": { "approved": false } }
},
{
"match": {
"body.status": { "operator": "equals", "value": "approved" }
},
"response": { "code": 200, "data": { "approved": true } }
}
],
"response": { "code": 400, "message": "Unknown status" }
}curl -X POST http://localhost:3000/_api/routes \
-H "Content-Type: application/json" \
-d '{"path":"/api/products","method":"GET","response":{"code":200,"data":[{"id":1,"name":"Product A"}]}}'curl -X POST http://localhost:3000/_api/routes \
-H "Content-Type: application/json" \
-d '{
"path": "/api/login",
"method": "POST",
"delay": 500,
"response": {
"code": 200,
"data": { "token": "mock-jwt-token", "expiresIn": 3600 }
}
}'curl -X POST http://localhost:3000/_api/routes \
-H "Content-Type: application/json" \
-d '{
"path": "/api/users",
"method": "GET",
"response": {
"code": 200,
"data": {
"list": [{"id":1,"name":"User1"},{"id":2,"name":"User2"}],
"total": 100,
"page": 1,
"pageSize": 10
}
}
}'curl -X POST http://localhost:3000/_api/routes \
-H "Content-Type: application/json" \
-d '{
"path": "/api/error",
"method": "GET",
"response": {
"code": 500,
"message": "Internal Server Error"
}
}'curl -X POST http://localhost:3000/_api/routes \
-H "Content-Type: application/json" \
-d '{
"path": "/sse/notifications",
"method": "GET",
"type": "sse",
"interval": 1000,
"repeat": true,
"events": [
{"data": "{\"type\":\"notification\",\"message\":\"New message\"}"}
]
}'curl -X POST http://localhost:3000/_api/shutdownconst MockServer = require('./.trae/skills/api-mock-server/scripts/server.js');
// Or use the API client
const baseUrl = 'http://localhost:3000';
// Create route
await fetch(`${baseUrl}/_api/routes`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
path: '/api/test',
method: 'GET',
response: { code: 200, data: { message: 'Hello' } }
})
});
// List routes
const res = await fetch(`${baseUrl}/_api/routes`);
const { routes } = await res.json();
// Delete route
await fetch(`${baseUrl}/_api/routes/GET:/api/test`, { method: 'DELETE' });scripts/config/scripts/config/interfaces.jsscripts/config/sse-interfaces.js.envscripts/PORT=3000
ADMIN_PASSWORD=admin123scripts/data/routes.jsonGET /_healthGET /_api/routesnode .trae/skills/api-mock-server/scripts/server.jsPOST /_api/shutdownDELETE /_api/routes/:keyPOST /_api/routes/cleardelayMETHOD:PATHSSE:METHOD:PATH