Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNG-15 Add API urls, start tests and function for pokemons.js #10

Merged
merged 10 commits into from
Jan 3, 2021
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
POKEMON_API_BASE_URL =
POKEMON_API_BASE_URL = "https://pokeapi.co/api/v2"
QUIZ_MAX_TIME_SECONDS = 120
50 changes: 50 additions & 0 deletions src/api/pokemon.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
getPokemonById,
getTypeById
} from "./pokemons.js";

describe('Test pokemon API to get pokemon', () => {

it("Given pokemon id is 1 when asking for data, should get id, photoUrl, types, name of the pokemon", async () => {
//given
const pokemonId = 1;

//when
const pokeData = await getPokemonById(pokemonId)

//then
expect(pokeData).toEqual({
id: 1,
name: "bulbasaur",
photoUrl: "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/1.png",
types: [{
id: 12,
type: "grass"
},
{
id: 4,
type: "poison"
}
]
});
})
});


describe("Test pokemon API to get pokemon types", () => {

it("Given the type id is 12 when asking for pokemon data, should return id and name of the type", async () => {
//given
const typeId = 12;

//when
const typeData = await getTypeById(typeId)

//then
expect(typeData).toEqual({
id: 12,
name: "grass"
}
);
})
})
40 changes: 40 additions & 0 deletions src/api/pokemons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fetch from "cross-fetch"

const POKEMON_API_BASE_URL = process.env.POKEMON_API_BASE_URL || "https://pokeapi.co/api/v2";

export async function getPokemonById(id) {
const getTypeIdFromUrl = (url) => {
const regex = /\/type\/(\d+)\/$/;
return Number(regex.exec(url)[1])
};
Comment on lines +6 to +9
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't aware of this complexity in code, but I am glad you managed to overcome it, cool!

However you should know that extracting this typeId from url sounds like a bad code example for me. We shouldn't do things like that, but I am glad it works and it even saves two requests to pokeapi, which is a good thing :)

But! There is no need to change that.


const parseType = (type) => {
return {
id: getTypeIdFromUrl(type.type.url),
type: type.type.name
}
};

const res = await fetch(`${POKEMON_API_BASE_URL}/pokemon/${id}`);
const jsonRes = await res.json();

return {
id: jsonRes.id,
name: jsonRes.name,
types: jsonRes.types.map(parseType),
photoUrl: jsonRes.sprites.other["official-artwork"].front_default
}
};

export async function getTypeById(id) {
const res = await fetch(`${POKEMON_API_BASE_URL}/type/${id}`);
const jsonRes = await res.json();
const {
id: typeId,
name: typeName
} = jsonRes;
return {
id: typeId,
name: typeName
}
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'regenerator-runtime/runtime' //async/await with Parcel
import {App} from "./app/App";

const ONE_SECOND_MILLIS = 1000;
const POKEMON_API_BASE_URL = process.env.POKEMON_API_BASE_URL || "https://swapi.dev/api";
const POKEMON_API_BASE_URL = process.env.POKEMON_API_BASE_URL || "https://pokeapi.co/api/v2";
const QUIZ_MAX_TIME = process.env.QUIZ_MAX_TIME_SECONDS ? process.env.QUIZ_MAX_TIME_SECONDS * ONE_SECOND_MILLIS : 120 * ONE_SECOND_MILLIS;

window.onload = () => App({options: {pokemonApiBaseUrl: POKEMON_API_BASE_URL, quizMaxTime: QUIZ_MAX_TIME}})