brevis/index.ts

104 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-04-12 21:34:44 +00:00
import express from "express";
import helmet from "helmet";
import { createClient } from "@supabase/supabase-js";
2021-04-13 20:38:29 +00:00
import { Key, Shorten } from "./src/types";
import { logToDiscord } from "./src/utils";
import { nanoid } from "nanoid";
2021-04-12 21:34:44 +00:00
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) => {
return res.json({
2021-04-13 20:38:29 +00:00
success: true,
message: "Hey, this API isn't public yet but you can look at the code over at:",
link: "https://github.com/thaldrin/brevis"
2021-04-13 00:27:42 +00:00
});
});
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,
2021-04-13 20:38:29 +00:00
message: "The Link you are trying to visit does not exist.",
2021-04-13 00:27:42 +00:00
});
}
if (error !== null) {
return res.json({
success: false,
2021-04-13 20:38:29 +00:00
message: "We ran into an Error while proccessing your Request.",
2021-04-13 00:27:42 +00:00
});
}
// @ts-ignore
return res.redirect(data[0].url);
});
2021-04-12 21:34:44 +00:00
2021-04-13 20:38:29 +00:00
app.post('/', async (req, res) => {
let { authorization } = req.headers
let { url, slug } = req.body
if (!authorization) {
return res.json({
success: false,
message: "You have not passed an API Key into the Authorization Header."
})
}
let { data: data_key, error: key_error } = await supabase.from<Key>("apikeys").select().eq('key', authorization)
// if() return
if (data_key?.length === 0) {
return res.json({
success: false,
message: "You are not Authorized to use this API."
})
}
if (!url) {
return res.json({
success: false,
message: "You need to supply a URL to shorten."
})
}
if (!slug) slug = nanoid(6)
let { data: slug_data, error: slug_error } = await supabase.from<Shorten>('brevis').select().eq('slug', slug).limit(1)
// @ts-ignore
if (slug_data[0] && slug === slug_data[0].slug) {
return res.json({
success: false,
message: "A Shorten with this Slug already exists, please choose another or try again."
})
}
let { data: insert_shorten_data, error: insert_shorten_error } = await supabase.from<Shorten>('brevis').insert({
// @ts-ignore
slug, creator: data_key[0].id, url,
})
if (process.env.BREVIS_WEBHOOK) {
// @ts-ignore
await logToDiscord(`**${data_key[0].id}** (${data_key[0].reason})\nshortened <${url}> with /${slug}`)
}
// @ts-ignore
console.log(`${data_key[0].id} (${data_key[0].reason}) shortened ${url} with /${slug}`)
return res.json({})
})
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;