Skip to content

Commit

Permalink
Merge pull request #27 from TTTaevas/interface-checking
Browse files Browse the repository at this point in the history
Make tests check if objects match their interface
  • Loading branch information
TTTaevas authored Nov 19, 2023
2 parents 7909b87 + d33dc6e commit e583405
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 80 deletions.
6 changes: 5 additions & 1 deletion lib/beatmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ export interface Beatmapset {
favourite_count: number
id: number
nsfw: boolean
offset: number
play_count: number
/**
* A string like that, where id is the `id` of the beatmapset: `//b.ppy.sh/preview/58951.mp3`
*/
preview_url: string
source: string
/**
* Can be/Is 0 if there is no source
*/
source: string | 0
spotlight: boolean
/**
* Is it ranked, is it graveyarded, etc
Expand Down
84 changes: 58 additions & 26 deletions lib/changelog.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
export interface ChangelogBuild {
interface ChangelogBuild {
created_at: Date
display_version: string
id: number
update_stream: UpdateStream | null
/**
* How many users are playing on this version of the game? (if lazer/web, should be 0, lazer doesn't show such stats)
*/
Expand All @@ -16,28 +15,33 @@ export interface ChangelogBuild {
* @remarks The ID of a Youtube video is whatever comes after `/watch?v=` in its url
*/
youtube_id: string | null
changelog_entries?: {
category: string
/**
* Can be January 1st 1970!
*/
created_at: Date | null
github_pull_request_id: number | null
github_url: string | null
}

/**
* Expected from ChangelogBuildWithChangelogentriesVersions
*/
export interface ChangelogBuildWithUpdatestreams extends ChangelogBuild {
update_stream: UpdateStream
}

interface ChangelogBuildWithChangelogentries extends ChangelogBuild {
changelog_entries: {
id: number | null
major: boolean
repository: string | null
title: string | null
type: string
github_pull_request_id: number | null
github_url: string | null
url: string | null
type: string
category: string
title: string | null
major: boolean
/**
* Entry message in Markdown format, embedded HTML is allowed, exists only if Markdown was requested
* Can be January 1st 1970!
*/
message?: string | null
created_at: Date
/**
* Entry message in HTML format, exists only if HTML was requested
* Doesn't exist if no github user is associated with who's credited with the change
*/
message_html?: string | null
github_user?: {
display_name: string
github_url: string | null
Expand All @@ -47,21 +51,49 @@ export interface ChangelogBuild {
user_id: number | null
user_url: string | null
}
}
/**
* The ChangelogBuilds in `versions` will not have `changelog_entries` or `versions`, and `users` will be 0
*/
versions?: {
next: ChangelogBuild | null
previous: ChangelogBuild | null
/**
* Entry message in Markdown format, embedded HTML is allowed, exists only if Markdown was requested
*/
message?: string | null
/**
* Entry message in HTML format, exists only if HTML was requested
*/
message_html?: string | null

}[]
}

/**
* Expected from api.getChangelogBuilds()
*/
export interface ChangelogBuildWithUpdatestreamsChangelogentries extends ChangelogBuildWithUpdatestreams, ChangelogBuildWithChangelogentries {

}

/**
* Expected from api.getChangelogBuild()
*/
export interface ChangelogBuildWithChangelogentriesVersions extends ChangelogBuildWithChangelogentries {
versions: {
next: ChangelogBuildWithUpdatestreams | null
previous: ChangelogBuildWithUpdatestreams | null
}
}

/**
* Expected from ChangelogBuildWithUpdatestreams
*/
export interface UpdateStream {
display_name: string | null
id: number
is_featured: boolean
name: string
display_name: string | null
is_featured: boolean
}

/**
* Expected from api.getChangelogStreams()
*/
export interface UpdateStreamWithLatestbuildUsercount extends UpdateStream {
latest_build: ChangelogBuild | null
/**
* How many users are playing on this? (if lazer/web, should be 0, lazer doesn't show such stats)
Expand Down
14 changes: 7 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { Leader, Match, MatchInfo, MultiplayerScore, MultiplayerScores, Playlist
import { Rulesets, Mod } from "./misc.js"
import { BeatmapUserScore, Score, ScoreWithUser, ScoreWithUserBeatmapBeatmapset } from "./score.js"
import { Rankings, RankingsCountry, RankingsSpotlight, Spotlight } from "./ranking.js"
import { ChangelogBuild, UpdateStream } from "./changelog.js"
import { ChangelogBuildWithChangelogentriesVersions, ChangelogBuildWithUpdatestreamsChangelogentries, UpdateStream } from "./changelog.js"

export {User, UserExtended, KudosuHistory}
export {Beatmap, BeatmapExtended, Beatmapset, BeatmapsetExtended}
export {BeatmapUserScore, Score}
export {Room, Leader, PlaylistItem, MultiplayerScore}
export {Rulesets}
export {ChangelogBuild, UpdateStream}
export {UpdateStream}

/**
* Scopes determine what the API instance can do as a user!
Expand Down Expand Up @@ -465,9 +465,9 @@ export class API {
* @param stream The name of the thing related to osu!, like `lazer`, `web`, `cuttingedge`, `beta40`, `stable40`
* @param build The name of the version! Usually something like `2023.1026.0` for lazer, or `20230326` for stable
*/
async getChangelogBuild(stream: string, build: string): Promise<ChangelogBuild> {
async getChangelogBuild(stream: string, build: string): Promise<ChangelogBuildWithChangelogentriesVersions> {
const response = await this.request("get", `changelog/${stream}/${build}`)
return correctType(response) as ChangelogBuild
return correctType(response) as ChangelogBuildWithChangelogentriesVersions
}

/**
Expand All @@ -478,10 +478,10 @@ export class API {
* @param message_formats Each element of `changelog_entries` will have a `message` property if `markdown`, `message_html` if `html`, defaults to both
*/
async getChangelogBuilds(versions?: {from?: string, to?: string}, max_id?: number,
stream?: string, message_formats: ("html" | "markdown")[] = ["html", "markdown"]): Promise<ChangelogBuild[]> {
stream?: string, message_formats: ("html" | "markdown")[] = ["html", "markdown"]): Promise<ChangelogBuildWithUpdatestreamsChangelogentries[]> {
const [from, to] = [versions?.from, versions?.to]
const response = await this.request("get", "changelog", {from, to, max_id, stream, message_formats})
return response.builds.map((b: ChangelogBuild) => correctType(b)) as ChangelogBuild[]
return response.builds.map((b: ChangelogBuildWithUpdatestreamsChangelogentries) => correctType(b)) as ChangelogBuildWithUpdatestreamsChangelogentries[]
}

/**
Expand Down Expand Up @@ -642,7 +642,7 @@ function correctType(x: any): any {
const k = Object.keys(x)
const v = Object.values(x)
for (let i = 0; i < k.length; i++) {
if (k[i].includes("name") || k[i].includes("version")) continue // don't turn names made of numbers into integers
if (typeof v[i] === "string" && (k[i].includes("name") || k[i].includes("version"))) continue // don't turn names made of numbers into integers
x[k[i]] = correctType(v[i])
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface Score {
* In a format where `96.40%` would be `0.9640` (likely with some numbers after the zero)
*/
accuracy: number
best_id: number
best_id: number | null
created_at: Date
id: number
max_combo: number
Expand Down
Loading

0 comments on commit e583405

Please sign in to comment.