forked from lmmfranco/nintendo-switch-eshop
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,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' | ||
}) | ||
]) | ||
); | ||
}); | ||
}); |
This file contains 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 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 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,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; | ||
}; |
This file contains 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