taevas.xyz/netlify/functions/osu_token.ts
Taevas 719672ffa0 Load an Info's Website separately from the Info itself
Makes it so:
- An `Info`'s `Website` doesn't need to wait for other `Website`s of that same `Info` to load to show up
- An `Info`s `Website` not working won't prevent other `Website`s of that same `Info` from showing up
- Code is more split and organized

Furthermore, the token for the osu! API is now stored, and used for ALL osu! requests for 24 hours instead of being revoked

Overall, it's a lot of future-proofing so things on working even if I'm no longer there to maintain them
Also so `Info`s can be added, changed, and removed more easily
2024-05-04 19:14:18 +02:00

31 lines
803 B
TypeScript

import {type Handler} from "@netlify/functions";
import {API} from "osu-api-v2-js";
const handler: Handler = async () => {
const [token, expiration] = [process.env["OSU_TOKEN"], process.env["OSU_TOKEN_EXPIRATION"]];
let expired = false;
if (expiration) {
try {
expired = new Date(expiration) < new Date();
} catch {
expired = true;
}
} else {
expired = true;
}
if (!token || expired) {
console.log("Setting a new token for osu!...");
const api = await API.createAsync({id: 11451, secret: process.env.API_OSU!});
process.env["OSU_TOKEN"] = api.access_token;
process.env["OSU_TOKEN_EXPIRATION"] = api.expires.toISOString();
console.log("Successfully set a new token for osu!");
}
return {
statusCode: 200,
};
};
export {handler};