feat: 新增英雄单查、新增、更新、删除接口及API文档

新增了英雄相关的四个核心API接口:单英雄查询、新增/覆盖英雄、局部更新英雄、删除英雄,并补充了详细的API调用规范文档,方便智能体配置英雄。
This commit is contained in:
panFD
2026-10-05 15:10:39 +08:00
parent 8c7748436b
commit 0db3404481
2 changed files with 349 additions and 0 deletions

View File

@@ -142,6 +142,78 @@ app.post('/api/heroes', async (req, res) => {
}
})
/** 单英雄读取:GET /api/heroes/:uuid */
app.get('/api/heroes/:uuid', async (req, res) => {
try {
const uuid = Number(req.params.uuid)
if (Number.isNaN(uuid)) return res.status(400).json({ error: 'uuid 必须为数字' })
const heroes = await store.getHeroes()
const hero = heroes.find((h) => h.uuid === uuid)
if (!hero) return res.status(404).json({ error: `英雄 ${uuid} 不存在` })
res.json(hero)
} catch (e) {
res.status(500).json({ error: String(e?.message || e) })
}
})
/** 单英雄新增/覆盖:POST /api/heroes/:uuid(body 为完整英雄对象,uuid 以 URL 为准) */
app.post('/api/heroes/:uuid', async (req, res) => {
try {
if (!(await guardVersion(res))) return
const uuid = Number(req.params.uuid)
if (Number.isNaN(uuid)) return res.status(400).json({ error: 'uuid 必须为数字' })
const hero = req.body
if (!hero || typeof hero !== 'object') return res.status(400).json({ error: 'body 必须为英雄对象' })
hero.uuid = uuid
const heroes = await store.getHeroes()
const idx = heroes.findIndex((h) => h.uuid === uuid)
if (idx >= 0) heroes[idx] = hero
else heroes.push(hero)
await store.saveHeroes(heroes)
res.json({ ok: true, uuid, action: idx >= 0 ? 'updated' : 'created' })
} catch (e) {
res.status(500).json({ error: String(e?.message || e) })
}
})
/** 单英雄局部更新:PUT /api/heroes/:uuid(body 为部分字段,与现有英雄浅合并) */
app.put('/api/heroes/:uuid', async (req, res) => {
try {
if (!(await guardVersion(res))) return
const uuid = Number(req.params.uuid)
if (Number.isNaN(uuid)) return res.status(400).json({ error: 'uuid 必须为数字' })
const patch = req.body
if (!patch || typeof patch !== 'object') return res.status(400).json({ error: 'body 必须为部分字段对象' })
const heroes = await store.getHeroes()
const idx = heroes.findIndex((h) => h.uuid === uuid)
if (idx < 0) return res.status(404).json({ error: `英雄 ${uuid} 不存在` })
// uuid 不可变,防止 patch 覆盖
delete patch.uuid
heroes[idx] = { ...heroes[idx], ...patch }
await store.saveHeroes(heroes)
res.json({ ok: true, uuid, action: 'patched' })
} catch (e) {
res.status(500).json({ error: String(e?.message || e) })
}
})
/** 单英雄删除:DELETE /api/heroes/:uuid */
app.delete('/api/heroes/:uuid', async (req, res) => {
try {
if (!(await guardVersion(res))) return
const uuid = Number(req.params.uuid)
if (Number.isNaN(uuid)) return res.status(400).json({ error: 'uuid 必须为数字' })
const heroes = await store.getHeroes()
const idx = heroes.findIndex((h) => h.uuid === uuid)
if (idx < 0) return res.status(404).json({ error: `英雄 ${uuid} 不存在` })
heroes.splice(idx, 1)
await store.saveHeroes(heroes)
res.json({ ok: true, uuid, action: 'deleted' })
} catch (e) {
res.status(500).json({ error: String(e?.message || e) })
}
})
/** 预设库:GET 从线上库读取 */
app.get('/api/templates', async (_req, res) => {
try {