2021-04-12 21:34:44 +00:00
|
|
|
import express from "express";
|
|
|
|
import helmet from "helmet";
|
|
|
|
import { createClient } from "@supabase/supabase-js";
|
|
|
|
import { Shorten } from "./src/types";
|
|
|
|
|
|
|
|
const app = express();
|
2021-04-13 00:27:42 +00:00
|
|
|
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" }));
|
2021-04-12 21:34:44 +00:00
|
|
|
app.use(helmet());
|
|
|
|
|
|
|
|
app.get("/", async (req, res) => {
|
2021-04-13 00:27:42 +00:00
|
|
|
let { data, error } = await supabase.from<Shorten>("brevis").select();
|
|
|
|
if (error) return res.status(400).json(error);
|
2021-04-12 21:34:44 +00:00
|
|
|
return res.json({
|
2021-04-13 00:27:42 +00:00
|
|
|
data,
|
|
|
|
});
|
|
|
|
});
|
2021-04-12 21:34:44 +00:00
|
|
|
|
|
|
|
app.get("/:slug", async (req, res) => {
|
2021-04-13 00:27:42 +00:00
|
|
|
let { data, error } = await supabase
|
|
|
|
.from<Shorten>("brevis").select().eq("slug", req.params.slug).limit(1);
|
|
|
|
|
|
|
|
if (data?.length === 0) {
|
|
|
|
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);
|
|
|
|
});
|
2021-04-12 21:34:44 +00:00
|
|
|
|
|
|
|
// default catch all handler
|
2021-04-13 00:27:42 +00:00
|
|
|
app.all("*", (req, res) => {
|
2021-04-12 21:34:44 +00:00
|
|
|
res.status(404).json({
|
|
|
|
success: false,
|
2021-04-13 00:27:42 +00:00
|
|
|
message: "route not defined",
|
2021-04-12 21:34:44 +00:00
|
|
|
data: null,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = app;
|