taevas.xyz/netlify/functions/speedruncom.ts

73 lines
2 KiB
TypeScript
Raw Normal View History

import {type Handler} from "@netlify/functions";
import {api} from "./shared/api.js";
import {type SpeedruncomInfo} from "../../src/components/Info/Speedrunning/Speedruncom.js";
2023-05-08 02:31:43 +02:00
2023-11-05 21:01:24 +01:00
const handler: Handler = async () => {
// using the API's embedding would be stupid here, as that'd create lag due to irrelevant runs
const speedruncom = await api<{
data: Array<{
place: number;
2023-05-08 02:31:43 +02:00
run: {
weblink: string;
game: string;
level: string | undefined;
category: string | undefined;
date: string;
};
}>;
2024-04-27 21:22:48 +02:00
}>("https://www.speedrun.com/api/v1/users/j03v45mj/personal-bests");
2023-05-08 02:31:43 +02:00
2024-04-27 21:22:48 +02:00
const detailsToRequest = [new Promise((resolve) => {
2023-05-08 02:31:43 +02:00
resolve(api<{
data: {
names: {
international: string;
};
2023-05-08 02:31:43 +02:00
assets: {
"cover-tiny": {
uri: string;
};
};
};
2024-04-27 21:22:48 +02:00
}>(`https://www.speedrun.com/api/v1/games/${speedruncom.data[0].run.game}`));
})];
2023-05-08 02:31:43 +02:00
if (speedruncom.data[0].run.level) {
2024-04-27 21:22:48 +02:00
detailsToRequest.push(new Promise((resolve) => {
2023-05-08 02:31:43 +02:00
resolve(api<{
data: {
name: string;
};
2024-04-27 21:22:48 +02:00
}>(`https://www.speedrun.com/api/v1/levels/${speedruncom.data[0].run.level}`));
}));
2023-05-08 02:31:43 +02:00
}
if (speedruncom.data[0].run.category) {
2024-04-27 21:22:48 +02:00
detailsToRequest.push(new Promise((resolve) => {
2023-05-08 02:31:43 +02:00
resolve(api<{
data: {
name: string;
};
2024-04-27 21:22:48 +02:00
}>(`https://www.speedrun.com/api/v1/categories/${speedruncom.data[0].run.category}`));
}));
2023-05-08 02:31:43 +02:00
}
2024-04-27 21:22:48 +02:00
const details = await Promise.all(detailsToRequest) as [Record<string, any>];
2023-05-08 02:31:43 +02:00
2023-11-05 21:01:24 +01:00
const run: SpeedruncomInfo = {
2023-05-08 02:31:43 +02:00
place: speedruncom.data[0].place,
link: speedruncom.data[0].run.weblink,
date: speedruncom.data[0].run.date,
thumbnail: details[0].data.assets["cover-tiny"].uri,
game: details[0].data.names.international,
2024-04-27 21:22:48 +02:00
details: details.slice(1).map((d) => (d.data as {name: string}).name),
};
2023-05-08 02:31:43 +02:00
return {
statusCode: 200,
body: JSON.stringify(run),
};
};
2023-05-08 02:31:43 +02:00
export {handler};