-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(getRecentGameAwards): add function (#98)
- Loading branch information
1 parent
3be63a0
commit c3fe412
Showing
13 changed files
with
208 additions
and
24 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,73 @@ | ||
import { http, HttpResponse } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
|
||
import { apiBaseUrl } from "../utils/internal"; | ||
import { buildAuthorization } from "../utils/public"; | ||
import { getRecentGameAwards } from "./getRecentGameAwards"; | ||
import type { GetRecentGameAwardsResponse, RecentGameAwards } from "./models"; | ||
|
||
const server = setupServer(); | ||
|
||
describe("Function: getRecentGameAwards", () => { | ||
// MSW Setup | ||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
it("is defined #sanity", () => { | ||
// ASSERT | ||
expect(getRecentGameAwards).toBeDefined(); | ||
}); | ||
|
||
it("retrieves metadata about all recently-earned game awards on the site", async () => { | ||
// ARRANGE | ||
const authorization = buildAuthorization({ | ||
username: "mockUserName", | ||
webApiKey: "mockWebApiKey", | ||
}); | ||
|
||
const mockResponse: GetRecentGameAwardsResponse = { | ||
Count: 1, | ||
Total: 1, | ||
Results: [ | ||
{ | ||
User: "renanbrj", | ||
AwardKind: "mastered", | ||
AwardDate: "2022-01-01T23:48:04+00:00", | ||
GameID: 14_284, | ||
GameTitle: "Batman Returns", | ||
ConsoleID: 15, | ||
ConsoleName: "Game Gear", | ||
}, | ||
], | ||
}; | ||
|
||
server.use( | ||
http.get(`${apiBaseUrl}/API_GetRecentGameAwards.php`, () => | ||
HttpResponse.json(mockResponse) | ||
) | ||
); | ||
|
||
// ACT | ||
const response = await getRecentGameAwards(authorization); | ||
|
||
const expectedResponse: RecentGameAwards = { | ||
count: 1, | ||
total: 1, | ||
results: [ | ||
{ | ||
user: "renanbrj", | ||
awardKind: "mastered", | ||
awardDate: "2022-01-01T23:48:04+00:00", | ||
gameId: 14_284, | ||
gameTitle: "Batman Returns", | ||
consoleId: 15, | ||
consoleName: "Game Gear", | ||
}, | ||
], | ||
}; | ||
|
||
// ASSERT | ||
expect(response).toEqual(expectedResponse); | ||
}); | ||
}); |
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,85 @@ | ||
import { | ||
apiBaseUrl, | ||
buildRequestUrl, | ||
call, | ||
serializeProperties, | ||
} from "../utils/internal"; | ||
import type { AuthObject, AwardKind } from "../utils/public"; | ||
import type { GetRecentGameAwardsResponse, RecentGameAwards } from "./models"; | ||
|
||
/** | ||
* A call to this function will retrieve all recently granted game | ||
* awards across the site's userbase. | ||
* | ||
* @param authorization An object containing your username and webApiKey. | ||
* This can be constructed with `buildAuthorization()`. | ||
* | ||
* @param payload.startDate The date to fetch awards from. | ||
* | ||
* @param payload.offset Optional. Defaults to 0. | ||
* | ||
* @param payload.count Optional. Defaults to 25. | ||
* | ||
* @param payload.desiredAwardKinds Optional. Defaults to all. Accepts "beaten-softcore", "beaten-hardcore", "completed", and/or "mastered". | ||
* | ||
* @example | ||
* ``` | ||
* const recentGameAwards = await getRecentGameAwards( | ||
* authorization, | ||
* ); | ||
* ``` | ||
* | ||
* @returns An object containing metadata about all recently granted game | ||
* awards across the site's userbase | ||
* ``` | ||
* { | ||
* count: 1, | ||
* total: 1, | ||
* results: [ | ||
* { | ||
* user: "renanbrj", | ||
* awardKind: "mastered", | ||
* awardDate: "2022-01-01T23:48:04+00:00", | ||
* gameId: 14_284, | ||
* gameTitle: "Batman Returns", | ||
* consoleId: 15, | ||
* consoleName: "Game Gear", | ||
* }, | ||
* ], | ||
* } | ||
* ``` | ||
*/ | ||
export const getRecentGameAwards = async ( | ||
authorization: AuthObject, | ||
payload?: Partial<{ | ||
startDate: string; | ||
offset: number; | ||
count: number; | ||
desiredAwardKinds: AwardKind[]; | ||
}> | ||
): Promise<RecentGameAwards> => { | ||
const queryParams: Record<string, any> = {}; | ||
if (payload?.startDate) { | ||
queryParams.d = payload.startDate; | ||
} | ||
if (payload?.offset) { | ||
queryParams.o = payload.offset; | ||
} | ||
if (payload?.count) { | ||
queryParams.c = payload.count; | ||
} | ||
if (payload?.desiredAwardKinds) { | ||
queryParams.k = payload.desiredAwardKinds.join(","); | ||
} | ||
|
||
const url = buildRequestUrl( | ||
apiBaseUrl, | ||
"/API_GetRecentGameAwards.php", | ||
authorization, | ||
queryParams | ||
); | ||
|
||
const rawResponse = await call<GetRecentGameAwardsResponse>({ url }); | ||
|
||
return serializeProperties(rawResponse); | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./getAchievementOfTheWeek"; | ||
export * from "./getActiveClaims"; | ||
export * from "./getClaims"; | ||
export * from "./getRecentGameAwards"; | ||
export * from "./getTopTenUsers"; | ||
export * from "./models"; |
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,15 @@ | ||
import type { AwardKind } from "../../utils/public"; | ||
|
||
export interface GetRecentGameAwardsResponse { | ||
Count: number; | ||
Total: number; | ||
Results: Array<{ | ||
User: string; | ||
AwardKind: AwardKind; | ||
AwardDate: string; | ||
GameID: number; | ||
GameTitle: string; | ||
ConsoleID: number; | ||
ConsoleName: 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
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,15 @@ | ||
import type { AwardKind } from "../../utils/public"; | ||
|
||
export interface RecentGameAwards { | ||
count: number; | ||
total: number; | ||
results: Array<{ | ||
user: string; | ||
awardKind: AwardKind; | ||
awardDate: string; | ||
gameId: number; | ||
gameTitle: string; | ||
consoleId: number; | ||
consoleName: 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
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,5 @@ | ||
export type AwardKind = | ||
| "beaten-softcore" | ||
| "beaten-hardcore" | ||
| "completed" | ||
| "mastered"; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./auth-object.model"; | ||
export * from "./award-kind.model"; |