1
1
mirror of https://github.com/ZeroCatDev/ClassworksKV.git synced 2025-12-07 21:13:10 +00:00

feat: 增强POST /:key处理,支持空值和JSON格式验证

This commit is contained in:
Sunwuyuan 2025-12-06 13:09:00 +08:00
parent 1f68aea39f
commit da633ca5b6
No known key found for this signature in database
GPG Key ID: A6A54CF66F56BB64

View File

@ -354,9 +354,21 @@ router.post(
const deviceId = res.locals.deviceId; const deviceId = res.locals.deviceId;
const { key } = req.params; const { key } = req.params;
const value = req.body; let value = req.body;
// 处理空值,转换为空对象
if (value === null || value === undefined || value === '') {
value = {};
}
// 验证是否能被 JSON 序列化
try {
JSON.stringify(value);
} catch (error) {
return next(
errors.createError(400, "无效的数据格式")
);
}
// 获取客户端IP // 获取客户端IP
const creatorIp = const creatorIp =
@ -366,7 +378,7 @@ router.post(
req.connection.socket?.remoteAddress || req.connection.socket?.remoteAddress ||
""; "";
const result = await kvStore.upsert(deviceId, key, value, creatorIp); const result = kvStore.upsert(deviceId, key, value, creatorIp);
// 广播单个键的变更 // 广播单个键的变更
const uuid = res.locals.device?.uuid; const uuid = res.locals.device?.uuid;