2024-04-27 18:45:18 +02:00
|
|
|
import {type Handler} from "@netlify/functions";
|
|
|
|
import {api} from "./shared/api.js";
|
2024-05-04 19:14:18 +02:00
|
|
|
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<{
|
2024-04-27 18:45:18 +02:00
|
|
|
data: Array<{
|
|
|
|
place: number;
|
2023-05-08 02:31:43 +02:00
|
|
|
run: {
|
2024-04-27 18:45:18 +02:00
|
|
|
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: {
|
2024-04-27 18:45:18 +02:00
|
|
|
international: string;
|
|
|
|
};
|
2023-05-08 02:31:43 +02:00
|
|
|
assets: {
|
|
|
|
"cover-tiny": {
|
2024-04-27 18:45:18 +02:00
|
|
|
uri: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2024-04-27 21:22:48 +02:00
|
|
|
}>(`https://www.speedrun.com/api/v1/games/${speedruncom.data[0].run.game}`));
|
2024-04-27 18:45:18 +02:00
|
|
|
})];
|
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: {
|
2024-04-27 18:45:18 +02:00
|
|
|
name: string;
|
|
|
|
};
|
2024-04-27 21:22:48 +02:00
|
|
|
}>(`https://www.speedrun.com/api/v1/levels/${speedruncom.data[0].run.level}`));
|
2024-04-27 18:45:18 +02:00
|
|
|
}));
|
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: {
|
2024-04-27 18:45:18 +02:00
|
|
|
name: string;
|
|
|
|
};
|
2024-04-27 21:22:48 +02:00
|
|
|
}>(`https://www.speedrun.com/api/v1/categories/${speedruncom.data[0].run.category}`));
|
2024-04-27 18:45:18 +02:00
|
|
|
}));
|
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),
|
2024-04-27 18:45:18 +02:00
|
|
|
};
|
2023-05-08 02:31:43 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
statusCode: 200,
|
2024-04-27 18:45:18 +02:00
|
|
|
body: JSON.stringify(run),
|
|
|
|
};
|
|
|
|
};
|
2023-05-08 02:31:43 +02:00
|
|
|
|
2024-04-27 18:45:18 +02:00
|
|
|
export {handler};
|