PokeAPI / pokedex-promise-v2

An easy way to use pokeapi v2 with promises in node.js
MIT License
516 stars 91 forks source link

Unable to get evolutionary chain without id? #32

Closed ghost closed 6 years ago

ghost commented 6 years ago

The documentation in the readme is far from being useful.

Ive spent around an hour trying to figure out how to access a pokemon's evolutionary chain without knowing its specific id and have yet to find a single way to do so.

Is it even possible to do this? the pokeapi documentation states that the species object should contain the evolution chain, but console logging the species object for a pokemon like pikachu gives me nothing but a name and a url, which are both useless if I'm wanting the evolutionary chain since there seems to be now way to get it without an id.

tdmalone commented 6 years ago

I think you will need to call the pokemon-species to get the evolution chain ID, and then call the evolution chain URL afterwards - so, you'll need two calls.

It might be possible to guess the evolution chain ID as I'm fairly sure it goes in order (i.e. 1 is Bulbasaur, 2 is Charmander etc.), but it's probably not a good idea to rely on that order being maintained across generations.

ghost commented 6 years ago

so im supposed to make a list of all the evolutionary charts and make my own methods and other things if I want to just simply get an evolutionary list without an id? amazing

ghost commented 6 years ago

Id have figured that it would be a priority to make it easy to get an evolutionary chain considering its one of the most sought-after parts of a pokemon database api

tdmalone commented 6 years ago

Well, you can't get a resource without knowing it's ID. What's wrong with requesting the pokemon-species and then getting the evolution chain from that?

Also - this project is open source, if you think a feature should be there that's not, feel free to work on it! But please don't get upset that something you want isn't already there.

christopher-caldwell commented 2 years ago

I had the same question, and came up with a small function to get the ID. The only way this would break is if another ID is inserted before the evolution ID https://pokeapi.co/api/v2/some-id/123/evolution-chain/112/. This would flag 123instead of 112

const getEvolutionIdFromUrl = (url: string): number => {
  const startIndex = url.search(/\/[0-9]+\//)
  return parseInt(url.slice(startIndex).replace(/\//g, ''))
}

describe('Extracting the Evolution ID from the URL', () => {
  test('Single digit ID', () => {
    const url = 'https://pokeapi.co/api/v2/evolution-chain/1/'
    const id = getEvolutionIdFromUrl(url)
    expect(id).toBe(1)
  })
  test('Two digit ID', () => {
    const url = 'https://pokeapi.co/api/v2/evolution-chain/11/'
    const id = getEvolutionIdFromUrl(url)
    expect(id).toBe(11)
  })
  test('Three digit ID', () => {
    const url = 'https://pokeapi.co/api/v2/evolution-chain/112/'
    const id = getEvolutionIdFromUrl(url)
    expect(id).toBe(112)
  })
  test('V1: Three digit ID', () => {
    const url = 'https://pokeapi.co/api/v1/evolution-chain/112/'
    const id = getEvolutionIdFromUrl(url)
    expect(id).toBe(112)
  })
  test('Random insertions ( URL changes form )', () => {
    const url = 'https://pokeapi.co/api/v2/something-else-added/evolution-chain/112/'
    const id = getEvolutionIdFromUrl(url)
    expect(id).toBe(112)
  })
})