import { AjaxUtils } from 'api/AjaxUtils'; import { IPokemon } from 'app/models/Pokemon'; interface IPokemonJSON extends IPokemon {} interface IPokemonService {} export class PokemonService implements IPokemonService { public getPokemonList() { const queryParameters = { type: 'no touch', is_active: true, }; return AjaxUtils.ajaxGet('/dist/db/order.json', queryParameters) .then((response : Array) => { return Promise.resolve(this.serializePokemonList(response)); }); } private serializePokemonList(jsonPokemonList : Array) : Array { const pokemonList = jsonPokemonList.reduce((result : Array, pokemonJson) => { try { if (typeof pokemonJson.name !== 'string') { throw new Error('pokemon missing name'); } if (typeof pokemonJson.id !== 'string') { throw new Error('pokemon missing id'); } if (typeof pokemonJson.family !== 'string') { throw new Error('pokemon missing family'); } if (typeof pokemonJson.dex !== 'number') { throw new Error('pokemon missing dex'); } if (typeof pokemonJson.stats !== 'object') { throw new Error('pokemon missing stats'); } const pokemon : IPokemon = { ...pokemonJson }; result.push(pokemon); } catch (e) { /* tslint:disable-next-line:no-console */ console.error(pokemonJson, e.message); } return result; }, []); return pokemonList; } }