Skip to content

Commit

Permalink
achievements-app#126 - PR review changes: naming, base url and code l…
Browse files Browse the repository at this point in the history
…ocation
  • Loading branch information
alekpentchev committed Mar 29, 2023
1 parent 3745c80 commit 642cd10
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { rest } from "msw";
import { setupServer } from "msw/node";

import { AuthorizationPayload } from "../models";
import { GetUserPurchasedGamesResponse } from "../models/user-purchased-games-response.model";
import { getUserPurchasedGames } from "./getUserPurchasedGames";
import { USER_GAMES_BASE_URL } from "./USER_GAMES_BASE_URL";
import { GetPurchasedGamesResponse } from "../models/purchased-games-response.model";
import { getPurchasedGames } from "./getPurchasedGames";
import { GRAPHQL_BASE_URL } from "./GRAPHQL_BASE_URL";

const server = setupServer();

Expand All @@ -16,7 +16,7 @@ describe("Function: getUserPurchasedGames", () => {

it("is defined #sanity", () => {
// ASSERT
expect(getUserPurchasedGames).toBeDefined();
expect(getPurchasedGames).toBeDefined();
});

it("retrieve the games library for a given user", async () => {
Expand All @@ -25,7 +25,7 @@ describe("Function: getUserPurchasedGames", () => {
accessToken: "mockAccessToken"
};

const mockResponse: GetUserPurchasedGamesResponse = {
const mockResponse: GetPurchasedGamesResponse = {
data: {
purchasedTitlesRetrieve: {
__typename: "GameList",
Expand Down Expand Up @@ -70,13 +70,13 @@ describe("Function: getUserPurchasedGames", () => {
};

server.use(
rest.get(USER_GAMES_BASE_URL, (_, res, ctx) => {
rest.get(GRAPHQL_BASE_URL, (_, res, ctx) => {
return res(ctx.json(mockResponse));
})
);

// ACT
const response = await getUserPurchasedGames(mockAuthorization);
const response = await getPurchasedGames(mockAuthorization);
// ASSERT
expect(response).toEqual(mockResponse);
});
Expand All @@ -96,12 +96,12 @@ describe("Function: getUserPurchasedGames", () => {
};

server.use(
rest.get(USER_GAMES_BASE_URL, (_, res, ctx) => {
rest.get(GRAPHQL_BASE_URL, (_, res, ctx) => {
return res(ctx.json(mockResponse));
})
);

// ASSERT
await expect(getUserPurchasedGames(mockAuthorization)).rejects.toThrow();
await expect(getPurchasedGames(mockAuthorization)).rejects.toThrow();
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// import { AuthorizationPayload, ProfileFromUserNameResponse } from "../models";

import { AuthorizationPayload } from "../models";
import { GetUserPurchasedGamesResponse } from "../models/user-purchased-games-response.model";
import { GetPurchasedGamesResponse } from "../models/purchased-games-response.model";
import { call } from "../utils/call";
import { USER_GAMES_BASE_URL } from "./USER_GAMES_BASE_URL";
import { GRAPHQL_BASE_URL } from "./GRAPHQL_BASE_URL";

export type GetUserPurchasedGamesOptions = {
export type GetPurchasedGamesOptions = {
platform?: Array<"ps4" | "ps5">;
size?: number;
start?: number;
Expand All @@ -15,7 +13,7 @@ export type GetUserPurchasedGamesOptions = {
subscriptionService?: string;
};

const defaultOptions: GetUserPurchasedGamesOptions = {
const defaultOptions: GetPurchasedGamesOptions = {
platform: ["ps4", "ps5"],
size: 500,
start: 0,
Expand All @@ -39,18 +37,18 @@ const defaultOptions: GetUserPurchasedGamesOptions = {
* @param options.isActive Whether to return active games only. Defaults to `true`.
* @param options.subscriptionService The subscription service to filter by. Defaults to `"NONE"`.
*/
export const getUserPurchasedGames = async (
export const getPurchasedGames = async (
authorization: AuthorizationPayload,
options: Partial<GetUserPurchasedGamesOptions> = defaultOptions
options: Partial<GetPurchasedGamesOptions> = defaultOptions
): Promise<any> => {
const operationName = "getPurchasedGameList";
const variables = encodeURIComponent(JSON.stringify(options));
const extensions = encodeURIComponent(
'{"persistedQuery":{"version":1,"sha256Hash":"2c045408b0a4d0264bb5a3edfed4efd49fb4749cf8d216be9043768adff905e2"}}'
);
const url = `${USER_GAMES_BASE_URL}?operationName=${operationName}&variables=${variables}&extensions=${extensions}`;
const url = `${GRAPHQL_BASE_URL}?operationName=${operationName}&variables=${variables}&extensions=${extensions}`;

const response = await call<GetUserPurchasedGamesResponse>(
const response = await call<GetPurchasedGamesResponse>(
{ url },
authorization
);
Expand All @@ -59,5 +57,12 @@ export const getUserPurchasedGames = async (
throw new Error((response as any)?.error?.message ?? "Unexpected Error");
}

// The GraphQL queries can return non-truthy values.
if (!response.data || !response.data.purchasedTitlesRetrieve) {
throw new Error(JSON.stringify(response));
}

return response;

return response;
};
1 change: 1 addition & 0 deletions src/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./getPurchasedGames";
export * from "./getRecentlyPlayedGames";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TitlePlatform } from "./title-platform.model";

export interface GetUserPurchasedGamesResponse {
export interface GetPurchasedGamesResponse {
data: {
purchasedTitlesRetrieve: {
/**
Expand Down
2 changes: 0 additions & 2 deletions src/user/USER_GAMES_BASE_URL.ts

This file was deleted.

0 comments on commit 642cd10

Please sign in to comment.