taevas.xyz/netlify/functions/osu.ts

53 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-05-08 21:57:34 +02:00
import { Handler } from '@netlify/functions'
import { API, APIError, User } from 'osu-api-v2-js'
2023-05-29 14:47:53 +02:00
import { OsuInfo } from '../../src/components/infos/Osu'
2023-05-08 21:57:34 +02:00
2023-11-05 21:01:24 +01:00
const handler: Handler = async () => {
const api = await API.createAsync({id: 11451, secret: process.env.API_OSU!})
2023-05-08 21:57:34 +02:00
if (!api) {
return {
statusCode: 404,
body: ""
}
}
2023-11-05 21:01:24 +01:00
const profile = await Promise.all([
new Promise((resolve) => resolve(api!.getUser({id: 7276846}, 0))),
new Promise((resolve) => resolve(api!.getUser({id: 7276846}, 1))),
new Promise((resolve) => resolve(api!.getUser({id: 7276846}, 2))),
new Promise((resolve) => resolve(api!.getUser({id: 7276846}, 3)))
2023-05-08 21:57:34 +02:00
])
if (profile.find((mode) => mode instanceof APIError)) {
return {
statusCode: 404,
body: ""
}
}
2023-11-05 21:01:24 +01:00
const info: OsuInfo = {
country: (profile[0] as User).country?.name || "Unknown",
osu: {global: 0, country: 0},
taiko: {global: 0, country: 0},
fruits: {global: 0, country: 0},
mania: {global: 0, country: 0}
2023-05-08 21:57:34 +02:00
}
for (let i = 0; i < profile.length; i++) {
2023-11-05 21:01:24 +01:00
const ruleset = profile[i] as User
if (ruleset.statistics && ruleset.rank_history) {
// At the time of writing this, osu-api-v2-js ain't exactly the greatest package ever written
2023-11-05 21:01:24 +01:00
const stats = ruleset.statistics as any
info[ruleset.rank_history.mode].global = stats.global_rank
info[ruleset.rank_history.mode].country = stats.country_rank
2023-05-08 21:57:34 +02:00
}
}
return {
statusCode: 200,
body: JSON.stringify(info)
2023-05-08 21:57:34 +02:00
}
}
export { handler }