mirror of https://github.com/thaldrin/brevis.git
handle redirects // formatting
This commit is contained in:
parent
5db5b2321b
commit
0565a06bde
55
index.ts
55
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<Shorten>("brevis").select()
|
||||
if (error) return res.status(400).json(error)
|
||||
|
||||
console.log(data)
|
||||
|
||||
let { data, error } = await supabase.from<Shorten>("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<Shorten>("brevis").select()
|
||||
if (error) return res.status(400).json(error)
|
||||
let { data, error } = await supabase
|
||||
.from<Shorten>("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,
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue