Skip to content

Commit c59b25f

Browse files
committed
Add return types for api methods, update some docs/comments
1 parent 11d4a1c commit c59b25f

File tree

8 files changed

+60
-53
lines changed

8 files changed

+60
-53
lines changed

src/plugins/api/account-api.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import type {
88
} from "#types/api-types";
99
import { removeCookie, setCookie } from "#utils/app-utils";
1010

11-
/**
12-
* A wrapper for the account API requests.
13-
*/
11+
/** A wrapper for the account API requests. */
1412
export class AccountApi extends ApiBase {
1513
//#region Public
1614

@@ -39,7 +37,7 @@ export class AccountApi extends ApiBase {
3937
* @param registerData The {@linkcode AccountRegisterRequest} to send
4038
* @returns An error message if something went wrong
4139
*/
42-
public async register(registerData: AccountRegisterRequest) {
40+
public async register(registerData: AccountRegisterRequest): Promise<string | null> {
4341
try {
4442
const response = await this.doPost("/account/register", registerData, "form-urlencoded");
4543

@@ -60,7 +58,7 @@ export class AccountApi extends ApiBase {
6058
* @param loginData The {@linkcode AccountLoginRequest} to send
6159
* @returns An error message if something went wrong
6260
*/
63-
public async login(loginData: AccountLoginRequest) {
61+
public async login(loginData: AccountLoginRequest): Promise<string | null> {
6462
try {
6563
const response = await this.doPost("/account/login", loginData, "form-urlencoded");
6664

@@ -82,7 +80,7 @@ export class AccountApi extends ApiBase {
8280
* Send a logout request.
8381
* **Always** (no matter if failed or not) removes the session cookie.
8482
*/
85-
public async logout() {
83+
public async logout(): Promise<void> {
8684
try {
8785
const response = await this.doGet("/account/logout");
8886

@@ -95,4 +93,6 @@ export class AccountApi extends ApiBase {
9593

9694
removeCookie(SESSION_ID_COOKIE); // we are always clearing the cookie.
9795
}
96+
97+
//#endregion
9898
}

src/plugins/api/admin-api.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class AdminApi extends ApiBase {
1616
* @param params The {@linkcode LinkAccountToDiscordIdRequest} to send
1717
* @returns `null` if successful, error message if not
1818
*/
19-
public async linkAccountToDiscord(params: LinkAccountToDiscordIdRequest) {
19+
public async linkAccountToDiscord(params: LinkAccountToDiscordIdRequest): Promise<string | null> {
2020
try {
2121
const response = await this.doPost("/admin/account/discordLink", params, "form-urlencoded");
2222

@@ -40,7 +40,7 @@ export class AdminApi extends ApiBase {
4040
* @param params The {@linkcode UnlinkAccountFromDiscordIdRequest} to send
4141
* @returns `null` if successful, error message if not
4242
*/
43-
public async unlinkAccountFromDiscord(params: UnlinkAccountFromDiscordIdRequest) {
43+
public async unlinkAccountFromDiscord(params: UnlinkAccountFromDiscordIdRequest): Promise<string | null> {
4444
try {
4545
const response = await this.doPost("/admin/account/discordUnlink", params, "form-urlencoded");
4646

@@ -64,7 +64,7 @@ export class AdminApi extends ApiBase {
6464
* @param params The {@linkcode LinkAccountToGoogledIdRequest} to send
6565
* @returns `null` if successful, error message if not
6666
*/
67-
public async linkAccountToGoogleId(params: LinkAccountToGoogledIdRequest) {
67+
public async linkAccountToGoogleId(params: LinkAccountToGoogledIdRequest): Promise<string | null> {
6868
try {
6969
const response = await this.doPost("/admin/account/googleLink", params, "form-urlencoded");
7070

@@ -88,7 +88,7 @@ export class AdminApi extends ApiBase {
8888
* @param params The {@linkcode UnlinkAccountFromGoogledIdRequest} to send
8989
* @returns `null` if successful, error message if not
9090
*/
91-
public async unlinkAccountFromGoogleId(params: UnlinkAccountFromGoogledIdRequest) {
91+
public async unlinkAccountFromGoogleId(params: UnlinkAccountFromGoogledIdRequest): Promise<string | null> {
9292
try {
9393
const response = await this.doPost("/admin/account/googleUnlink", params, "form-urlencoded");
9494

src/plugins/api/api-base.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ export abstract class ApiBase {
1010

1111
protected readonly base: string;
1212

13+
//#endregion
1314
//#region Public
1415

1516
constructor(base: string) {
1617
this.base = base;
1718
}
1819

20+
//#endregion
1921
//#region Protected
2022

2123
/**
2224
* Send a GET request.
2325
* @param path The path to send the request to.
2426
*/
25-
protected async doGet(path: string) {
27+
protected async doGet(path: string): Promise<Response> {
2628
return this.doFetch(path, { method: "GET" });
2729
}
2830

@@ -32,7 +34,7 @@ export abstract class ApiBase {
3234
* @param bodyData The body-data to send.
3335
* @param dataType The data-type of the {@linkcode bodyData}.
3436
*/
35-
protected async doPost<D = undefined>(path: string, bodyData?: D, dataType: DataType = "json") {
37+
protected async doPost<D = undefined>(path: string, bodyData?: D, dataType: DataType = "json"): Promise<Response> {
3638
let body: string | undefined;
3739
const headers: HeadersInit = {};
3840

@@ -83,11 +85,13 @@ export abstract class ApiBase {
8385
* @param data the data to transform to {@linkcode URLSearchParams}
8486
* @returns a {@linkcode URLSearchParams} representaton of {@linkcode data}
8587
*/
86-
protected toUrlSearchParams<D extends Record<string, any>>(data: D) {
88+
protected toUrlSearchParams<D extends Record<string, any>>(data: D): URLSearchParams {
8789
const arr = Object.entries(data)
8890
.map(([key, value]) => [key, String(value ?? "")])
8991
.filter(([, value]) => value !== "");
9092

9193
return new URLSearchParams(arr);
9294
}
95+
96+
//#endregion
9397
}

src/plugins/api/api.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import { DailyApi } from "#api/daily-api";
55
import { SavedataApi } from "#api/savedata-api";
66
import type { TitleStatsResponse } from "#types/api-types";
77

8-
/**
9-
* A wrapper for API requests.
10-
*/
8+
/** A wrapper for API requests. */
119
class Api extends ApiBase {
1210
//#region Fields
1311

@@ -21,6 +19,7 @@ class Api extends ApiBase {
2119
/** Whether the server/api is connected. By default we assume `true`. */
2220
private _isConnected: boolean;
2321

22+
//#endregion
2423
//#region Public
2524

2625
constructor(base: string) {
@@ -32,14 +31,14 @@ class Api extends ApiBase {
3231
}
3332

3433
/** Whether the server/api is connected. By default we assume `true`. */
35-
public get isConnected() {
34+
public get isConnected(): boolean {
3635
return this._isConnected;
3736
}
3837

3938
/**
4039
* Request game title-stats.
4140
*/
42-
public async getGameTitleStats() {
41+
public async getGameTitleStats(): Promise<TitleStatsResponse | null> {
4342
if (!this.isConnected) {
4443
this.printServerNotConnectedWarning();
4544
return null;
@@ -58,7 +57,7 @@ class Api extends ApiBase {
5857
* Unlink the currently logged in user from Discord.
5958
* @returns `true` if unlinking was successful, `false` if not
6059
*/
61-
public async unlinkDiscord() {
60+
public async unlinkDiscord(): Promise<boolean> {
6261
if (!this.isConnected) {
6362
this.printServerNotConnectedWarning();
6463
return false;
@@ -81,7 +80,7 @@ class Api extends ApiBase {
8180
* Unlink the currently logged in user from Google.
8281
* @returns `true` if unlinking was successful, `false` if not
8382
*/
84-
public async unlinkGoogle() {
83+
public async unlinkGoogle(): Promise<boolean> {
8584
if (!this.isConnected) {
8685
this.printServerNotConnectedWarning();
8786
return false;
@@ -105,7 +104,7 @@ class Api extends ApiBase {
105104
* @remarks
106105
* We have no dedicated ping/status endpoint yet, so we ping the game title stats endpoint, but without printing any errors by default.
107106
*/
108-
async ping() {
107+
public async ping(): Promise<void> {
109108
try {
110109
const response = await this.doGet("/game/titlestats");
111110
const data = await response.json();
@@ -125,7 +124,7 @@ class Api extends ApiBase {
125124
//#endregion
126125
//#region Private
127126

128-
private printServerNotConnectedWarning() {
127+
private printServerNotConnectedWarning(): void {
129128
if (import.meta.env.VITE_API_DEBUG === "1") {
130129
console.warn(this.ERR_SERVER_NOT_CONNECTED);
131130
}

src/plugins/api/daily-api.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { ApiBase } from "#api/api-base";
22

3-
/**
4-
* A wrapper for daily-run API requests.
5-
*/
3+
/** A wrapper for daily-run API requests. */
64
export class DailyApi extends ApiBase {
75
//#region Public
86

97
/**
108
* Request the daily-run seed.
119
* @returns The active daily-run seed as `string`.
1210
*/
13-
public async getSeed() {
11+
public async getSeed(): Promise<string | null> {
1412
try {
1513
const response = await this.doGet("/daily/seed");
1614
return response.text();
@@ -19,4 +17,6 @@ export class DailyApi extends ApiBase {
1917
return null;
2018
}
2119
}
20+
21+
//#endregion
2222
}

src/plugins/api/savedata-api.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import { SystemSavedataApi } from "#api/system-savedata-api";
44
import { MAX_INT_ATTR_VALUE } from "#constants/game-constants";
55
import type { UpdateAllSavedataRequest } from "#types/api-types";
66

7-
/**
8-
* A wrapper for savedata API requests.
9-
*/
7+
/** A wrapper for savedata API requests. */
108
export class SavedataApi extends ApiBase {
119
//#region Fields
1210

1311
public readonly system: SystemSavedataApi;
1412
public readonly session: SessionSavedataApi;
1513

14+
//#endregion
1615
//#region Public
1716

1817
constructor(base: string) {
@@ -26,7 +25,7 @@ export class SavedataApi extends ApiBase {
2625
* @param bodyData The {@linkcode UpdateAllSavedataRequest | request data} to send
2726
* @returns An error message if something went wrong
2827
*/
29-
public async updateAll(bodyData: UpdateAllSavedataRequest) {
28+
public async updateAll(bodyData: UpdateAllSavedataRequest): Promise<string> {
3029
try {
3130
const rawBodyData = JSON.stringify(bodyData, (_k: any, v: any) => {
3231
if (typeof v === "bigint") {
@@ -41,4 +40,6 @@ export class SavedataApi extends ApiBase {
4140
return "Unknown error";
4241
}
4342
}
43+
44+
//#endregion
4445
}

src/plugins/api/session-savedata-api.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import type {
99
} from "#types/api-types";
1010
import type { SessionSaveData } from "#types/session-data";
1111

12-
/**
13-
* A wrapper for session savedata API requests.
14-
*/
12+
/** A wrapper for session savedata API requests. */
1513
export class SessionSavedataApi extends ApiBase {
1614
//#region Public
1715

@@ -40,7 +38,7 @@ export class SessionSavedataApi extends ApiBase {
4038
* @param params The {@linkcode GetSessionSavedataRequest} to send
4139
* @returns The session as `string`
4240
*/
43-
public async get(params: GetSessionSavedataRequest) {
41+
public async get(params: GetSessionSavedataRequest): Promise<string | null> {
4442
try {
4543
const urlSearchParams = this.toUrlSearchParams(params);
4644
const response = await this.doGet(`/savedata/session/get?${urlSearchParams}`);
@@ -58,7 +56,7 @@ export class SessionSavedataApi extends ApiBase {
5856
* @param rawSavedata The raw savedata (as `string`)
5957
* @returns An error message if something went wrong
6058
*/
61-
public async update(params: UpdateSessionSavedataRequest, rawSavedata: string) {
59+
public async update(params: UpdateSessionSavedataRequest, rawSavedata: string): Promise<string> {
6260
try {
6361
const urlSearchParams = this.toUrlSearchParams(params);
6462
const response = await this.doPost(`/savedata/session/update?${urlSearchParams}`, rawSavedata);
@@ -76,7 +74,7 @@ export class SessionSavedataApi extends ApiBase {
7674
* @param params The {@linkcode DeleteSessionSavedataRequest} to send
7775
* @returns An error message if something went wrong
7876
*/
79-
public async delete(params: DeleteSessionSavedataRequest) {
77+
public async delete(params: DeleteSessionSavedataRequest): Promise<string | null> {
8078
try {
8179
const urlSearchParams = this.toUrlSearchParams(params);
8280
const response = await this.doGet(`/savedata/session/delete?${urlSearchParams}`);
@@ -98,7 +96,10 @@ export class SessionSavedataApi extends ApiBase {
9896
* @param params The {@linkcode ClearSessionSavedataRequest} to send
9997
* @param sessionData The {@linkcode SessionSaveData} object
10098
*/
101-
public async clear(params: ClearSessionSavedataRequest, sessionData: SessionSaveData) {
99+
public async clear(
100+
params: ClearSessionSavedataRequest,
101+
sessionData: SessionSaveData,
102+
): Promise<ClearSessionSavedataResponse> {
102103
try {
103104
const urlSearchParams = this.toUrlSearchParams(params);
104105
const response = await this.doPost(`/savedata/session/clear?${urlSearchParams}`, sessionData);
@@ -113,4 +114,6 @@ export class SessionSavedataApi extends ApiBase {
113114
success: false,
114115
} as ClearSessionSavedataResponse;
115116
}
117+
118+
//#endregion
116119
}

src/plugins/api/system-savedata-api.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ import type {
55
VerifySystemSavedataRequest,
66
VerifySystemSavedataResponse,
77
} from "#types/api-types";
8+
import type { SystemSaveData } from "#types/system-data";
89

9-
/**
10-
* A wrapper for system savedata API requests.
11-
*/
10+
/** A wrapper for system savedata API requests. */
1211
export class SystemSavedataApi extends ApiBase {
1312
//#region Public
1413

1514
/**
16-
* Get a system savedata.
17-
* @param params The {@linkcode GetSystemSavedataRequest} to send
18-
* @returns The system savedata as `string` or `null` on error
15+
* Get the system savedata.
16+
* @param params - The {@linkcode GetSystemSavedataRequest} to send
17+
* @returns The system savedata as `string`, or `null` on error
1918
*/
20-
public async get(params: GetSystemSavedataRequest) {
19+
public async get(params: GetSystemSavedataRequest): Promise<string | null> {
2120
try {
2221
const urlSearchParams = this.toUrlSearchParams(params);
2322
const response = await this.doGet(`/savedata/system/get?${urlSearchParams}`);
@@ -32,13 +31,12 @@ export class SystemSavedataApi extends ApiBase {
3231

3332
/**
3433
* Verify if the session is valid.
35-
* If not the {@linkcode SystemSaveData} is returned.
36-
* @param params The {@linkcode VerifySystemSavedataRequest} to send
34+
* If not, the `SystemSaveData` is returned.
35+
* @param params - The {@linkcode VerifySystemSavedataRequest} to send
3736
* @returns A {@linkcode SystemSaveData} if **NOT** valid, otherwise `null`.
38-
*
39-
* TODO: add handling for errors
4037
*/
41-
public async verify(params: VerifySystemSavedataRequest) {
38+
// TODO: add handling for errors
39+
public async verify(params: VerifySystemSavedataRequest): Promise<SystemSaveData | null> {
4240
const urlSearchParams = this.toUrlSearchParams(params);
4341
const response = await this.doGet(`/savedata/system/verify?${urlSearchParams}`);
4442

@@ -57,12 +55,12 @@ export class SystemSavedataApi extends ApiBase {
5755
}
5856

5957
/**
60-
* Update a system savedata.
61-
* @param params The {@linkcode UpdateSystemSavedataRequest} to send
62-
* @param rawSystemData The raw {@linkcode SystemSaveData}
58+
* Update the system savedata.
59+
* @param params - The {@linkcode UpdateSystemSavedataRequest} to send
60+
* @param rawSystemData - The raw {@linkcode SystemSaveData}
6361
* @returns An error message if something went wrong
6462
*/
65-
public async update(params: UpdateSystemSavedataRequest, rawSystemData: string) {
63+
public async update(params: UpdateSystemSavedataRequest, rawSystemData: string): Promise<string> {
6664
try {
6765
const urSearchParams = this.toUrlSearchParams(params);
6866
const response = await this.doPost(`/savedata/system/update?${urSearchParams}`, rawSystemData);
@@ -74,4 +72,6 @@ export class SystemSavedataApi extends ApiBase {
7472

7573
return "Unknown Error";
7674
}
75+
76+
//#endregion
7777
}

0 commit comments

Comments
 (0)