Skip to content
This repository has been archived by the owner on Nov 6, 2021. It is now read-only.

Commit

Permalink
fix(getGamesAmerica): improve requests being made for better api data
Browse files Browse the repository at this point in the history
fixes #473
  • Loading branch information
favna committed Jun 20, 2021
1 parent 40c0012 commit 881174d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 158 deletions.
204 changes: 100 additions & 104 deletions src/lib/getGames/getGamesAmerica.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import fetch from 'node-fetch';
import { stringify } from 'querystring';
import {
US_ALGOLIA_HEADERS,
US_AVAILABILITY_FILTER,
US_ESRB_RATINGS_FILTERS,
US_FACETS,
US_GAME_LIST_LIMIT,
US_GET_GAMES_URL,
US_INDEX_TITLE_ASC,
US_INDEX_TITLE_DES,
US_PLATFORM_FACET_FILTER
} from '../utils/constants';
import { US_ALGOLIA_HEADERS, US_GET_GAMES_URL } from '../utils/constants';
import type { AlgoliaResponse, GameUS } from '../utils/interfaces';
import { arrayRemoveDuplicates, EshopError } from '../utils/utils';

Expand All @@ -23,108 +13,66 @@ import { arrayRemoveDuplicates, EshopError } from '../utils/utils';
* @returns Promise containing all the games
*/
export const getGamesAmerica = async (): Promise<GameUS[]> => {
const limit = US_GAME_LIST_LIMIT;
const page = 0;

const body = {
const baseParameters: Omit<ParamsObject, 'facetFilters'> = {
hitsPerPage: US_GAME_LIST_LIMIT,
page,
analytics: false,
facets: US_FACETS
};

const requests: Request[] = [];

for (const rating of US_ESRB_RATINGS_FILTERS) {
requests.push(
{
indexName: US_INDEX_TITLE_ASC,
params: stringify({ ...baseParameters, facetFilters: `[["${rating}"],["${US_PLATFORM_FACET_FILTER}"]]` })
},
{
indexName: US_INDEX_TITLE_DESC,
params: stringify({ ...baseParameters, facetFilters: `[["${rating}"],["${US_PLATFORM_FACET_FILTER}"]]` })
}
);
}

for (const rating of US_AVAILABILITY_FILTER) {
requests.push(
{
indexName: US_INDEX_TITLE_ASC,
params: stringify({ ...baseParameters, facetFilters: `[["${rating}"],["${US_PLATFORM_FACET_FILTER}"]]` })
},
{
indexName: US_INDEX_TITLE_DESC,
params: stringify({ ...baseParameters, facetFilters: `[["${rating}"],["${US_PLATFORM_FACET_FILTER}"]]` })
}
);
}

for (const rating of US_COMMON_GAME_FRANCHISES) {
requests.push(
{
indexName: US_INDEX_TITLE_ASC,
params: stringify({ ...baseParameters, facetFilters: `[["${rating}"],["${US_PLATFORM_FACET_FILTER}"]]` })
},
{
indexName: US_INDEX_TITLE_DESC,
params: stringify({ ...baseParameters, facetFilters: `[["${rating}"],["${US_PLATFORM_FACET_FILTER}"]]` })
}
);
}

const requestOptions = {
body: JSON.stringify({
requests: [
{
indexName: US_INDEX_TITLE_ASC,
params: stringify({
query: '',
hitsPerPage: limit,
page,
analytics: false,
facets: US_FACETS,
facetFilters: `[["${US_ESRB_RATINGS_FILTERS.everyone}"],["${US_PLATFORM_FACET_FILTER}"]]`
})
},
{
indexName: US_INDEX_TITLE_DES,
params: stringify({
query: '',
hitsPerPage: limit,
page,
analytics: false,
facets: US_FACETS,
facetFilters: `[["${US_ESRB_RATINGS_FILTERS.everyone}"],["${US_PLATFORM_FACET_FILTER}"]]`
})
},
{
indexName: US_INDEX_TITLE_ASC,
params: stringify({
query: '',
hitsPerPage: limit,
page,
analytics: false,
facets: US_FACETS,
facetFilters: `[["${US_ESRB_RATINGS_FILTERS.everyone10}"],["${US_PLATFORM_FACET_FILTER}"]]`
})
},
{
indexName: US_INDEX_TITLE_DES,
params: stringify({
query: '',
hitsPerPage: limit,
page,
analytics: false,
facets: US_FACETS,
facetFilters: `[["${US_ESRB_RATINGS_FILTERS.everyone10}"],["${US_PLATFORM_FACET_FILTER}"]]`
})
},
{
indexName: US_INDEX_TITLE_ASC,
params: stringify({
query: '',
hitsPerPage: limit,
page,
analytics: false,
facets: US_FACETS,
facetFilters: `[["${US_ESRB_RATINGS_FILTERS.teen}"],["${US_PLATFORM_FACET_FILTER}"]]`
})
},
{
indexName: US_INDEX_TITLE_DES,
params: stringify({
query: '',
hitsPerPage: limit,
page,
analytics: false,
facets: US_FACETS,
facetFilters: `[["${US_ESRB_RATINGS_FILTERS.teen}"],["${US_PLATFORM_FACET_FILTER}"]]`
})
},
{
indexName: US_INDEX_TITLE_ASC,
params: stringify({
query: '',
hitsPerPage: limit,
page,
analytics: false,
facets: US_FACETS,
facetFilters: `[["${US_ESRB_RATINGS_FILTERS.mature}"],["${US_PLATFORM_FACET_FILTER}"]]`
})
},
{
indexName: US_INDEX_TITLE_ASC,
params: stringify({
query: '',
hitsPerPage: limit,
page,
analytics: false,
facets: US_FACETS,
facetFilters: `[["${US_AVAILABILITY_FILTER}"],["${US_PLATFORM_FACET_FILTER}"]]`
})
}
]
requests
}),
method: 'POST',
headers: US_ALGOLIA_HEADERS
};

try {
const allGamesResponse = await fetch(US_GET_GAMES_URL, body);
const allGamesResponse = await fetch(US_GET_GAMES_URL, requestOptions);
if (!allGamesResponse.ok) throw new Error('US_games_request_failed');
const gamesResponse: AlgoliaResponse = await allGamesResponse.json();

Expand All @@ -140,3 +88,51 @@ export const getGamesAmerica = async (): Promise<GameUS[]> => {
throw err;
}
};

interface Request {
indexName: string;
params: string;
}

interface ParamsObject {
hitsPerPage: number;
page: number;
analytics: boolean;
facets: string;
facetFilters: string;
}

/** @internal The maximum number of entries that Nintendo lets us get in 1 request for US games */
const US_GAME_LIST_LIMIT = 1000;

/** @internal Index names for querying all games by ascending title */
const US_INDEX_TITLE_ASC = 'ncom_game_en_us_title_asc';

/** @internal Index names for querying all games by descending title */
const US_INDEX_TITLE_DESC = 'ncom_game_en_us_title_des';

/** @internal Static query parameters for facets/filters in US Algolia calls */
const US_FACETS = JSON.stringify([
'generalFilters',
'platform',
'availability',
'genres',
'howToShop',
'virtualConsole',
'franchises',
'priceRange',
'esrbRating',
'playerFilters'
]);

/** @internal */
const US_PLATFORM_FACET_FILTER = 'platform:Nintendo Switch';

/** @internal ESRB options for querying all games in one request */
const US_ESRB_RATINGS_FILTERS = ['esrbRating:Everyone', 'esrbRating:Everyone 10+', 'esrbRating:Teen', 'esrbRating:Mature'];

/** @internal Availability filters for querying all games in one request */
const US_AVAILABILITY_FILTER = ['availability:Pre-order', 'availability:Coming soon', 'availability:Available now'];

/** @internal Common franchises for querying all games in one request */
const US_COMMON_GAME_FRANCHISES = ['franchises:Mario', 'franchises:Zelda', 'franchises:Pokémon', 'franchises:Kirby'];
56 changes: 2 additions & 54 deletions src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,67 +25,15 @@ export const US_GAME_CHECK_CODE = '70010000000185';
*/
export const US_GAME_CODE_REGEX = /HAC\w(\w{4})/;

/**
* Default limit for getting US games - Defaults to 200
* @internal
*/
export const US_GAME_LIST_LIMIT = 1000;

/**
* Index names for querying all games by both ascending and descending title
* @internal
*/
export const US_INDEX_TITLE_ASC = 'ncom_game_en_us_title_asc';

/** @internal */
export const US_INDEX_TITLE_DES = 'ncom_game_en_us_title_des';

/**
* Static query parameters for facets/filters in US Algolia calls
* @internal
*/
export const US_FACETS = JSON.stringify([
'generalFilters',
'platform',
'availability',
'genres',
'howToShop',
'virtualConsole',
'franchises',
'priceRange',
'esrbRating',
'playerFilters'
]);

/** @internal */
export const US_PLATFORM_FACET_FILTER = 'platform:Nintendo Switch';

/**
* ESRB options and Coming Soon facet filters for querying all games in one request
* @internal
*/
export const US_ESRB_RATINGS_FILTERS = {
everyone: 'esrbRating:Everyone',
everyone10: 'esrbRating:Everyone 10+',
teen: 'esrbRating:Teen',
mature: 'esrbRating:Mature'
};

/** @internal */
export const US_AVAILABILITY_FILTER = 'availability:Coming soon';

/**
* Request headers for US games
* @internal
*/
/** @internal Request headers for US games */
export const US_ALGOLIA_HEADERS = {
'Content-Type': 'application/json',
'X-Algolia-API-Key': US_ALGOLIA_KEY,
'X-Algolia-Application-Id': US_ALGOLIA_ID
};

/**
* Options used for getting EU gmaes
* Options used for getting EU games
* @internal
*/
export const EU_GET_GAMES_OPTIONS = {
Expand Down

0 comments on commit 881174d

Please sign in to comment.