-
Notifications
You must be signed in to change notification settings - Fork 51
feat(user): add get purchased games endpoint #222
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
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
53ec74c
feat: add get purchased games endpoint
bartektricks e5baae7
chore(docs): add docs for purchased game endpoint
bartektricks dcf98b3
chore(docs): update readme with getPurchasedGames endpoint
bartektricks e1a1824
fix(test): fix getPurchasedGames tests
bartektricks 0217429
docs: use proper sort by type for getPurchasedGames
bartektricks 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,203 @@ | ||
| import nock from "nock"; | ||
|
|
||
| import type { AuthorizationPayload, PurchasedGamesResponse } from "../models"; | ||
| import { getPurchasedGames } from "./getPurchasedGames"; | ||
| import { GRAPHQL_BASE_URL } from "./GRAPHQL_BASE_URL"; | ||
|
|
||
| const accessToken = "mockAccessToken"; | ||
|
|
||
| describe("Function: getPurchasedGames", () => { | ||
| afterEach(() => { | ||
| nock.cleanAll(); | ||
| }); | ||
|
|
||
| it("is defined #sanity", () => { | ||
| // ASSERT | ||
| expect(getPurchasedGames).toBeDefined(); | ||
| }); | ||
|
|
||
| it("retrieves purchased games for the user", async () => { | ||
| // ARRANGE | ||
| const mockAuthorization: AuthorizationPayload = { | ||
| accessToken | ||
| }; | ||
|
|
||
| const mockResponse: PurchasedGamesResponse = { | ||
| data: { | ||
| purchasedTitlesRetrieve: { | ||
| __typename: "GameList", | ||
| games: [ | ||
| { | ||
| __typename: "GameLibraryTitle", | ||
| conceptId: "203715", | ||
| entitlementId: "EP2002-CUSA01433_00-ROCKETLEAGUEEU01", | ||
| image: { | ||
| __typename: "Media", | ||
| url: "https://image.api.playstation.com/gs2-sec/appkgo/prod/CUSA01433_00/7/i_5c5e430a49994f22df5fd81f446ead7b6ae45027af490b415fe4e744a9918e4c/i/icon0.png" | ||
| }, | ||
| isActive: true, | ||
| isDownloadable: true, | ||
| isPreOrder: false, | ||
| membership: "NONE", | ||
| name: "Rocket League®", | ||
| platform: "PS4", | ||
| productId: "EP2002-CUSA01433_00-ROCKETLEAGUEEU01", | ||
| titleId: "CUSA01433_00" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const baseUrlObj = new URL(GRAPHQL_BASE_URL); | ||
| const baseUrl = `${baseUrlObj.protocol}//${baseUrlObj.host}`; | ||
| const basePath = baseUrlObj.pathname; | ||
|
|
||
| // ... we need to use a nock matcher to verify the query parameters ... | ||
| const expectedVariables = JSON.stringify({ | ||
| isActive: true, | ||
| platform: ["ps4", "ps5"], | ||
| size: 24, | ||
| start: 0, | ||
| sortBy: "ACTIVE_DATE", | ||
| sortDirection: "desc" | ||
| }); | ||
| const expectedExtensions = JSON.stringify({ | ||
| persistedQuery: { | ||
| version: 1, | ||
| sha256Hash: | ||
| "827a423f6a8ddca4107ac01395af2ec0eafd8396fc7fa204aaf9b7ed2eefa168" | ||
| } | ||
| }); | ||
|
|
||
| const mockScope = nock(baseUrl) | ||
| .get(basePath) | ||
| .query((params) => { | ||
| expect(params.operationName).toEqual("getPurchasedGameList"); | ||
| expect(params.variables).toEqual(expectedVariables); | ||
| expect(params.extensions).toEqual(expectedExtensions); | ||
| return true; | ||
| }) | ||
| .matchHeader("authorization", `Bearer ${accessToken}`) | ||
| .reply(200, mockResponse); | ||
|
|
||
| // ACT | ||
| const response = await getPurchasedGames(mockAuthorization); | ||
|
|
||
| // ASSERT | ||
| expect(response).toEqual(mockResponse); | ||
| expect(mockScope.isDone()).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("retrieves purchased games with custom options", async () => { | ||
| // ARRANGE | ||
| const mockAuthorization: AuthorizationPayload = { | ||
| accessToken | ||
| }; | ||
|
|
||
| const mockResponse: PurchasedGamesResponse = { | ||
| data: { | ||
| purchasedTitlesRetrieve: { | ||
| __typename: "GameList", | ||
| games: [] | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const baseUrlObj = new URL(GRAPHQL_BASE_URL); | ||
| const baseUrl = `${baseUrlObj.protocol}//${baseUrlObj.host}`; | ||
| const basePath = baseUrlObj.pathname; | ||
|
|
||
| const expectedVariables = JSON.stringify({ | ||
| isActive: false, | ||
| platform: ["ps4", "ps5"], | ||
| size: 50, | ||
| start: 0, | ||
| sortBy: "ACTIVE_DATE", | ||
| sortDirection: "desc" | ||
| }); | ||
| const expectedExtensions = JSON.stringify({ | ||
| persistedQuery: { | ||
| version: 1, | ||
| sha256Hash: | ||
| "827a423f6a8ddca4107ac01395af2ec0eafd8396fc7fa204aaf9b7ed2eefa168" | ||
| } | ||
| }); | ||
|
|
||
| const mockScope = nock(baseUrl) | ||
| .get(basePath) | ||
| .query((params) => { | ||
| expect(params.operationName).toEqual("getPurchasedGameList"); | ||
| expect(params.variables).toEqual(expectedVariables); | ||
| expect(params.extensions).toEqual(expectedExtensions); | ||
| return true; | ||
| }) | ||
| .matchHeader("authorization", `Bearer ${accessToken}`) | ||
| .reply(200, mockResponse); | ||
|
|
||
| // ACT | ||
| const response = await getPurchasedGames(mockAuthorization, { | ||
| isActive: false, | ||
| size: 50, | ||
| sortBy: "ACTIVE_DATE" | ||
| }); | ||
|
|
||
| // ASSERT | ||
| expect(response).toEqual(mockResponse); | ||
| expect(mockScope.isDone()).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("throws an error if response data is null", async () => { | ||
| // ARRANGE | ||
| const mockAuthorization: AuthorizationPayload = { | ||
| accessToken | ||
| }; | ||
|
|
||
| const mockErrorResponse = { | ||
| data: null | ||
| }; | ||
|
|
||
| const baseUrlObj = new URL(GRAPHQL_BASE_URL); | ||
| const baseUrl = `${baseUrlObj.protocol}//${baseUrlObj.host}`; | ||
| const basePath = baseUrlObj.pathname; | ||
|
|
||
| nock(baseUrl) | ||
| .get(basePath) | ||
| .query(true) | ||
| .matchHeader("authorization", `Bearer ${accessToken}`) | ||
| .reply(200, mockErrorResponse); | ||
|
|
||
| // ASSERT | ||
| await expect(getPurchasedGames(mockAuthorization)).rejects.toThrowError( | ||
| JSON.stringify(mockErrorResponse) | ||
| ); | ||
| }); | ||
|
|
||
| it("throws an error if purchasedTitlesRetrieve is null", async () => { | ||
| // ARRANGE | ||
| const mockAuthorization: AuthorizationPayload = { | ||
| accessToken | ||
| }; | ||
|
|
||
| const mockErrorResponse = { | ||
| data: { | ||
| purchasedTitlesRetrieve: null | ||
| } | ||
| }; | ||
|
|
||
| const baseUrlObj = new URL(GRAPHQL_BASE_URL); | ||
| const baseUrl = `${baseUrlObj.protocol}//${baseUrlObj.host}`; | ||
| const basePath = baseUrlObj.pathname; | ||
|
|
||
| nock(baseUrl) | ||
| .get(basePath) | ||
| .query(true) | ||
| .matchHeader("authorization", `Bearer ${accessToken}`) | ||
| .reply(200, mockErrorResponse); | ||
|
|
||
| // ASSERT | ||
| await expect(getPurchasedGames(mockAuthorization)).rejects.toThrowError( | ||
| JSON.stringify(mockErrorResponse) | ||
| ); | ||
| }); | ||
| }); |
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,76 @@ | ||
| import type { AuthorizationPayload, PurchasedGamesResponse } from "../models"; | ||
| import { Membership } from "../models/membership.model"; | ||
| import { call } from "../utils/call"; | ||
| import { GRAPHQL_BASE_URL } from "./GRAPHQL_BASE_URL"; | ||
| import { getPurchasedGameListHash } from "./operationHashes"; | ||
|
|
||
| type GetPurchasedGamesOptions = { | ||
| isActive: boolean; | ||
| platform: ("ps4" | "ps5")[]; | ||
| size: number; | ||
| start: number; | ||
| sortBy: "ACTIVE_DATE"; | ||
| sortDirection: "asc" | "desc"; | ||
| membership: Membership; | ||
| }; | ||
|
|
||
| /** | ||
| * A call to this function will retrieve purchased games for the user associated | ||
| * with the npsso token provided to this module during initialisation. | ||
| * | ||
| * This endpoint returns only PS4 and PS5 games. | ||
| * | ||
| * @param authorization An object containing your access token, typically retrieved with `exchangeAccessCodeForAuthTokens()`. | ||
| * @param options Optional parameters to filter and sort purchased games. | ||
| */ | ||
| export const getPurchasedGames = async ( | ||
| authorization: AuthorizationPayload, | ||
| options: Partial<GetPurchasedGamesOptions> = {} | ||
| ): Promise<PurchasedGamesResponse> => { | ||
| const url = new URL(GRAPHQL_BASE_URL); | ||
|
|
||
| const { | ||
| isActive = true, | ||
| platform = ["ps4", "ps5"], | ||
| size = 24, | ||
| start = 0, | ||
| sortBy = "ACTIVE_DATE", | ||
| sortDirection = "desc", | ||
| ...restOptions | ||
| } = options; | ||
|
|
||
| url.searchParams.set("operationName", "getPurchasedGameList"); | ||
| url.searchParams.set( | ||
| "variables", | ||
| JSON.stringify({ | ||
| isActive, | ||
| platform, | ||
| size, | ||
| start, | ||
| sortBy, | ||
| sortDirection, | ||
| ...restOptions | ||
| }) | ||
| ); | ||
| url.searchParams.set( | ||
| "extensions", | ||
| JSON.stringify({ | ||
| persistedQuery: { | ||
| version: 1, | ||
| sha256Hash: getPurchasedGameListHash | ||
| } | ||
| }) | ||
| ); | ||
|
|
||
| const response = await call<PurchasedGamesResponse>( | ||
| { url: url.toString() }, | ||
| authorization | ||
| ); | ||
|
|
||
| // The GraphQL queries can return non-truthy values. | ||
| if (!response.data || !response.data.purchasedTitlesRetrieve) { | ||
| throw new Error(JSON.stringify(response)); | ||
| } | ||
|
|
||
| return response; | ||
| }; |
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from "./getPurchasedGames"; | ||
| export * from "./getRecentlyPlayedGames"; |
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
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 @@ | ||
| export type Membership = "NONE" | "PS_PLUS"; |
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,52 @@ | ||
| import { Membership } from "./membership.model"; | ||
| import { TitlePlatform } from "./title-platform.model"; | ||
|
|
||
| export interface PurchasedGame { | ||
| /** GraphQL object type/schema */ | ||
| __typename: "GameLibraryTitle"; | ||
|
|
||
| /** Unique concept identifier for the game */ | ||
| conceptId: string | null; | ||
|
|
||
| /** Unique entitlement identifier */ | ||
| entitlementId: string; | ||
|
|
||
| /** Contains a url to a game icon file */ | ||
| image: { | ||
| __typename: "Media"; | ||
| url: string; | ||
| }; | ||
|
|
||
| /** Whether the game is currently active */ | ||
| isActive: boolean; | ||
|
|
||
| /** Whether the game is downloadable */ | ||
| isDownloadable: boolean; | ||
|
|
||
| /** Whether the game is a pre-order */ | ||
| isPreOrder: boolean; | ||
|
|
||
| /** The membership level associated with this game */ | ||
| membership: Membership; | ||
|
|
||
| /** The name of the game */ | ||
| name: string; | ||
|
|
||
| /** The platform this game is available on */ | ||
| platform: TitlePlatform; | ||
|
|
||
| /** Unique product identifier */ | ||
| productId: string; | ||
|
|
||
| /** Unique title identifier */ | ||
| titleId: string; | ||
| } | ||
|
|
||
| export interface PurchasedGamesResponse { | ||
| data: { | ||
| purchasedTitlesRetrieve: { | ||
| __typename: "GameList"; | ||
| games: PurchasedGame[]; | ||
| }; | ||
| }; | ||
| } | ||
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
Oops, something went wrong.
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.