And that should be it

This commit is contained in:
cirroskais 2024-03-18 16:00:01 -04:00
parent cb6e42096b
commit 54957dd711
No known key found for this signature in database
GPG key ID: 5FC73EBF2678E33D
3 changed files with 52 additions and 1 deletions

6
src/env.d.ts vendored Normal file
View file

@ -0,0 +1,6 @@
declare module "bun" {
interface Env {
DISCORD_CLIENT_ID: string;
DISCORD_CLIENT_SECRET: string;
}
}

View file

@ -1 +1,11 @@
console.log("Hello via Bun!");
import token from "./routes/token";
Bun.serve({
port: 3000,
async fetch(req) {
const url = new URL(req.url);
if (url.pathname === "/") return new Response("hello yes this is garf");
if (url.pathname === "/api/token") return await token(req);
return new Response("garf dont know what want...");
},
});

35
src/routes/token.ts Normal file
View file

@ -0,0 +1,35 @@
interface TokenRequest {
code: string;
}
interface TokenResponse {
access_token: string;
token_type: string;
expires_in: number;
refresh_token: string;
scope: string;
}
export default async function (req: Request): Promise<Response> {
if (req.method !== "post") return new Response("garf expected a POST request...");
if (!req.headers.get("Content-Type")) return new Response("garf expected some jay sawn...");
if (!req.headers.get("Content-Type")?.includes("application/json")) return new Response("garf expected some jay sawn...");
const body = (await req.json()) as TokenRequest;
if (!body) return new Response("garf expected some jay sawn...");
const response = await fetch("https://discord.com/api/oauth2/token", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
client_id: process.env.DISCORD_CLIENT_ID,
client_secret: process.env.DISCORD_CLIENT_SECRET,
grant_type: "authorization_code",
code: body?.code,
}),
});
const { access_token } = (await response.json()) as TokenResponse;
return new Response(access_token);
}