-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] Pokerogue API client (#4583)
* start migrating Utils.apiFetch to api class * move dailyranking to api * use api in title-ui-handler * remove: Utils.apiFetch * migrate `updateSystemSavedata` to api * migrate clear session savedata to api * migrate updateAllSavedata to api * migrate `updateSessionSavedata` to api * rename `api` to `pokerogue-api` * migrate unlink discord to pokerogue-api * migrate unlink google to pokerogue-api * update pokerogue-api login * migrate register account to pokerogue-api * remove Utils.apiPost * reset overrides.ts * chore: cleanup * fix env.development * fix circular dependencies with api * fix gamedata verify missing await * fix daily api calls in daily-run-scorebard * fix discord-link request body being empty there was a double `toUrlSearchParams()` call involved * add pokerogue-api test coverge * add test-utils `getApiBaseUrl()` method * add pokerogue-admin-api test coverage * add pokerogue-account-api test coverage * add pokerogue-daily-api test coverage * add pokerogue-savedata-api test coverage * fix some test describes * add pokerogue-session-savedata-api test coverage * add pokerogue-system-savedata-api test coverage * fix tests * fix tryExportData thanks @MokaStitcher * chore: fix menu-ui-handlers.ts * fix admin-ui-handler (types) * extend test-coverage for admin-api * remove outdated code * skip some clowning-around-encounter tests if events are active this is not a permanent solution * Update src/system/game-data.ts Co-authored-by: PigeonBar <[email protected]> * Revert "skip some clowning-around-encounter tests if events are active" This reverts commit a97dafe. * mark `localServerUrl` and `apiUrl` as deprecated in `utils.ts` --------- Co-authored-by: NightKev <[email protected]> Co-authored-by: PigeonBar <[email protected]>
- Loading branch information
1 parent
a70f086
commit 7a0c88e
Showing
46 changed files
with
2,036 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { UserInfo } from "#app/@types/UserInfo"; | ||
|
||
export interface AccountInfoResponse extends UserInfo {} | ||
|
||
export interface AccountLoginRequest { | ||
username: string; | ||
password: string; | ||
} | ||
|
||
export interface AccountLoginResponse { | ||
token: string; | ||
} | ||
|
||
export interface AccountRegisterRequest { | ||
username: string; | ||
password: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export interface LinkAccountToDiscordIdRequest { | ||
username: string; | ||
discordId: string; | ||
} | ||
|
||
export interface UnlinkAccountFromDiscordIdRequest { | ||
username: string; | ||
discordId: string; | ||
} | ||
|
||
export interface LinkAccountToGoogledIdRequest { | ||
username: string; | ||
googleId: string; | ||
} | ||
|
||
export interface UnlinkAccountFromGoogledIdRequest { | ||
username: string; | ||
googleId: string; | ||
} | ||
|
||
export interface SearchAccountRequest { | ||
username: string; | ||
} | ||
|
||
export interface SearchAccountResponse { | ||
username: string; | ||
discordId: string; | ||
googleId: string; | ||
lastLoggedIn: string; | ||
registered: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface TitleStatsResponse { | ||
playerCount: number; | ||
battleCount: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { ScoreboardCategory } from "#app/ui/daily-run-scoreboard"; | ||
|
||
export interface GetDailyRankingsRequest { | ||
category: ScoreboardCategory; | ||
page?: number; | ||
} | ||
|
||
export interface GetDailyRankingsPageCountRequest { | ||
category: ScoreboardCategory; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { SessionSaveData, SystemSaveData } from "#app/system/game-data"; | ||
|
||
export interface UpdateAllSavedataRequest { | ||
system: SystemSaveData; | ||
session: SessionSaveData; | ||
sessionSlotId: number; | ||
clientSessionId: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export class UpdateSessionSavedataRequest { | ||
slot: number; | ||
trainerId: number; | ||
secretId: number; | ||
clientSessionId: string; | ||
} | ||
|
||
/** This is **NOT** similar to {@linkcode ClearSessionSavedataRequest} */ | ||
export interface NewClearSessionSavedataRequest { | ||
slot: number; | ||
clientSessionId: string; | ||
} | ||
|
||
export interface GetSessionSavedataRequest { | ||
slot: number; | ||
clientSessionId: string; | ||
} | ||
|
||
export interface DeleteSessionSavedataRequest { | ||
slot: number; | ||
clientSessionId: string; | ||
} | ||
|
||
/** This is **NOT** similar to {@linkcode NewClearSessionSavedataRequest} */ | ||
export interface ClearSessionSavedataRequest { | ||
slot: number; | ||
trainerId: number; | ||
clientSessionId: string; | ||
} | ||
|
||
/** | ||
* Pokerogue API response for path: `/savedata/session/clear` | ||
*/ | ||
export interface ClearSessionSavedataResponse { | ||
/** Contains the error message if any occured */ | ||
error?: string; | ||
/** Is `true` if the request was successfully processed */ | ||
success?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { SystemSaveData } from "#app/system/game-data"; | ||
|
||
export interface GetSystemSavedataRequest { | ||
clientSessionId: string; | ||
} | ||
|
||
export class UpdateSystemSavedataRequest { | ||
clientSessionId: string; | ||
trainerId?: number; | ||
secretId?: number; | ||
} | ||
|
||
export interface VerifySystemSavedataRequest { | ||
clientSessionId: string; | ||
} | ||
|
||
export interface VerifySystemSavedataResponse { | ||
valid: boolean; | ||
systemData: SystemSaveData; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface UserInfo { | ||
username: string; | ||
lastSessionSlot: number; | ||
discordId: string; | ||
googleId: string; | ||
hasAdminRole: boolean; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { SESSION_ID_COOKIE_NAME } from "#app/constants"; | ||
import { getCookie } from "#app/utils"; | ||
|
||
type DataType = "json" | "form-urlencoded"; | ||
|
||
export abstract class ApiBase { | ||
//#region Fields | ||
|
||
public readonly ERR_GENERIC: string = "There was an error"; | ||
|
||
protected readonly base: string; | ||
|
||
//#region Public | ||
|
||
constructor(base: string) { | ||
this.base = base; | ||
} | ||
|
||
//#region Protected | ||
|
||
/** | ||
* Send a GET request. | ||
* @param path The path to send the request to. | ||
*/ | ||
protected async doGet(path: string) { | ||
return this.doFetch(path, { method: "GET" }); | ||
} | ||
|
||
/** | ||
* Send a POST request. | ||
* @param path THe path to send the request to. | ||
* @param bodyData The body-data to send. | ||
* @param dataType The data-type of the {@linkcode bodyData}. | ||
*/ | ||
protected async doPost<D = undefined>(path: string, bodyData?: D, dataType: DataType = "json") { | ||
let body: string | undefined = undefined; | ||
const headers: HeadersInit = {}; | ||
|
||
if (bodyData) { | ||
if (dataType === "json") { | ||
body = typeof bodyData === "string" ? bodyData : JSON.stringify(bodyData); | ||
headers["Content-Type"] = "application/json"; | ||
} else if (dataType === "form-urlencoded") { | ||
if (bodyData instanceof Object) { | ||
body = this.toUrlSearchParams(bodyData).toString(); | ||
} else { | ||
console.warn("Could not add body data to form-urlencoded!", bodyData); | ||
} | ||
headers["Content-Type"] = "application/x-www-form-urlencoded"; | ||
} else { | ||
console.warn(`Unsupported data type: ${dataType}`); | ||
body = String(bodyData); | ||
headers["Content-Type"] = "text/plain"; | ||
} | ||
} | ||
|
||
return await this.doFetch(path, { method: "POST", body, headers }); | ||
} | ||
|
||
/** | ||
* A generic request helper. | ||
* @param path The path to send the request to. | ||
* @param config The request {@linkcode RequestInit | Configuration}. | ||
*/ | ||
protected async doFetch(path: string, config: RequestInit): Promise<Response> { | ||
config.headers = { | ||
...config.headers, | ||
Authorization: getCookie(SESSION_ID_COOKIE_NAME), | ||
"Content-Type": config.headers?.["Content-Type"] ?? "application/json", | ||
}; | ||
|
||
console.log(`Sending ${config.method ?? "GET"} request to: `, this.base + path, config); | ||
|
||
return await fetch(this.base + path, config); | ||
} | ||
|
||
/** | ||
* Helper to transform data to {@linkcode URLSearchParams} | ||
* Any key with a value of `undefined` will be ignored. | ||
* Any key with a value of `null` will be included. | ||
* @param data the data to transform to {@linkcode URLSearchParams} | ||
* @returns a {@linkcode URLSearchParams} representaton of {@linkcode data} | ||
*/ | ||
protected toUrlSearchParams<D extends Record<string, any>>(data: D) { | ||
const arr = Object.entries(data) | ||
.map(([ key, value ]) => (value !== undefined ? [ key, String(value) ] : [ key, "" ])) | ||
.filter(([ , value ]) => value !== ""); | ||
|
||
return new URLSearchParams(arr); | ||
} | ||
} |
Oops, something went wrong.