refactor(types): result types

move to shared types (#4774)
add types to ape client (#4786)
This commit is contained in:
Miodec 2024-02-08 17:57:45 +01:00
parent 15c697802d
commit e31613533c
6 changed files with 36 additions and 7 deletions

View file

@ -621,7 +621,9 @@ export async function addResult(
);
}
const data: AddResultData = {
const data: Omit<SharedTypes.PostResultResponse, "insertedId"> & {
insertedId: ObjectId;
} = {
isPb,
tagPbs,
insertedId: addedResult.insertedId,

View file

@ -5,25 +5,30 @@ export default class Results {
this.httpClient = httpClient;
}
async get(offset?: number): Ape.EndpointResponse {
async get(
offset?: number
): Ape.EndpointResponse<SharedTypes.DBResult<SharedTypes.Config.Mode>[]> {
return await this.httpClient.get(BASE_PATH, { searchQuery: { offset } });
}
async save(
result: SharedTypes.Result<SharedTypes.Config.Mode>
): Ape.EndpointResponse {
): Ape.EndpointResponse<Ape.Results.PostResult> {
return await this.httpClient.post(BASE_PATH, {
payload: { result },
});
}
async updateTags(resultId: string, tagIds: string[]): Ape.EndpointResponse {
async updateTags(
resultId: string,
tagIds: string[]
): Ape.EndpointResponse<Ape.Results.PatchResult> {
return await this.httpClient.patch(`${BASE_PATH}/tags`, {
payload: { resultId, tagIds },
});
}
async deleteAll(): Ape.EndpointResponse {
async deleteAll(): Ape.EndpointResponse<Ape.Results.DeleteAll> {
return await this.httpClient.delete(BASE_PATH);
}
}

View file

@ -0,0 +1,9 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
// for some reason when using the dot notaion, the types are not being recognized as used
declare namespace Ape.Results {
type PostResult = SharedTypes.PostResultResponse;
type PatchResult = {
tagPbs: string[];
};
type DeleteAll = null;
}

View file

@ -149,7 +149,8 @@ $("#resultEditTagsPanelWrapper .confirmButton").on("click", async () => {
);
}
const responseTagPbs = response.data.tagPbs;
//can do this because the response will not be null if the status is 200
const responseTagPbs = response.data?.tagPbs ?? [];
Notifications.add("Tags updated", 1, {
duration: 2,

View file

@ -1198,7 +1198,7 @@ async function saveResult(
$("#result .stats .tags .editTagsButton").attr(
"result-id",
response.data.insertedId
response.data?.insertedId as string //if status is 200 then response.data is not null or undefined
);
$("#result .stats .tags .editTagsButton").removeClass("invisible");

View file

@ -454,4 +454,16 @@ declare namespace SharedTypes {
badgeId: number | null;
hidden?: boolean;
}
type PostResultResponse = {
isPb: boolean;
tagPbs: string[];
insertedId: string;
dailyLeaderboardRank?: number;
weeklyXpLeaderboardRank?: number;
xp: number;
dailyXpBonus: boolean;
xpBreakdown: Record<string, number>;
streak: number;
};
}