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