diff --git a/package.json b/package.json index cb4eba4..d587565 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "tsx": "^4.19.3" }, "scripts": { - "dev": "tsx --watch --env-file-if-exists=.env index.ts", + "dev": "tsx --watch --env-file-if-exists=.env src/index.ts", "build": "node build.mjs", "start": "node build.mjs && node dist/index.js" } diff --git a/src/SpotifyAPI.ts b/src/SpotifyAPI.ts new file mode 100644 index 0000000..e69de29 diff --git a/Yapper.ts b/src/Yapper.ts similarity index 100% rename from Yapper.ts rename to src/Yapper.ts diff --git a/index.ts b/src/index.ts similarity index 68% rename from index.ts rename to src/index.ts index 390a0a1..4900b38 100644 --- a/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import Fastify from "fastify"; import { Yapper } from "./Yapper"; import path from "path"; -import { readFileSync, writeFileSync } from "fs"; +import { readFileSync, stat, writeFileSync } from "fs"; const state = new Proxy( JSON.parse(readFileSync(path.join(__dirname, "state.json"), "utf-8")), @@ -53,24 +53,28 @@ fastify.get("/auth", async (request, reply) => { fastify.get("/callback", async (request, reply) => { if ((request.query as any).error) return reply.code(400).send(); - const tokenRes = await fetch("https://accounts.spotify.com/api/token", { - method: "POST", - headers: { - "Content-Type": "application/x-www-form-urlencoded", - Authorization: - "Basic " + - Buffer.from( - process.env.CLIENT_ID + ":" + process.env.CLIENT_SECRET - ).toString("base64") - }, - body: new URLSearchParams({ - code: (request.query as any).code, - redirect_uri: `https://${request.hostname}/callback`, - grant_type: "authorization_code" + const tokenRes = await ( + await fetch("https://accounts.spotify.com/api/token", { + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded", + Authorization: + "Basic " + + Buffer.from( + process.env.CLIENT_ID + ":" + process.env.CLIENT_SECRET + ).toString("base64") + }, + body: new URLSearchParams({ + code: (request.query as any).code, + redirect_uri: `https://${request.hostname}/callback`, + grant_type: "authorization_code" + }) }) - }); + ).json(); - console.log(await tokenRes.json()); + state.accessToken = tokenRes.access_token; + state.explodesAt = Date.now() + tokenRes.expires_in * 1000; + state.refreshToken = tokenRes.refresh_token; }); try {