-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new getGameHashes() function (#114)
- Loading branch information
1 parent
29d9910
commit 18c12fb
Showing
9 changed files
with
177 additions
and
2 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
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,77 @@ | ||
/* eslint-disable sonarjs/no-duplicate-string */ | ||
|
||
import { http, HttpResponse } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
|
||
import { apiBaseUrl } from "../utils/internal"; | ||
import { buildAuthorization } from "../utils/public"; | ||
import { getGameHashes } from "./getGameHashes"; | ||
import type { GetGameHashesResponse } from "./models"; | ||
|
||
const server = setupServer(); | ||
|
||
describe("Function: getGameExtended", () => { | ||
// MSW Setup | ||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
it("is defined #sanity", () => { | ||
// ASSERT | ||
expect(getGameHashes).toBeDefined(); | ||
}); | ||
|
||
it("given a game ID, retrieves extended metadata about the game", async () => { | ||
// ARRANGE | ||
const authorization = buildAuthorization({ | ||
username: "mockUserName", | ||
webApiKey: "mockWebApiKey", | ||
}); | ||
|
||
const mockResponse: GetGameHashesResponse = { | ||
Results: [ | ||
{ | ||
Name: "Sonic The Hedgehog (USA, Europe) (Ru) (NewGame).md", | ||
MD5: "1b1d9ac862c387367e904036114c4825", | ||
Labels: ["nointro", "rapatches"], | ||
PatchUrl: | ||
"https://github.com/RetroAchievements/RAPatches/raw/main/MD/Translation/Russian/1-Sonic1-Russian.zip", | ||
}, | ||
{ | ||
Name: "Sonic The Hedgehog (USA, Europe).md", | ||
MD5: "1bc674be034e43c96b86487ac69d9293", | ||
Labels: ["nointro"], | ||
PatchUrl: null, | ||
}, | ||
], | ||
}; | ||
|
||
server.use( | ||
http.get(`${apiBaseUrl}/API_GetGameHashes.php`, () => | ||
HttpResponse.json(mockResponse) | ||
) | ||
); | ||
|
||
// ACT | ||
const response = await getGameHashes(authorization, { gameId: 1 }); | ||
|
||
// ASSERT | ||
expect(response).toEqual({ | ||
results: [ | ||
{ | ||
name: "Sonic The Hedgehog (USA, Europe) (Ru) (NewGame).md", | ||
md5: "1b1d9ac862c387367e904036114c4825", | ||
labels: ["nointro", "rapatches"], | ||
patchUrl: | ||
"https://github.com/RetroAchievements/RAPatches/raw/main/MD/Translation/Russian/1-Sonic1-Russian.zip", | ||
}, | ||
{ | ||
name: "Sonic The Hedgehog (USA, Europe).md", | ||
md5: "1bc674be034e43c96b86487ac69d9293", | ||
labels: ["nointro"], | ||
patchUrl: null, | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
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,66 @@ | ||
import type { ID } from "../utils/internal"; | ||
import { | ||
apiBaseUrl, | ||
buildRequestUrl, | ||
call, | ||
serializeProperties, | ||
} from "../utils/internal"; | ||
import type { AuthObject } from "../utils/public"; | ||
import type { GameHashes, GetGameHashesResponse } from "./models"; | ||
|
||
/** | ||
* A call to this function will retrieve a list of hashes linked to a game. | ||
* | ||
* @param authorization An object containing your username and webApiKey. | ||
* This can be constructed with `buildAuthorization()`. | ||
* | ||
* @param payload.gameId The unique game ID. If you are unsure, open the | ||
* game's page on the RetroAchievements.org website. For example, Dragster's | ||
* URL is https://retroachievements.org/game/14402. We can see from the | ||
* URL that the game ID is "14402". | ||
* | ||
* @example | ||
* ``` | ||
* const game = await getGameHashes( | ||
* authorization, | ||
* { gameId: 14402 } | ||
* ); | ||
* ``` | ||
* | ||
* @returns An object containing a list of game hashes. | ||
* ```json | ||
* { | ||
* "Results": [ | ||
* { | ||
* "MD5": "1b1d9ac862c387367e904036114c4825", | ||
* "Name": "Sonic The Hedgehog (USA, Europe) (Ru) (NewGame).md", | ||
* "Labels": ["nointro", "rapatches"], | ||
* "PatchUrl": "https://github.com/RetroAchievements/RAPatches/raw/main/MD/Translation/Russian/1-Sonic1-Russian.zip" | ||
* }, | ||
* { | ||
* "MD5": "1bc674be034e43c96b86487ac69d9293", | ||
* "Name": "Sonic The Hedgehog (USA, Europe).md", | ||
* "Labels": ["nointro"], | ||
* "PatchUrl": null | ||
* } | ||
* ] | ||
* } | ||
* ``` | ||
*/ | ||
export const getGameHashes = async ( | ||
authorization: AuthObject, | ||
payload: { gameId: ID } | ||
): Promise<GameHashes> => { | ||
const { gameId } = payload; | ||
|
||
const url = buildRequestUrl( | ||
apiBaseUrl, | ||
"/API_GetGameHashes.php", | ||
authorization, | ||
{ i: gameId } | ||
); | ||
|
||
const rawResponse = await call<GetGameHashesResponse>({ 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
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,10 @@ | ||
interface GameHash { | ||
md5: string; | ||
name: string; | ||
labels: string[]; | ||
patchUrl: string; | ||
} | ||
|
||
export interface GameHashes { | ||
results: GameHash[]; | ||
} |
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,10 @@ | ||
interface GameHashResult { | ||
MD5: string; | ||
Name: string; | ||
Labels: string[]; | ||
PatchUrl: string; | ||
} | ||
|
||
export interface GetGameHashesResponse { | ||
Results: GameHashResult[]; | ||
} |
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