Skip to content

Commit

Permalink
Update Beatmapset's nominations_summary & WikiPage's tags
Browse files Browse the repository at this point in the history
I don't really understand what the data of `nominations_summary`'s `eligible_main_rulesets` actually mean so no documentation from me there
The `tags` of a `WikiPage` could actually be numbers, the API server actually is the one that does this mistake, this wrapper fixes that
  • Loading branch information
TTTaevas committed Jun 7, 2024
1 parent 5db9d94 commit 7b43de7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
7 changes: 6 additions & 1 deletion lib/beatmapset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,12 @@ export namespace Beatmapset {
legacy_thread_url: string
nominations_summary: {
current: number
required: number
eligible_main_rulesets: (keyof typeof Ruleset)[] | null
/** Required nominations */
required_meta: {
main_ruleset: number
non_main_ruleset: number
}
}
ranked: RankStatus
ranked_date: Date | null
Expand Down
22 changes: 13 additions & 9 deletions lib/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,15 @@ export function adaptParametersForGETRequests(parameters: {[k: string]: any}): {
* @param x Anything, but should be a string, an array that contains a string, or an object which has a string
* @returns x, but with it (or what it contains) now having the correct type
*/
export function correctType(x: any): any {
export function correctType(x: any, force_string?: boolean): any {
// those MUST be strings; turn the server's numbers into strings and keep the server's strings as strings
const bannedProperties = [
"name", "artist", "title", "location", "interests", "occupation", "twitter",
"discord", "version", "author", "raw", "bbcode", "title", "message", "creator", "source"
"name", "artist", "title", "tags", "username", "location", "interests", "occupation", "twitter",
"discord", "version", "display_version", "author", "raw", "bbcode", "title", "message", "creator", "source"
]
if (force_string && typeof x !== "object") {
return String(x)
}

if (typeof x === "boolean") {
return x
Expand All @@ -121,17 +125,17 @@ export function correctType(x: any): any {
if (/[0-9]{2}:[0-9]{2}:[0-9]{2}\+[0-9]{2}:[0-9]{2}$/.test(x)) x = x.substring(0, x.indexOf("+")) + "Z"
return new Date(x)
} else if (Array.isArray(x)) {
return x.map((e) => correctType(e))
return x.map((e) => correctType(e, force_string))
} else if (!isNaN(x) && x !== "") {
return x === null ? null : Number(x)
} else if (typeof x === "object" && x !== null) {
const k = Object.keys(x)
const v = Object.values(x)
for (let i = 0; i < k.length; i++) {
if (typeof v[i] === "string" && bannedProperties.some((p) => k[i].includes(p))) continue // don't turn names made of numbers into integers
x[k[i]] = correctType(v[i])
const keys = Object.keys(x)
const vals = Object.values(x)
for (let i = 0; i < keys.length; i++) {
x[keys[i]] = correctType(vals[i], bannedProperties.some((p) => keys[i] === p))
}
}

return x
}

Expand Down
1 change: 1 addition & 0 deletions lib/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ const testOther = async (): Promise<boolean> => {

const test = async (id: string, secret: string): Promise<void> => {
api = await osu.API.createAsync({id: Number(id), secret}, undefined, "all") //"http://127.0.0.1:8080")
api.timeout = 30

const tests = [
testBeatmapPack,
Expand Down

0 comments on commit 7b43de7

Please sign in to comment.