-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add getUserSetRequests() function #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wescopeland
merged 4 commits into
RetroAchievements:main
from
stiefeljackal:feat/user/getUserSetRequests
Feb 4, 2026
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bfbd1f2
feat: add getUserSetRequests() function
stiefeljackal 8fef5b3
docs: add getUserSetRequests() to README
stiefeljackal 8f66f77
Update src/user/getUserSetRequests.ts
wescopeland 9415eaa
Merge branch 'main' into feat/user/getUserSetRequests
wescopeland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,149 @@ | ||
| import { http, HttpResponse } from "msw"; | ||
| import { setupServer } from "msw/node"; | ||
|
|
||
| import { apiBaseUrl } from "../utils/internal"; | ||
| import { buildAuthorization } from "../utils/public"; | ||
| import { getUserSetRequests } from "./getUserSetRequests"; | ||
| import type { GetUserSetRequestsResponse, UserSetRequests } from "./models"; | ||
| import { RequestListType } from "./models"; | ||
|
|
||
| const server = setupServer(); | ||
|
|
||
| describe("Function: getUserSetRequests", () => { | ||
| // MSW Setup | ||
| beforeAll(() => server.listen()); | ||
| afterEach(() => server.resetHandlers()); | ||
| afterAll(() => server.close()); | ||
|
|
||
| it("is defined #sanity", () => { | ||
| // ASSERT | ||
| expect(getUserSetRequests).toBeDefined(); | ||
| }); | ||
|
|
||
| it("using defaults, retrieves the list of set requests of the given user", async () => { | ||
| // ARRANGE | ||
| const authorization = buildAuthorization({ | ||
| username: "mockUserName", | ||
| webApiKey: "mockWebApiKey", | ||
| }); | ||
|
|
||
| const mockResponse = mockGetUserSetRequestsResponse; | ||
|
|
||
| server.use( | ||
| http.get(`${apiBaseUrl}/API_GetUserSetRequests.php`, (info) => { | ||
| const url = new URL(info.request.url); | ||
| expect(url.searchParams.get("u")).toEqual(mockOtherUsername); | ||
| expect(url.searchParams.has("t")).toBeFalsy(); | ||
| return HttpResponse.json(mockResponse); | ||
| }) | ||
| ); | ||
|
|
||
| // ACT | ||
| const response = await getUserSetRequests(authorization, { | ||
| username: mockOtherUsername, | ||
| }); | ||
| expect(response).toEqual(mockUserSetRequestsValue); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { requestListType: RequestListType.ActiveRequests }, | ||
| { requestListType: RequestListType.AllRequests }, | ||
| ])( | ||
| "calls the 'User Set Requests' endpoint with a given request list type ($requestListType)", | ||
| async ({ requestListType: expectedRequestListType }) => { | ||
| // ARRANGE | ||
| const authorization = buildAuthorization({ | ||
| username: "mockUserName", | ||
| webApiKey: "mockWebApiKey", | ||
| }); | ||
|
|
||
| server.use( | ||
| http.get(`${apiBaseUrl}/API_GetUserSetRequests.php`, (info) => { | ||
| const url = new URL(info.request.url); | ||
| expect(url.searchParams.get("u")).toEqual(mockOtherUsername); | ||
| expect(url.searchParams.get("t")).toEqual( | ||
| String(expectedRequestListType) | ||
| ); | ||
| return HttpResponse.json(mockGetUserSetRequestsResponse); | ||
| }) | ||
| ); | ||
|
|
||
| // ACT | ||
| await getUserSetRequests(authorization, { | ||
| username: mockOtherUsername, | ||
| requestListType: expectedRequestListType, | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| it.each([ | ||
| { status: 503, statusText: "The API is currently down" }, | ||
| { status: 422, statusText: "HTTP Error: Status 422 Unprocessable Entity" }, | ||
| ])( | ||
| "given the API returns a $status, throws an error", | ||
| async ({ status, statusText }) => { | ||
| // ARRANGE | ||
| const authorization = buildAuthorization({ | ||
| username: "mockUserName", | ||
| webApiKey: "mockWebApiKey", | ||
| }); | ||
|
|
||
| const mockResponse = `<html><body>${statusText}</body></html>`; | ||
|
|
||
| server.use( | ||
| http.get(`${apiBaseUrl}/API_GetUserSetRequests.php`, () => | ||
| HttpResponse.json(mockResponse, { status, statusText }) | ||
| ) | ||
| ); | ||
|
|
||
| // ASSERT | ||
| await expect( | ||
| getUserSetRequests(authorization, { username: mockOtherUsername }) | ||
| ).rejects.toThrow(); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| const mockOtherUsername = "otherMockUser"; | ||
|
|
||
| const mockGetUserSetRequestsResponse: GetUserSetRequestsResponse = { | ||
| RequestedSets: [ | ||
| { | ||
| GameID: 8149, | ||
| Title: "Example Set 1", | ||
| ConsoleID: 0, | ||
| ConsoleName: "Example Console", | ||
| ImageIcon: "/Images/000001.png", | ||
| }, | ||
| { | ||
| GameID: 9001, | ||
| Title: "Example Set 2", | ||
| ConsoleID: 2, | ||
| ConsoleName: "Example Console 2", | ||
| ImageIcon: "/Images/000002.png", | ||
| }, | ||
| ], | ||
| TotalRequests: 5, | ||
| PointsForNext: 5000, | ||
| }; | ||
|
|
||
| const mockUserSetRequestsValue: UserSetRequests = { | ||
| requestedSets: [ | ||
| { | ||
| gameId: 8149, | ||
| title: "Example Set 1", | ||
| consoleId: 0, | ||
| consoleName: "Example Console", | ||
| imageIcon: "/Images/000001.png", | ||
| }, | ||
| { | ||
| gameId: 9001, | ||
| title: "Example Set 2", | ||
| consoleId: 2, | ||
| consoleName: "Example Console 2", | ||
| imageIcon: "/Images/000002.png", | ||
| }, | ||
| ], | ||
| totalRequests: 5, | ||
| pointsForNext: 5000, | ||
| }; |
This file contains hidden or 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,92 @@ | ||
| import { | ||
| apiBaseUrl, | ||
| buildRequestUrl, | ||
| call, | ||
| serializeProperties, | ||
| } from "../utils/internal"; | ||
| import type { AuthObject } from "../utils/public"; | ||
| import type { | ||
| GetUserSetRequestsResponse, | ||
| RequestListType, | ||
| UserSetRequests, | ||
| } from "./models"; | ||
|
|
||
| /** | ||
| * A call to this function will retrieve a given user's set requests. | ||
| * | ||
| * @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 set requests | ||
| * for. | ||
| * | ||
| * @param payload.requestListType An optional parameter to filter set requests | ||
| * by their current status. If omittted, the API will return only active | ||
| * requests. | ||
| * | ||
| * @example | ||
| * ``` | ||
| * const userSetRequests = await getUserSetRequests(authorization, { | ||
| * username: "ExampleUser" | ||
| * }); | ||
| * ``` | ||
| * | ||
| * @returns An object containing a list of requested sets that the | ||
| * given user made. | ||
| * ```json | ||
| * { | ||
| * "requestedSets": [ | ||
| * { | ||
| * "gameId": 8149, | ||
| * "title": "Example Set 1", | ||
| * "consoleId": 0, | ||
| * "consoleName": "Example Console", | ||
| * "imageIcon": "/Images/000001.png" | ||
| * }, | ||
| * { | ||
| * "gameId": 9001, | ||
| * "title": "Example Set 2", | ||
| * "consoleId": 2, | ||
| * "consoleName": "Example Console 2", | ||
| * "imageIcon": "/Images/000002.png" | ||
| * } | ||
| * ], | ||
| * "totalRequests": 5, | ||
| * "pointsForNext": 5000 | ||
| * } | ||
| * ``` | ||
| * | ||
| * @throws If the API was given invalid parameters (422) or if the | ||
| * API is currently down (503). | ||
| */ | ||
| export const getUserSetRequests = async ( | ||
| authorization: AuthObject, | ||
| payload: { username: string; requestListType?: RequestListType } | ||
| ): Promise<UserSetRequests> => { | ||
| const queryParams: Record<string, number | string> = {}; | ||
| queryParams.u = payload.username; | ||
| if ( | ||
| payload.requestListType !== null && | ||
| payload.requestListType !== undefined | ||
| ) { | ||
| queryParams.t = payload.requestListType; | ||
| } | ||
|
|
||
| const url = buildRequestUrl( | ||
| apiBaseUrl, | ||
| "/API_GetUserSetRequests.php", | ||
| authorization, | ||
| queryParams | ||
| ); | ||
|
|
||
| const rawResponse = await call<GetUserSetRequestsResponse>({ url }); | ||
|
|
||
| return serializeProperties(rawResponse, { | ||
| shouldCastToNumbers: [ | ||
| "GameID", | ||
| "ConsoleID", | ||
| "TotalRequests", | ||
| "PointsForNext", | ||
| ], | ||
| }); | ||
| }; | ||
This file contains hidden or 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 hidden or 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,11 @@ | ||
| export interface GetUserSetRequestsResponse { | ||
| RequestedSets: Array<{ | ||
| GameID: number | string; | ||
| Title: string; | ||
| ConsoleID: number | string; | ||
| ConsoleName: string; | ||
| ImageIcon: string; | ||
| }>; | ||
| TotalRequests: number; | ||
| PointsForNext: number; | ||
| } |
This file contains hidden or 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 hidden or 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 enum RequestListType { | ||
| ActiveRequests = 0, | ||
| AllRequests = 1, | ||
| } |
This file contains hidden or 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,11 @@ | ||
| export interface UserSetRequests { | ||
| requestedSets: Array<{ | ||
| gameId: number; | ||
| title: string; | ||
| consoleId: number; | ||
| consoleName: string; | ||
| imageIcon: string; | ||
| }>; | ||
| totalRequests: number; | ||
| pointsForNext: number; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.