From 0565a06bde4eee6bec8455923c268338ffd35b92 Mon Sep 17 00:00:00 2001 From: Lio Young Date: Tue, 13 Apr 2021 02:27:42 +0200 Subject: [PATCH] handle redirects // formatting --- index.ts | 55 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/index.ts b/index.ts index f5be057..0a08f22 100644 --- a/index.ts +++ b/index.ts @@ -4,41 +4,52 @@ import { createClient } from "@supabase/supabase-js"; import { Shorten } from "./src/types"; const app = express(); -// @ts-ignore -const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_KEY) +const supabase = createClient( + // @ts-ignore + process.env.SUPABASE_URL, + // @ts-ignore + process.env.SUPABASE_KEY +); - -app.use(express.json({ limit: '50mb' })); -app.use(express.urlencoded({ extended: true, limit: '50mb' })); +app.use(express.json({ limit: "50mb" })); +app.use(express.urlencoded({ extended: true, limit: "50mb" })); app.use(helmet()); - app.get("/", async (req, res) => { - let { data, error } = await supabase.from("brevis").select() - if (error) return res.status(400).json(error) - - console.log(data) - + let { data, error } = await supabase.from("brevis").select(); + if (error) return res.status(400).json(error); return res.json({ - data - }) -}) + data, + }); +}); app.get("/:slug", async (req, res) => { - let { data, error } = await supabase.from("brevis").select() - if (error) return res.status(400).json(error) + let { data, error } = await supabase + .from("brevis").select().eq("slug", req.params.slug).limit(1); - return res.json({ - data: data - }) -}) + if (data?.length === 0) { + console.log("hewo"); + return res.json({ + success: false, + msg: "The Link you are trying to visit does not exist.", + }); + } + if (error !== null) { + return res.json({ + success: false, + msg: "We ran into an Error while proccessing your Request.", + }); + } + // @ts-ignore + return res.redirect(data[0].url); +}); // default catch all handler -app.all('*', (req, res) => { +app.all("*", (req, res) => { res.status(404).json({ success: false, - message: 'route not defined', + message: "route not defined", data: null, }); });