Added api route for getting basic information about an url

This commit is contained in:
Nicolai Ort 2021-08-14 09:03:18 +02:00
parent a6397f01d1
commit 1ac700f584
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
1 changed files with 28 additions and 4 deletions

View File

@ -35,7 +35,7 @@ fastify.get('/a/:id', async (req, res) => {
fastify.get('/yt/:id', async (req, res) => {
res.redirect(302, `https://youtu.be/${req.params.id}`)
})
//Automagic Youtube redirects on /ytpl/
//Automagic Youtube Playlist redirects on /ytpl/
fastify.get('/ytpl/:id', async (req, res) => {
res.redirect(302, `https://youtube.com/playlist?list=${req.params.id}`)
})
@ -69,8 +69,8 @@ const newUrlSchema = {
}
};
//Create new url route
fastify.post('/api/new', { newUrlSchema }, async (req, res) => {
//Create new url api route
fastify.post('/api', { newUrlSchema }, async (req, res) => {
const target = req.body?.target;
let shortcode = req.body?.shortcode;
@ -132,6 +132,30 @@ fastify.post('/api/new', { newUrlSchema }, async (req, res) => {
}
});
//Get url api route
fastify.get('/api/:shortcode', async (req, res) => {
const shortcode = req.params.shortcode;
//This should never happen but better safe than 500
if (!shortcode) {
return 404;
}
const exists = await knex.select('shortcode', 'target')
.from('urls')
.where('shortcode', '=', shortcode)
.limit(1);
if (exists.length == 0) {
return 404;
}
return {
url: `${config.getBaseUrl()}/${exists[0].shortcode}`,
shortcode: exists[0].shortcode,
target: exists[0].target
}
});
/**
* Checks for some default providers with custom url schemes (amazon and youtube r/n)
* @param {string} target The target URL