handle redirects // formatting

This commit is contained in:
Lio Young 2021-04-13 02:27:42 +02:00
parent 5db5b2321b
commit 0565a06bde
No known key found for this signature in database
GPG Key ID: 789795A11879E169
1 changed files with 33 additions and 22 deletions

View File

@ -4,41 +4,52 @@ import { createClient } from "@supabase/supabase-js";
import { Shorten } from "./src/types"; import { Shorten } from "./src/types";
const app = express(); const app = express();
// @ts-ignore const supabase = createClient(
const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_KEY) // @ts-ignore
process.env.SUPABASE_URL,
// @ts-ignore
process.env.SUPABASE_KEY
);
app.use(express.json({ limit: "50mb" }));
app.use(express.json({ limit: '50mb' })); app.use(express.urlencoded({ extended: true, limit: "50mb" }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
app.use(helmet()); app.use(helmet());
app.get("/", async (req, res) => { app.get("/", async (req, res) => {
let { data, error } = await supabase.from<Shorten>("brevis").select() let { data, error } = await supabase.from<Shorten>("brevis").select();
if (error) return res.status(400).json(error) if (error) return res.status(400).json(error);
console.log(data)
return res.json({ return res.json({
data data,
}) });
}) });
app.get("/:slug", async (req, res) => { app.get("/:slug", async (req, res) => {
let { data, error } = await supabase.from<Shorten>("brevis").select() let { data, error } = await supabase
if (error) return res.status(400).json(error) .from<Shorten>("brevis").select().eq("slug", req.params.slug).limit(1);
return res.json({ if (data?.length === 0) {
data: data 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 // default catch all handler
app.all('*', (req, res) => { app.all("*", (req, res) => {
res.status(404).json({ res.status(404).json({
success: false, success: false,
message: 'route not defined', message: "route not defined",
data: null, data: null,
}); });
}); });