49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
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<IPokemonJSON>) => {
|
|
return Promise.resolve(this.serializePokemonList(response));
|
|
});
|
|
}
|
|
|
|
private serializePokemonList(jsonPokemonList : Array<IPokemonJSON>) : Array<IPokemon> {
|
|
const pokemonList = jsonPokemonList.reduce((result : Array<IPokemon>, 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;
|
|
}
|
|
}
|