Added 'clientside' flag to getters and setters

This commit is contained in:
Nicolai Ort 2021-09-25 18:28:49 +02:00
parent e3214084f6
commit 11bd1b4f1f
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
1 changed files with 12 additions and 7 deletions

View File

@ -104,7 +104,7 @@ fastify.get('/:shortcode', async (req, res) => {
if (!shortcode) {
return 404;
}
const target = await knex.select('target', 'no_preview')
const target = await knex.select('target', 'no_preview', 'clientside')
.from('urls')
.where('shortcode', '=', shortcode)
.limit(1);
@ -154,6 +154,7 @@ const newUrlSchema = {
target: { type: 'string' },
shortcode: { type: 'string' },
no_preview: { type: 'boolean' },
clientside: { type: 'boolean' }
}
}
};
@ -163,6 +164,7 @@ fastify.post('/api', { newUrlSchema }, async (req, res) => {
const target = req.body?.target;
let shortcode = req.body?.shortcode;
let no_preview = req.body?.no_preview || false;
let clientside = req.body?.clientside || false;
//Check if the user provided a target
if (!target) {
@ -182,7 +184,7 @@ fastify.post('/api', { newUrlSchema }, async (req, res) => {
return response;
}
}
const exists = await knex.select('shortcode', 'no_preview')
const exists = await knex.select('shortcode', 'no_preview', 'clientside')
.from('urls')
.where('target', '=', target)
.limit(1);
@ -192,7 +194,8 @@ fastify.post('/api', { newUrlSchema }, async (req, res) => {
url: `${config.getBaseUrl()}/${shortcode}`,
shortcode,
target,
no_preview: exists[0].no_preview
no_preview: exists[0].no_preview,
clientside: exists[0].clientside
}
}
shortcode = uniqid();
@ -214,13 +217,14 @@ fastify.post('/api', { newUrlSchema }, async (req, res) => {
}
//Create a new db entry
await knex('urls').insert({ target, shortcode, no_preview });
await knex('urls').insert({ target, shortcode, no_preview, clientside });
return {
url: `${config.getBaseUrl()}/${shortcode}`,
shortcode,
target,
no_preview
no_preview,
clientside
}
});
@ -247,7 +251,7 @@ fastify.get('/api/:shortcode', async (req, res) => {
return 404;
}
const exists = await knex.select('shortcode', 'target', 'no_preview')
const exists = await knex.select('shortcode', 'target', 'no_preview', 'clientside')
.from('urls')
.where('shortcode', '=', shortcode)
.limit(1);
@ -264,6 +268,7 @@ fastify.get('/api/:shortcode', async (req, res) => {
shortcode: exists[0].shortcode,
target: exists[0].target,
no_preview: exists[0].no_preview,
clientside: exists[0].clientside,
visits: visits.length
}
});
@ -350,7 +355,7 @@ fastify.after(() => {
//Get all urls api route
fastify.get('/api', { onRequest: fastify.auth([fastify.basicAuth, fastify.verifyJWT]) }, async (req, res) => {
urls = await knex.select('target', 'shortcode', 'no_preview')
urls = await knex.select('target', 'shortcode', 'no_preview', 'clientside')
.from('urls');
for (let url of urls) {