diff --git a/src/env.d.ts b/src/env.d.ts new file mode 100644 index 0000000..936aef7 --- /dev/null +++ b/src/env.d.ts @@ -0,0 +1,6 @@ +declare module "bun" { + interface Env { + DISCORD_CLIENT_ID: string; + DISCORD_CLIENT_SECRET: string; + } +} diff --git a/src/index.ts b/src/index.ts index f67b2c6..f5ae705 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,11 @@ -console.log("Hello via Bun!"); \ No newline at end of file +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..."); + }, +}); diff --git a/src/routes/token.ts b/src/routes/token.ts new file mode 100644 index 0000000..7e173d0 --- /dev/null +++ b/src/routes/token.ts @@ -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 { + 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); +}