From 33a359cc71b76653ee335c048468a0afce63c60d Mon Sep 17 00:00:00 2001 From: Wan <495709+wa0x6e@users.noreply.github.com> Date: Mon, 28 Aug 2023 10:55:16 +0900 Subject: [PATCH] fix: fix error when msg.space is empty (#140) * fix: fix error when msg.space is empty * fix: validate message before reading * chore: refactoring --- src/api.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/api.ts b/src/api.ts index 263f751..6e394ab 100644 --- a/src/api.ts +++ b/src/api.ts @@ -66,12 +66,20 @@ router.get('/api/messages/:hash', async (req, res) => { }); router.post('/', async (req, res) => { - if (!req.body.data || !req.body.data.message) { + const msg = req.body.data?.message; + + if (!msg) { return res.status(400).json({ error: 'Invalid format request' }); } + if (!req.body.data.types.Space && !msg.settings && !msg.space) { + return res.status(400).json({ + error: 'Missing space' + }); + } + let address; try { address = getAddress(req.body.address); @@ -82,7 +90,6 @@ router.post('/', async (req, res) => { } try { - const msg = req.body.data.message; const msgHash = snapshot.utils.getHash(req.body.data); const env = 'livenet'; let network = env === 'livenet' ? '1' : '5';