pokeapi()
npm install -- --version
The pokeapi()
function is syntatic sugar for the PokeApi
class. The function returns an instance of the class and allows chaining methods for fetching data in a procedural fashion. Both function and class shares the same methods and properties. The main diference is how they are invoked.
Methods
get()
Fetches a single record of given PokéApi resource endpoint.
Signature
async get<T>(idOrName: number | string) => Promise<PokeApiResource<T>>
Parameter | Type | Required | Description |
---|---|---|---|
idOrName | number or string | Yes | The desired id or name for given PokéApi resource. If PokéApi resource is in type |
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>>
Parameter | Type | Required | Description |
---|---|---|---|
id | number | Yes | The 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>
Parameter | Type | Required | Description |
---|---|---|---|
name | string | Yes | The 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>>
Parameter | Type | Required | Description |
---|---|---|---|
params | Partial<QueryParams> , void , string , or number | Yes / No | If resource is in type |
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>
Parameter | Type | Required | Description |
---|---|---|---|
params | Partial<QueryParams> | No | Optional 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'
Since T
extends a key of PokeApiResourceKeys
, we can guarantee the pokeapi()
function is valid and has the correct endpoint response interface associated.
QueryParams
Query parameters for retrieving PokéAPI paginated resources.
Type |
---|
Object |
Properties
Name | Required | Type |
---|---|---|
offset | No | number |
limit | No | number |