-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getUserRecentAchievements function
- Loading branch information
1 parent
b08ab17
commit 2ccc039
Showing
8 changed files
with
261 additions
and
0 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,60 @@ | ||
# getUserRecentAchievements | ||
|
||
A call to this function will retrieve a list of a target user's recently earned achievements, via their username. By default, it fetches achievements earned in the last hour. | ||
|
||
## Examples | ||
|
||
```ts | ||
import { getUserRecentAchievements } from "@retroachievements/api"; | ||
|
||
// This gets the user's achievements earned in the last hour. | ||
const userRecentAchievements = await getUserRecentAchievements(authorization, { | ||
userName: "xelnia" | ||
}); | ||
``` | ||
|
||
```ts | ||
import { getUserRecentAchievements } from "@retroachievements/api"; | ||
|
||
// This gets the user's achievements earned in the last two hours. | ||
const userRecentAchievements = await getUserRecentAchievements(authorization, { | ||
userName: "xelnia", | ||
recentMinutes: 120 | ||
}); | ||
``` | ||
|
||
## Returns | ||
|
||
```json | ||
[ | ||
{ | ||
"date": "2023-05-23 22:32:24", | ||
"hardcoreMode": true, | ||
"achievementId": 51214, | ||
"title": "You're a special Champ!", | ||
"description": "Win the Tournament as [You] on Hard with 1 attribute on max. and 1 attribute on min.", | ||
"badgeName": "121991", | ||
"points": 25, | ||
"author": "Som1", | ||
"gameTitle": "WWF King of the Ring", | ||
"gameIcon": "/Images/062599.png", | ||
"gameId": 6316, | ||
"consoleName": "Game Boy", | ||
"badgeUrl": "/Badge/121991.png", | ||
"gameUrl": "/game/6316" | ||
} | ||
] | ||
``` | ||
|
||
## Parameters | ||
|
||
| Name | Type | Description | | ||
| :-------------- | :------------------------------------------ | :--------------------------------------------------------------------------------------------------------------------------- | | ||
| `authorization` | [`AuthObject`](/v1/data-models/auth-object) | An object that must contain a `userName` and a `webApiKey`. See [this page](/getting-started) for how to create this object. | | ||
| `userName` | `string` | The user for which to retrieve the recently played games for. | | ||
| `recentMinutes` | `number?` | Optional. Defaults to 60. How many minutes back to fetch for the given user. | | ||
|
||
## Source | ||
|
||
[@retroachievements/api, getUserRecentAchievements.ts](https://github.dev/RetroAchievements/retroachievements-api-js/blob/main/src/user/getUserRecentAchievements.ts) | ||
[RAWeb, API_GetUserRecentAchievements.php](https://github.dev/RetroAchievements/RAWeb/blob/master/public/API/API_GetUserRecentAchievements.php) |
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,82 @@ | ||
import { rest } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
|
||
import { apiBaseUrl } from "../utils/internal"; | ||
import { buildAuthorization } from "../utils/public"; | ||
import { getUserRecentAchievements } from "./getUserRecentAchievements"; | ||
import type { GetUserRecentAchievementsResponse } from "./models"; | ||
|
||
const server = setupServer(); | ||
|
||
describe("Function: getUserRecentAchievements", () => { | ||
// MSW Setup | ||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
it("is defined #sanity", () => { | ||
// ASSERT | ||
expect(getUserRecentAchievements).toBeDefined(); | ||
}); | ||
|
||
it("retrieves a list of recently-earned user achievements", async () => { | ||
// ARRANGE | ||
const authorization = buildAuthorization({ | ||
userName: "mockUserName", | ||
webApiKey: "mockWebApiKey" | ||
}); | ||
|
||
const mockResponse: GetUserRecentAchievementsResponse = [ | ||
{ | ||
Date: "2023-05-23 22:32:24", | ||
HardcoreMode: 1, | ||
AchievementID: 51_214, | ||
Title: "You're a special Champ!", | ||
Description: | ||
"Win the Tournament as [You] on Hard with 1 attribute on max. and 1 attribute on min.", | ||
BadgeName: "121991", | ||
Points: 25, | ||
Author: "Som1", | ||
GameTitle: "WWF King of the Ring", | ||
GameIcon: "/Images/062599.png", | ||
GameID: 6316, | ||
ConsoleName: "Game Boy", | ||
BadgeURL: "/Badge/121991.png", | ||
GameURL: "/game/6316" | ||
} | ||
]; | ||
|
||
server.use( | ||
rest.get( | ||
`${apiBaseUrl}/API_GetUserRecentAchievements.php`, | ||
(_, res, ctx) => res(ctx.json(mockResponse)) | ||
) | ||
); | ||
|
||
// ACT | ||
const response = await getUserRecentAchievements(authorization, { | ||
userName: "xelnia" | ||
}); | ||
|
||
// ASSERT | ||
expect(response).toEqual([ | ||
{ | ||
date: "2023-05-23 22:32:24", | ||
hardcoreMode: true, | ||
achievementId: 51_214, | ||
title: "You're a special Champ!", | ||
description: | ||
"Win the Tournament as [You] on Hard with 1 attribute on max. and 1 attribute on min.", | ||
badgeName: "121991", | ||
points: 25, | ||
author: "Som1", | ||
gameTitle: "WWF King of the Ring", | ||
gameIcon: "/Images/062599.png", | ||
gameId: 6316, | ||
consoleName: "Game Boy", | ||
badgeUrl: "/Badge/121991.png", | ||
gameUrl: "/game/6316" | ||
} | ||
]); | ||
}); | ||
}); |
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,80 @@ | ||
import { | ||
apiBaseUrl, | ||
buildRequestUrl, | ||
call, | ||
serializeProperties | ||
} from "../utils/internal"; | ||
import type { AuthObject } from "../utils/public"; | ||
import type { | ||
GetUserRecentAchievementsResponse, | ||
UserRecentAchievement | ||
} from "./models"; | ||
|
||
/** | ||
* A call to this function will retrieve a list of a target user's | ||
* recently earned achievements, via their username. By default, it | ||
* fetches achievements earned in the last hour. | ||
* | ||
* @param authorization An object containing your userName and webApiKey. | ||
* This can be constructed with `buildAuthorization()`. | ||
* | ||
* @param payload.userName The user for which to retrieve the summary for. | ||
* | ||
* @param payload.recentMinutes Optional. Defaults to 60. How many minutes | ||
* back to fetch for the given user. | ||
* | ||
* @example | ||
* ``` | ||
* const userRecentAchievements = await getUserRecentAchievements( | ||
* authorization, | ||
* { userName: "xelnia" } | ||
* ); | ||
* ``` | ||
* | ||
* @returns An array containing metadata about a user's recently earned achievements. | ||
* ```json | ||
* [ | ||
* { | ||
* date: '2023-05-23 22:32:24', | ||
* hardcoreMode: true, | ||
* achievementId: 51214, | ||
* title: "You're a special Champ!", | ||
* description: 'Win the Tournament as [You] on Hard with 1 attribute on max. and 1 attribute on min.', | ||
* badgeName: '121991', | ||
* points: 25, | ||
* author: 'Som1', | ||
* gameTitle: 'WWF King of the Ring', | ||
* gameIcon: '/Images/062599.png', | ||
* gameId: 6316, | ||
* consoleName: 'Game Boy', | ||
* badgeUrl: '/Badge/121991.png', | ||
* gameUrl: '/game/6316' | ||
* } | ||
* ] | ||
* ``` | ||
*/ | ||
export const getUserRecentAchievements = async ( | ||
authorization: AuthObject, | ||
payload: { userName: string; recentMinutes?: number } | ||
): Promise<UserRecentAchievement[]> => { | ||
const { userName, recentMinutes } = payload; | ||
|
||
const queryParams: Record<string, string | number> = { u: userName }; | ||
|
||
if (recentMinutes !== undefined) { | ||
queryParams["m"] = recentMinutes; | ||
} | ||
|
||
const url = buildRequestUrl( | ||
apiBaseUrl, | ||
"/API_GetUserRecentAchievements.php", | ||
authorization, | ||
queryParams | ||
); | ||
|
||
const rawResponse = await call<GetUserRecentAchievementsResponse>({ url }); | ||
|
||
return serializeProperties(rawResponse, { | ||
shouldMapToBooleans: ["HardcoreMode"] | ||
}); | ||
}; |
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
19 changes: 19 additions & 0 deletions
19
src/user/models/get-user-recent-achievements-response.model.ts
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,19 @@ | ||
interface GetUserRecentAchievementsEntity { | ||
Date: string; | ||
HardcoreMode: 0 | 1; | ||
AchievementID: number; | ||
Title: string; | ||
Description: string; | ||
BadgeName: string; | ||
Points: number; | ||
Author: string; | ||
GameTitle: string; | ||
GameIcon: string; | ||
GameID: number; | ||
ConsoleName: string; | ||
BadgeURL: string; | ||
GameURL: string; | ||
} | ||
|
||
export type GetUserRecentAchievementsResponse = | ||
GetUserRecentAchievementsEntity[]; |
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,16 @@ | ||
export interface UserRecentAchievement { | ||
date: string; | ||
hardcoreMode: boolean; | ||
achievementId: number; | ||
title: string; | ||
description: string; | ||
badgeName: string; | ||
points: number; | ||
author: string; | ||
gameTitle: string; | ||
gameIcon: string; | ||
gameId: number; | ||
consoleName: string; | ||
badgeUrl: string; | ||
gameUrl: string; | ||
} |
2ccc039
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
retroachievements-api-js – ./
retroachievements-api-js-git-main-retroachievements.vercel.app
retroachievements-api-js-retroachievements.vercel.app
retroachievements-api-js.vercel.app
retroachievements-api-js-alpha.vercel.app