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('/api/billing/plan', 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 'pokemon missing name'; } if (typeof pokemonJson.id !== 'string') { throw 'pokemon missing id'; } if (typeof pokemonJson.family !== 'string') { throw 'pokemon missing family'; } if (typeof pokemonJson.dex !== 'number') { throw 'pokemon missing dex'; } if (typeof pokemonJson.stats !== 'object') { throw 'pokemon missing stats'; } const pokemon : IPokemon = { ...pokemonJson }; result.push(pokemon); } catch (e) { console.error(pokemonJson, e.message); } return result; }, []); return pokemonList; } }