Add the Pokémon client

A client is a class with methods that allow you to interact a particular API, e.g. making asynchronous requests to fetch and post records. You will import the PokéAPI's client from a library.

At the moment the code doesn't have any Pokémon information to work with, so let's set up a Pokémon client that will fetch our first Pokémon of the day!

  1. Run npm install pokedex-promise-v2 --save.

The docs for this library show how to create an instance of a Pokémon client.

  1. In src/index.ts import Pokedex from the Pokémon library, and create an instance of the Pokémon client. Do this within the run function so that your code will be able to use it when it runs.

  2. Use the getPokemonByName method from your instance of the Pokémon client to fetch a Pokémon by name .

At this point your src/index.ts should look like this:

import * as dotenv from 'dotenv'
dotenv.config()
import Pokedex from 'pokedex-promise-v2'
import { Config, Secret, Context } from './lib.js'

const run = async () => {
    const context = new Context()
    const secrets = new Secret()
    const config = new Config()

    console.error(`This run is in mode: ${context['run_mode']}`)
    console.error('------------------------CONFIG------------------------')
    console.error(config)

    console.error('------------------------CONTEXT------------------------')
    console.error(context)

    const pokeClient = new Pokedex()

    const tyranitar = await pokeClient.getPokemonByName('tyranitar')
    console.error(tyranitar)
}

run().then(
    () => {},
    () => {
        process.exitCode = 1
    }
)
  1. Run npm run build && npm run start.

You should see something like this logged - except that the printed object should include the whole tyranitar object.

> pokemon-of-the-day@1.0.0 start
> node build/src/

This run is in mode: normal
------------------------CONFIG------------------------
Config {}
------------------------CONTEXT------------------------
Context { run_mode: 'normal' }
{
  abilities: [
    { ability: [Object], is_hidden: false, slot: 1 },
    { ability: [Object], is_hidden: true, slot: 3 }
  ],
  ... further properties of the Pokémon Tyranitar
}

This shows the Pokémon client works, so you can remove the logic for fetching and logging out tyranitar.

Last updated