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

Commit

Permalink
refactor: remove the "shop" property from getGamesAmerica
Browse files Browse the repository at this point in the history
The "shop" property, a property that could be supplied to filter by certain shops, was extremely
unreliable at the best of times, hard to document and describe, and most people who opened issues on
this property always wanted all results anyway so this release removes the property entirely which
means that now all data the API can give is always given back.

BREAKING CHANGE: getGamesAmerica's options no longer takes a "shop" property

re #124
  • Loading branch information
favna committed Feb 4, 2020
1 parent e33cf2e commit 15a8a3c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 47 deletions.
6 changes: 3 additions & 3 deletions __tests__/getGamesAmerica.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import defaultGetGamesAmerica, { getGamesAmerica } from '../src';

describe('getGamesAmerica', () => {
test('should allow custom limit', async () => {
const data = await getGamesAmerica({shop: 'ncom', limit: 1});
const data = await getGamesAmerica({ limit: 1});
expect(data).toBeInstanceOf(Object);
expect(data).toHaveLength(1);
});

test('should allow unfiltered shop', async () => {
test('should allow no options', async () => {
jest.setTimeout(60000);
const data = await getGamesAmerica({shop: 'unfiltered'});
const data = await getGamesAmerica();

expect(data).toBeInstanceOf(Object);
expect(data.length).toBeGreaterThanOrEqual(1500);
Expand Down
7 changes: 0 additions & 7 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,6 @@ export interface RequestOptions {
limit?: number;
}

export interface USRequestOptions extends RequestOptions {
/**
* Either `'retail'`, `'ncom'` or `'all'`.
* @default ncom
*/
shop?: 'retail' | 'ncom' | 'all' | 'unfiltered';
}
export interface EURequestOptions extends RequestOptions {
/** Game information locale. (EU Only) */
locale?: string;
Expand Down
38 changes: 1 addition & 37 deletions src/nintendo-switch-eshop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ const arrayRemoveDuplicates = (array: any[], property: string) => {
*/
const hasProp = <O extends {}>(obj: O, prop: keyof O) => obj && prop in obj;

/**
* TypeGuard to check if the variable is an array of strings
*
* @param array Array to check
* @returns Boolean representing whether the input is an array
* @private
*/
const isStringArray = (array: string | string[]): array is string[] => {
return Array.isArray(array);
};

/**
* Fetches all games on american eshops
*
Expand All @@ -54,31 +43,10 @@ const isStringArray = (array: string | string[]): array is string[] => {
* @param games _(Optional)_ Array of games to filter by
* @returns Promise containing all the games
*/
export const getGamesAmerica = async (options: interfaces.USRequestOptions = {}, offset = 0, games: interfaces.GameUS[] = []): Promise<interfaces.GameUS[]> => {
export const getGamesAmerica = async (options: interfaces.RequestOptions = {}, offset = 0, games: interfaces.GameUS[] = []): Promise<interfaces.GameUS[]> => {
const limit = hasProp(options, 'limit') ? options.limit : constants.US_GAME_LIST_LIMIT;
const shopProp = hasProp(options, 'shop') ? options.shop : 'ncom';
let shop = shopProp === 'all' ? [ 'ncom', 'retail' ] : shopProp;
shop = shop === 'unfiltered' ? undefined : shop;

const page = Math.floor(offset / (limit as number));

const shopMapper = (shopType: 'ncom' | 'retail' | string) => {
switch (shopType) {
case 'ncom':
return 'filterShops:On Nintendo.com';
case 'retail':
return 'filterShops:On retail';
default:
return '';
}
};

let shopFilters: ReturnType<typeof shopMapper> | (ReturnType<typeof shopMapper>)[] = [ '' ];

if (shop) {
shopFilters = isStringArray(shop) ? shop.map(value => shopMapper(value)) : shopMapper(shop);
}

const sortingOptions = {
direction: constants.US_GET_GAMES_OPTIONS.direction,
sortBy: constants.US_GET_GAMES_OPTIONS.sort,
Expand All @@ -93,7 +61,6 @@ export const getGamesAmerica = async (options: interfaces.USRequestOptions = {},
params: stringify({
facetFilters: [
[ constants.US_GET_GAMES_OPTIONS.system ],
shopFilters
],
hitsPerPage: limit,
page,
Expand Down Expand Up @@ -134,7 +101,6 @@ export const getGamesAmerica = async (options: interfaces.USRequestOptions = {},
params: stringify({
facetFilters: [
[ constants.US_GET_GAMES_OPTIONS.system ],
shopFilters
],
facets: [
'categories'
Expand All @@ -161,7 +127,6 @@ export const getGamesAmerica = async (options: interfaces.USRequestOptions = {},
facetFilters: JSON.stringify([
[ constants.US_GET_GAMES_OPTIONS.system ],
[ `categories:${category}` ],
shopFilters
]),
hitsPerPage: 100,
}),
Expand All @@ -174,7 +139,6 @@ export const getGamesAmerica = async (options: interfaces.USRequestOptions = {},
[ constants.US_GET_GAMES_OPTIONS.system ],
[ `categories:${category}` ],
[ `priceRange:${priceRange}` ],
shopFilters
]),
facets: [
'platform',
Expand Down

0 comments on commit 15a8a3c

Please sign in to comment.