Skip to content

Commit

Permalink
feat: add getQueriedGamesAmerica
Browse files Browse the repository at this point in the history
You can use this to get games based on a query

BREAKING CHANGE: use getQueriedGamesAmerica to get games based on a given query. For example when
your users can perform a search (just like on the nintendo.com website) you can use this to severly
limit the results to go through.
  • Loading branch information
favna committed Oct 31, 2020
1 parent 59ead46 commit bfc1830
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
28 changes: 28 additions & 0 deletions __tests__/getQueriedGamesAmerica.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getQueriedGamesAmerica } from '../src';

describe('getQueriedGamesAmerica', () => {
test('GIVEN Zelda THEN returns at least XXX results', async () => {
const data = await getQueriedGamesAmerica('Zelda');

expect(data).toBeInstanceOf(Object);
expect(data.length).toBeGreaterThanOrEqual(20);

// Expect Link's Awakening to be in the data
expect(data).toEqual(
expect.arrayContaining([
expect.objectContaining({
title: "The Legend of Zelda: Link's Awakening"
})
])
);

// Expect Breath of the Wild to be in the data
expect(data).toEqual(
expect.arrayContaining([
expect.objectContaining({
title: 'The Legend of Zelda: Breath of the Wild'
})
])
);
});
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { getGamesAmerica as default, getGamesAmerica } from './lib/getGames/getGamesAmerica';
export { getGamesEurope } from './lib/getGames/getGamesEurope';
export { getGamesJapan } from './lib/getGames/getGamesJapan';
export { getQueriedGamesAmerica } from './lib/getGames/getQueriedGamesAmerica';
export { getActiveShops } from './lib/getShops/getActiveShops';
export { getShopsAmerica } from './lib/getShops/getShopsAmerica';
export { getShopsAsia } from './lib/getShops/getShopsAsia';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/getGames/getGamesAmerica.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const getGamesAmerica = async (): Promise<GameUS[]> => {
try {
const allGamesResponse = await fetch(US_GET_GAMES_URL, body);
if (!allGamesResponse.ok) throw new Error('US_games_request_failed');
const gamesResponse: AlgoliaResponse = await allGamesResponse.json();
const gamesResponse: AlgoliaResponse<GameUS> = await allGamesResponse.json();

let allGames: any[] | PromiseLike<GameUS[]> = [];
for (const results of gamesResponse.results) {
Expand Down
45 changes: 45 additions & 0 deletions src/lib/getGames/getQueriedGamesAmerica.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import fetch from 'node-fetch';
import { stringify } from 'querystring';
import { US_ALGOLIA_HEADERS, US_GET_GAMES_URL } from '../utils/constants';
import type { AlgoliaResponse, QueriedGameUS } from '../utils/interfaces';
import { EshopError } from '../utils/utils';

const QUERY_NINTENDO_KEY = '9a20c93440cf63cf1a7008d75f7438bf';

/**
* Fetches a subset of games from the American e-shops as based on a given query
* @param query The query to search for
* @returns Promise containing the first 200 games that match your query
* @license Apache-2.0 Favna & Antonio Román
* @copyright 2019
*/
export const getQueriedGamesAmerica = async (query: string): Promise<QueriedGameUS[]> => {
const response = await fetch(US_GET_GAMES_URL, {
method: 'POST',
headers: {
...US_ALGOLIA_HEADERS,
'X-Algolia-API-Key': QUERY_NINTENDO_KEY
},
body: JSON.stringify({
requests: [
{
indexName: 'noa_aem_game_en_us',
params: stringify({
facetFilters: ['type:game'],
hitsPerPage: 200,
page: 0,
query
})
}
]
})
});

if (!response.ok) throw new EshopError(`Fetching games for the query "${query} failed"`);

const { results }: AlgoliaResponse<QueriedGameUS> = await response.json();

if (!results.length) throw new EshopError(`No game results for the query "${query}"`);

return results[0].hits;
};
34 changes: 34 additions & 0 deletions src/lib/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,40 @@ export interface GameEU {
title_extras_txt: string[];
}

export interface QueriedGameUS {
type: string;
locale: string;
url: string;
title: string;
description: string;
lastModified: number;
id: string;
nsuid: string;
slug: string;
boxArt: string;
gallery: string;
platform: string;
releaseDateMask: string;
characters: string[];
categories: string[];
msrp: number | null;
esrb?: string;
esrbDescriptors?: string[];
virtualConsole: string;
generalFilters: string[];
filterShops: string[];
filterPlayers: string[];
publishers: string[];
developers: string[];
players: string;
featured: boolean;
freeToStart: boolean;
priceRange: string | null;
salePrice: number | null;
availability: string[];
objectID: string;
}

export interface GameUS {
/** @deprecated Product code. Can be parsed for a region wide code. */
game_code?: string;
Expand Down

0 comments on commit bfc1830

Please sign in to comment.