Skip to main content

PokeApi

The PokeApi is a wrapper class that allows fetching and handling resources from PokéAPI/v2 endpoints. All the methods returns typed interfaces regarding the desired PokéAPI resource. For example, a '/pokemon' endpoint, can return one single interface Pokemon resource, or many paginated resources, according to which method was used.

Methods

get()

Fetches a single record of given PokéApi resource endpoint.

Signature

async get<T>(idOrName: number | string) => Promise<PokeApiResource<T>>
ParameterTypeRequiredDescription
idOrNamenumber or stringYes

The desired id or name for given PokéApi resource. If PokéApi resource is in type UnnamedPaginationResource), fetching data is only available via its id.

Returns

Promise with given single PokéApi resource.

Example

const abilityById = await pokeapi('ability').get(1);
const abilityByName = await pokeapi('ability').get('sturdy');
const contestEffectOnlyById = await pokeapi('contest-effect').get(1);

// TS Error: Argument of type 'string' is not assignable to parameter of type 'number'.
const contestEffectByName = await pokeapi('contest-effect').get('foo');

getbyId()

Fetches a single record of given PokéApi resource endpoint by its id.

Signature

async getById<T>(id: number) => Promise<PokeApiResource<T>>
ParameterTypeRequiredDescription
idnumberYesThe desired resource id for given PokéApi resource

Returns

Promise with given single PokéApi resource.

Example

const item = await pokeapi('item').getById(1);

getByName()

Fetches a single record of given PokéApi resource endpoint by its name (if possible).

Signature

async getByName<T>(name: string) => Promise<PokeApiResource<T> | never>
ParameterTypeRequiredDescription
namestringYesThe desired resource name for given PokéApi resource

Returns

Promise with given single PokéApi resource or never (if resource is in UnnamedPaginationResource).

Example

const pokemon = await pokeapi('pokemon').getByName('charizard');

// contestEffect will be of type `never`, since ContestEffect resources don't have names.
const contestEffect = await pokeapi('contest-effect').getByName('foo');

getAll()

Fetches a list of records of given PokéApi resource endpoint.

Signature

async getAll<T>(
params?: Partial<QueryParams> | void | string | number
) => Promise<NamedAPIResource | PokeApiResource<T>>
ParameterTypeRequiredDescription
paramsPartial<QueryParams>, void, string, or numberYes / No

If resource is in type SubCollectionResource, a number or string must be provided. Otherwise, the parameter is optinal, and can be partially completed with QueryParams or void.

Returns

Promise with NamedAPIResource or, if given PokéApi resource endpoint is a sub-collection, a single PokéApi resource.

Example

// optional param
const pokemon = await pokeapi('pokemon').getAll();

// query params
const pokemonWithQueryParams = await pokeapi('pokemon').getAll({ offset: 10, limit: 5 });

// required since PokemonLocationArea resource is a sub-collection
const pokemonLocationArea = await pokemon('pokemon-location-area').getAll(1);

count()

Fetches the total number of records for given PokéApi resource (if possible)

Signature

async count<T>() => Promise<number | never>

Returns

Promise with total number of records for given PokéApi resource, if resource is not in SubCollectionResource, otherwise never.

Example

const pokemon = await pokeapi('pokemon').count();

// returns never since PokemonLocationArea resource is a sub-collection
const pokemonLocationArea = await pokemon('pokemon-location-area').count();

paginate()

Fetches a paginated result for given PokéApi resource (if possible).

Signature

async paginate<T>(params?: Partial<QueryParams>) => Promise<PaginatedResult | never>
ParameterTypeRequiredDescription
paramsPartial<QueryParams>NoOptional parameter for setting resouce list limit and offset values

Returns

Promise with paginated result number of records for given PokéApi resource, if resource is not in SubCollectionResource, otherwise never.

Example

const pokemon = await pokeapi('pokemon').paginate();
const pokemonWithParams = await pokeapi('pokemon').paginate({ limit: 10, offset: 100 });

// returns never since PokemonLocationArea resource is a sub-collection
const pokemonLocationArea = await pokemon('pokemon-location-area').paginate();

Type Definitions

PokeApiResource<T>

A generic interface for mapping the PokéAPI endpoint to their respective response interface model.

Type
Interface

Since T can only be valid endpoints for PokéAPI, all possible values for T are described on PokeApiResourceKeys, which are:

  • 'berry'
  • 'berry-firmness'
  • 'berry-flavor'
  • 'contest-type'
  • 'contest-effect'
  • 'super-contest-effect'
  • 'encounter-method'
  • 'encounter-condition'
  • 'encounter-condition-value'
  • 'evolution-chain'
  • 'evolution-trigger'
  • 'generation'
  • 'pokedex'
  • 'version'
  • 'version-group'
  • 'item'
  • 'item-attribute'
  • 'item-category'
  • 'item-fling-effect'
  • 'item-pocket'
  • 'location'
  • 'location-area'
  • 'pal-park-area'
  • 'region'
  • 'machine'
  • 'move'
  • 'move-ailment'
  • 'move-battle-style'
  • 'move-category'
  • 'move-damage-class'
  • 'move-learn-method'
  • 'move-target'
  • 'ability'
  • 'characteristic'
  • 'egg-group'
  • 'gender'
  • 'growth-rate'
  • 'nature'
  • 'pokeathlon-stat'
  • 'pokemon'
  • 'pokemon-location-area'
  • 'pokemon-color'
  • 'pokemon-form'
  • 'pokemon-habitat'
  • 'pokemon-shape'
  • 'pokemon-species'
  • 'stat'
  • 'type'
  • 'language'
info

Since T extends a key of PokeApiResourceKeys, we can guarantee the PokeApi class instance is valid and has the correct endpoint response interface associated.

QueryParams

Query parameters for retrieving PokéAPI paginated resources.

Type
Object

Properties

NameRequiredType
offset No number
limit No number