From 11bd1b4f1f4d079b3ca047472da9da34fb64732d Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 25 Sep 2021 18:28:49 +0200 Subject: [PATCH] Added 'clientside' flag to getters and setters --- src/server.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/server.js b/src/server.js index 307a3b3..1891450 100644 --- a/src/server.js +++ b/src/server.js @@ -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) {