mirror of
https://github.com/thaldrin/brevis.git
synced 2024-08-15 03:03:56 +00:00
get shorten creation to work
This commit is contained in:
parent
60280bed33
commit
ca83b1bb6c
1 changed files with 55 additions and 7 deletions
62
index.ts
62
index.ts
|
@ -1,8 +1,9 @@
|
|||
import express from "express";
|
||||
import helmet from "helmet";
|
||||
import { createClient } from "@supabase/supabase-js";
|
||||
import { Shorten } from "./src/types";
|
||||
|
||||
import { Key, Shorten } from "./src/types";
|
||||
import { logToDiscord } from "./src/utils";
|
||||
import { nanoid } from "nanoid";
|
||||
const app = express();
|
||||
const supabase = createClient(
|
||||
// @ts-ignore
|
||||
|
@ -16,10 +17,10 @@ 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);
|
||||
return res.json({
|
||||
data,
|
||||
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"
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -30,20 +31,67 @@ app.get("/:slug", async (req, res) => {
|
|||
if (data?.length === 0) {
|
||||
return res.json({
|
||||
success: false,
|
||||
msg: "The Link you are trying to visit does not exist.",
|
||||
message: "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.",
|
||||
message: "We ran into an Error while proccessing your Request.",
|
||||
});
|
||||
}
|
||||
// @ts-ignore
|
||||
return res.redirect(data[0].url);
|
||||
});
|
||||
|
||||
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({})
|
||||
})
|
||||
|
||||
// default catch all handler
|
||||
app.all("*", (req, res) => {
|
||||
res.status(404).json({
|
||||
|
|
Loading…
Reference in a new issue