Skip to content

Commit

Permalink
refactor: simplify authorization API
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland committed Oct 12, 2024
1 parent 29d9910 commit 5a37923
Show file tree
Hide file tree
Showing 35 changed files with 138 additions and 148 deletions.
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,20 @@ Learn how to authenticate and start pulling data from RetroAchievements on our d

To use any endpoint function in the API, you must first be authorized by RetroAchievements. Fortunately, this is a fairly straightforward process.

1. Visit [your control panel](https://retroachievements.org/controlpanel.php) on the RA website.
1. Visit [settings page](https://retroachievements.org/settings) on the RA website.

2. Find the "Keys" section on the page. Copy the web API key value. **Do not expose your API key anywhere publicly.**

3. You can now create your authorization object using your web API key.
3. You now have all you need to use any function from the API. Each function takes this web API key as its first argument. Here's an example:

```ts
import { buildAuthorization } from "@retroachievements/api";
import { getGame } from "@retroachievements/api";

const username = "<your username on RA>";
const webApiKey = "<your web API key>";

const authorization = buildAuthorization({ username, webApiKey });
```

4. You now have all you need to use any function in the API. Each function takes this authorization object as its first argument. Here's an example:

```ts
import { getGame } from "@retroachievements/api";

// This returns basic metadata about the game on this page:
// https://retroachievements.org/game/14402
const game = await getGame(authorization, { gameId: 14402 });
const game = await getGame(webApiKey, { gameId: 14402 });
```

## API
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"format:check": "prettier --check . '**/*.{json,md,js,ts,tsx}'",
"lint": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx .",
"lint:fix": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx . --fix",
"test": "vitest run",
"test": "vitest run --reporter=dot",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage",
"verify": "pnpm format:check && pnpm lint && pnpm test:coverage && pnpm build",
Expand Down
9 changes: 4 additions & 5 deletions src/achievement/getAchievementUnlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type { ApiAuthorization } from "../utils/public";
import type {
AchievementUnlocksMetadata,
GetAchievementUnlocksResponse,
Expand All @@ -15,8 +15,7 @@ import type {
* A call to this function will retrieve a list of users who
* have earned a given achievement, targeted by the achievement's ID.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
* @param authorization Your web API key retrieved from retroachievements.org/settings.
*
* @param payload.achievementId The target achievement we want to
* retrieve the unlocks list for. If unknown, this can be found
Expand All @@ -31,7 +30,7 @@ import type {
* @example
* ```
* const achievementUnlocks = await getAchievementUnlocks(
* authorization,
* webApiKey,
* { achievementId: 13876 }
* );
* ```
Expand All @@ -50,7 +49,7 @@ import type {
* ```
*/
export const getAchievementUnlocks = async (
authorization: AuthObject,
authorization: ApiAuthorization,
payload: { achievementId: ID; offset?: number; count?: number }
): Promise<AchievementUnlocksMetadata> => {
const { achievementId, offset, count } = payload;
Expand Down
7 changes: 3 additions & 4 deletions src/console/getConsoleIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import {
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type { ApiAuthorization } from "../utils/public";
import type { FetchedSystem, GetConsoleIdsResponse } from "./models";

/**
* A call to this function will retrieve the complete list
* of console ID and name pairs on the RetroAchievements.org
* platform.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
* @param authorization Your web API key retrieved from retroachievements.org/settings.
*
* @param payload.shouldOnlyRetrieveActiveSystems If true, only systems that
* officially support achievements will be returned.
Expand All @@ -39,7 +38,7 @@ import type { FetchedSystem, GetConsoleIdsResponse } from "./models";
* ```
*/
export const getConsoleIds = async (
authorization: AuthObject,
authorization: ApiAuthorization,
payload?: {
shouldOnlyRetrieveActiveSystems: boolean;
shouldOnlyRetrieveGameSystems: boolean;
Expand Down
7 changes: 3 additions & 4 deletions src/console/getGameList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import {
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type { ApiAuthorization } from "../utils/public";
import type { GameList, GetGameListResponse } from "./models";
/**
* A call to this function will retrieve the complete list
* of games for a specified console on the RetroAchievements.org
* platform.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
* @param authorization Your web API key retrieved from retroachievements.org/settings.
*
* @param payload.consoleId The unique console ID to retrieve a list of
* games from. The list of consoleIds can be retrieved using the `getConsoleIds()`
Expand Down Expand Up @@ -53,7 +52,7 @@ import type { GameList, GetGameListResponse } from "./models";
* ```
*/
export const getGameList = async (
authorization: AuthObject,
authorization: ApiAuthorization,
payload: {
consoleId: ID;
shouldOnlyRetrieveGamesWithAchievements?: boolean;
Expand Down
7 changes: 3 additions & 4 deletions src/feed/getAchievementOfTheWeek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type { ApiAuthorization } from "../utils/public";
import type {
AchievementOfTheWeek,
GetAchievementOfTheWeekResponse,
Expand All @@ -14,8 +14,7 @@ import type {
* A call to this function will retrieve comprehensive
* metadata about the current Achievement of the Week.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
* @param authorization Your web API key retrieved from retroachievements.org/settings.
*
* @example
* ```
Expand Down Expand Up @@ -59,7 +58,7 @@ import type {
* ```
*/
export const getAchievementOfTheWeek = async (
authorization: AuthObject
authorization: ApiAuthorization
): Promise<AchievementOfTheWeek> => {
const url = buildRequestUrl(
apiBaseUrl,
Expand Down
7 changes: 3 additions & 4 deletions src/feed/getActiveClaims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import {
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type { ApiAuthorization } from "../utils/public";
import type { GetSetClaimsResponse, SetClaim } from "./models";

/**
* A call to this function returns information about all
* (1000 max) active set claims.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
* @param authorization Your web API key retrieved from retroachievements.org/settings.
*
* @example
* ```
Expand Down Expand Up @@ -45,7 +44,7 @@ import type { GetSetClaimsResponse, SetClaim } from "./models";
* ```
*/
export const getActiveClaims = async (
authorization: AuthObject
authorization: ApiAuthorization
): Promise<SetClaim[]> => {
const url = buildRequestUrl(
apiBaseUrl,
Expand Down
21 changes: 19 additions & 2 deletions src/feed/getClaims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@ import {
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type { ApiAuthorization } from "../utils/public";
import type { GetSetClaimsResponse, SetClaim } from "./models";

type ClaimKind = "completed" | "dropped" | "expired";

/**
* A call to this function will retrieve a list of achievement set claims.
*
* @param authorization Your web API key retrieved from retroachievements.org/settings.
*
* @param payload.claimKind The specific kind of claims you'd like to retrieve a list of.
*
* @example
* ```
* const claims = await getClaims(
* authorization,
* { claimKind: "completed" }
* );
* ```
*
* @returns An array containing all the specified claims.
*/
export const getClaims = async (
authorization: AuthObject,
authorization: ApiAuthorization,
payload: { claimKind: ClaimKind }
): Promise<SetClaim[]> => {
const { claimKind } = payload;
Expand Down
7 changes: 3 additions & 4 deletions src/feed/getRecentGameAwards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import {
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject, AwardKind } from "../utils/public";
import type { ApiAuthorization, 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 authorization Your web API key retrieved from retroachievements.org/settings.
*
* @param payload.startDate The date to fetch awards from.
*
Expand Down Expand Up @@ -50,7 +49,7 @@ import type { GetRecentGameAwardsResponse, RecentGameAwards } from "./models";
* ```
*/
export const getRecentGameAwards = async (
authorization: AuthObject,
authorization: ApiAuthorization,
payload?: Partial<{
startDate: string;
offset: number;
Expand Down
7 changes: 3 additions & 4 deletions src/feed/getTopTenUsers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { apiBaseUrl, buildRequestUrl, call } from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type { ApiAuthorization } from "../utils/public";
import type {
GetTopTenUsersResponse,
TopTenUsers,
Expand All @@ -10,8 +10,7 @@ import type {
* A call to this function will retrieve the current top ten users
* on the site.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
* @param authorization Your web API key retrieved from retroachievements.org/settings.
*
* @example
* ```
Expand All @@ -28,7 +27,7 @@ import type {
* ```
*/
export const getTopTenUsers = async (
authorization: AuthObject
authorization: ApiAuthorization
): Promise<TopTenUsers> => {
const url = buildRequestUrl(
apiBaseUrl,
Expand Down
7 changes: 3 additions & 4 deletions src/game/getAchievementCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import {
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type { ApiAuthorization } from "../utils/public";
import type { AchievementCount, GetAchievementCountResponse } from "./models";

/**
* A call to this function will retrieve the list of
* achievement IDs for a game, targeted by game ID.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
* @param authorization Your web API key retrieved from retroachievements.org/settings.
*
* @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
Expand All @@ -35,7 +34,7 @@ import type { AchievementCount, GetAchievementCountResponse } from "./models";
* ```
*/
export const getAchievementCount = async (
authorization: AuthObject,
authorization: ApiAuthorization,
payload: { gameId: ID }
): Promise<AchievementCount> => {
const { gameId } = payload;
Expand Down
7 changes: 3 additions & 4 deletions src/game/getAchievementDistribution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ID } from "../utils/internal";
import { apiBaseUrl, buildRequestUrl, call } from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type { ApiAuthorization } from "../utils/public";
import type {
AchievementDistributionFlags,
GetAchievementDistributionResponse,
Expand All @@ -11,8 +11,7 @@ import type {
* of the number of players who have earned a specific
* number of achievements for a given game ID.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
* @param authorization Your web API key retrieved from retroachievements.org/settings.
*
* @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
Expand Down Expand Up @@ -57,7 +56,7 @@ import type {
* ```
*/
export const getAchievementDistribution = async (
authorization: AuthObject,
authorization: ApiAuthorization,
payload: {
gameId: ID;
flags?: AchievementDistributionFlags;
Expand Down
7 changes: 3 additions & 4 deletions src/game/getGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import {
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type { ApiAuthorization } from "../utils/public";
import type { Game, GetGameResponse } from "./models";

/**
* A call to this function will retrieve basic metadata about
* a game, targeted via its unique ID.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
* @param authorization Your web API key retrieved from retroachievements.org/settings.
*
* @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
Expand Down Expand Up @@ -52,7 +51,7 @@ import type { Game, GetGameResponse } from "./models";
* ```
*/
export const getGame = async (
authorization: AuthObject,
authorization: ApiAuthorization,
payload: { gameId: ID }
): Promise<Game> => {
const { gameId } = payload;
Expand Down
7 changes: 3 additions & 4 deletions src/game/getGameExtended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import {
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type { ApiAuthorization } from "../utils/public";
import type { GameExtended, GetGameExtendedResponse } from "./models";

/**
* A call to this function will retrieve extended metadata
* about a game, targeted via its unique ID.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
* @param authorization Your web API key retrieved from retroachievements.org/settings.
*
* @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
Expand Down Expand Up @@ -72,7 +71,7 @@ import type { GameExtended, GetGameExtendedResponse } from "./models";
* ```
*/
export const getGameExtended = async (
authorization: AuthObject,
authorization: ApiAuthorization,
payload: { gameId: ID; isRequestingUnofficialAchievements?: boolean }
): Promise<GameExtended> => {
const { gameId, isRequestingUnofficialAchievements } = payload;
Expand Down
Loading

0 comments on commit 5a37923

Please sign in to comment.