Improve API response

This commit is contained in:
Yarmo Mackenbach 2021-04-09 14:43:08 +02:00
parent d2b9854a2f
commit 8e990458a6
No known key found for this signature in database
GPG key ID: 37367F4AF4087AD1
2 changed files with 37 additions and 55 deletions

View file

@ -44,9 +44,7 @@ if (irc_nick) {
}
router.get('/', async (req, res) => {
return res
.status(400)
.json({
return res.status(400).json({
data: [],
error:
'Available endpoints: /json/:url, /text/:url, /dns/:hostname, /xmpp/:xmppid, /twitter/:tweetid, /matrix/:roomid/:eventid, /irc/:ircserver/:ircnick',
@ -57,9 +55,7 @@ router.param('url', async (req, res, next, url) => {
req.params.url = decodeURI(url)
if (!validUrl.isUri(req.params.url)) {
return res
.status(400)
.json({ data: [], error: 'URL provided was not valid' })
return res.status(400).json({ error: 'URL provided was not valid' })
}
next()
@ -71,7 +67,7 @@ router.param('xmppid', async (req, res, next, xmppid) => {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(req.params.xmppid)) {
next()
} else {
return res.status(400).json({ data: [], error: 'XMPP_ID was not valid' })
return res.status(400).json({ error: 'XMPP_ID was not valid' })
}
})
@ -90,9 +86,7 @@ router.param('xmppdata', async (req, res, next, xmppdata) => {
]
if (!allowedData.includes(req.params.xmppdata)) {
return res
.status(400)
.send({
return res.status(400).send({
data: [],
error:
'Allowed data are: FN, NUMBER, USERID, URL, BDAY, NICKNAME, NOTE, DESC',
@ -109,11 +103,11 @@ router.get('/get/json/:url', (req, res) => {
.then(async (result) => {
return await result.json()
})
.then(async (result) => {
return res.status(200).json({ data: result, error: [] })
.then(async (data) => {
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).send({ data: [], error: err })
return res.status(400).json({ error: err })
})
})
@ -123,10 +117,10 @@ router.get('/get/text/:url', (req, res) => {
return await result.text()
})
.then(async (result) => {
return res.status(200).json({ data: result, error: [] })
return res.status(200).send(result)
})
.catch((err) => {
return res.status(400).send({ data: [], error: err })
return res.status(400).json({ error: err })
})
})
@ -134,17 +128,15 @@ router.get('/get/dns/:hostname', (req, res) => {
fetcher
.dns(req.params.hostname)
.then((data) => {
return res.status(200).json({ data: data, error: [] })
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ data: [], error: err })
return res.status(400).json({ error: err })
})
})
router.get('/get/xmpp/:xmppid', async (req, res) => {
return res
.status(400)
.send({
return res.status(400).send({
data: [],
error:
'Data request parameter missing (FN, NUMBER, USERID, URL, BDAY, NICKNAME, NOTE, DESC)',
@ -153,9 +145,7 @@ router.get('/get/xmpp/:xmppid', async (req, res) => {
router.get('/get/xmpp/:xmppid/:xmppdata', async (req, res) => {
if (!xmpp_enabled) {
return res
.status(501)
.json({ data: [], error: 'XMPP not enabled on server' })
return res.status(501).json({ error: 'XMPP not enabled on server' })
}
fetcher
@ -165,20 +155,16 @@ router.get('/get/xmpp/:xmppid/:xmppdata', async (req, res) => {
password: xmpp_password,
})
.then((data) => {
return res.status(200).json({ data: data, error: [] })
return res.status(200).send(data)
})
.catch((err) => {
return res
.status(400)
.json({ data: [], error: err.message ? err.message : err })
return res.status(400).json({ error: err.message ? err.message : err })
})
})
router.get('/get/twitter/:tweetid', async (req, res) => {
if (!twitter_enabled) {
return res
.status(501)
.json({ data: [], error: 'Twitter not enabled on server' })
return res.status(501).json({ error: 'Twitter not enabled on server' })
}
fetcher
@ -186,18 +172,16 @@ router.get('/get/twitter/:tweetid', async (req, res) => {
bearerToken: twitter_bearer_token,
})
.then((data) => {
return res.status(200).json({ data: data, error: [] })
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ data: [], error: err })
return res.status(400).json({ error: err })
})
})
router.get('/get/matrix/:matrixroomid/:matrixeventid', async (req, res) => {
if (!matrix_enabled) {
return res
.status(501)
.json({ data: [], error: 'Matrix not enabled on server' })
return res.status(501).json({ error: 'Matrix not enabled on server' })
}
fetcher
@ -206,18 +190,16 @@ router.get('/get/matrix/:matrixroomid/:matrixeventid', async (req, res) => {
accessToken: process.env.MATRIX_ACCESS_TOKEN,
})
.then((data) => {
return res.status(200).json({ data: data, error: [] })
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ data: [], error: err })
return res.status(400).json({ error: err })
})
})
router.get('/get/irc/:ircserver/:ircnick', async (req, res) => {
if (!irc_enabled) {
return res
.status(501)
.json({ data: [], error: 'IRC not enabled on server' })
return res.status(501).json({ error: 'IRC not enabled on server' })
}
fetcher
@ -225,10 +207,10 @@ router.get('/get/irc/:ircserver/:ircnick', async (req, res) => {
nick: 'doipver148927',
})
.then((data) => {
return res.status(200).json({ data: data, error: [] })
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ data: [], error: err })
return res.status(400).json({ error: err })
})
})

View file

@ -41,7 +41,7 @@ const processURI = (uri, opts) => {
claim: {
fingerprint: null,
format: 'uri',
path: ['data'],
path: [],
relation: 'contains',
},
customRequestHandler: null,