A JavaScript wrapper for the Pokemon API
Because Snorlax ain't the only RESTful thing in the Pokemon Storage System!
- Clean, flexible interface to the RESTful Pokemon API
- Supports promises and callbacks
- Works in both node and the browser
Download the files:
$ npm install --save pokewrap
Then include it in your source:
// ES6+
import Pokewrap from 'pokewrap';
const pokewrap = new Pokewrap();
// CommonJS
const Pokewrap = require('pokewrap').default;
const pokewrap = new Pokewrap();
// Browser
var pokewrap = new Pokewrap.default();
Notes:
-
If you use the global variable or CommonJS to work with Pokewrap, you need to explicitly reference the
Pokewrap.default
object. -
Although Pokewrap supports callbacks, it is built using the
isomorphic-fetch
, which requires you to bring your ownPromise
library if your target environment doesn't support them natively. Bothes6-promise
andbluebird
are great options.
// What can Magikarp do, anyway?
pokewrap.getOneByName('magikarp')
.then((pokemon) => pokemon.moves.map(({ move }) => move.name))
.then((moves) => console.log(moves));
[ 'bounce', 'splash', 'tackle', 'flail' ]
// Uno, dos, tres!
pokewrap
.getMany(['articuno', 'zapdos', 'moltres'])
.then((pokemon) => console.log(getVitals(pokemon)))
.catch((err) => console.error(err));
function getVitals(pokemon) {
return pokemon.map(
({ name, height, weight }) => ({name, height, weight})
);
}
[ { name: 'articuno', height: 17, weight: 554 },
{ name: 'zapdos', height: 16, weight: 526 },
{ name: 'moltres', height: 20, weight: 600 } ]
// How big is a pinap berry?
pokewrap.getOne('berry', 'pinap')
.then((berry) => {
console.log(`A pinap berry is ${berry.size}mm in size.`);
});
A pinap berry is 80mm in size.
// Which pokemon loves asynchronous programming?
pokewrap.getOneById(483, (pokemon) => console.log(pokemon.name));
'dialga'
new Pokewrap(?config) => Pokewrap
config: ?Object
The configuration options and their default values are as follows:
Option | Default | Comments |
---|---|---|
baseUrl |
'http://pokeapi.co/api/v2' |
The base URL to send requests to. Changing this value can be useful for testing your own local copy of the Pokemon API |
defaultType |
'pokemon' |
The default endpoint to fetch resources from. If no type is provided to a call, then Pokewrap will fallback to using this type. |
requests |
{ redirect: 'follow' } |
Options that are passed to each Request object. See the Request documentation at MDN for the full list of options. |
getOne(type, id, ?opts, ?callback) => Promise<Resource>
type: ResourceType
id: NameOrId
opts: ?Object
callback: ?Function
getOne(opts, ?callback) => Promise<Resource>
opts: Object
callback: ?Function
// These calls are identical
pokewrap.getOne('berry', 'pinap');
pokewrap.getOne({ type: 'berry', id: 'pinap' });
pokewrap.getOne({ type: 'berry', name: 'pinap' });
// Pass options to the Request object
pokewrap.getOne('pokemon', 'magikarp', { cache: 'no-cache' });
getOneById(id, ?opts, ?callback) => Promise<Resource>
id: NameOrId
opts: ?Object
callback: ?Function
Note: You can also call this method using
getOneByID
orgetOneByName
.
pokewrap.getOneById(1);
pokewrap.getOneById('bulbasaur');
pokewrap.getOneByName('bulbasaur');
// Using a different default type
const pokewrap = new Pokewrap({ defaultType: 'berry' });
// Fetches a cheri berry, not bulbasaur
pokewrap.getOneById(1);
// Pass options to the Request object
pokewrap.getOneById('pikachu', { cache: 'no-cache' });
This is an alias for getOneById()
, for when it seems strange to fetch a
pokemon by name
using a method called getOneById
.
getMany(type, ?opts, ?callback) => Promise<Array<Resource>>
type: ResourceType
opts: ?Object
callback: ?Function
getMany(type, ids, ?opts, ?callback) => Promise<Array<Resource>>
type: ResourceType
ids: Array<NameOrId|ResourceObject>
opts: ?Object
callback: ?Function
getMany(ids, ?opts, ?callback) => Promise<Array<Resource>>
ids: Array<NameOrId|ResourceObject>
opts: ?Object
callback: ?Function
// Fetch a paginated list of resources in the given type
pokewrap.getMany('pokemon');
pokewrap.getMany('berry');
// Fetch an array of cherri, sitrus, and pinap berries
pokewrap.getMany('berry', [1, 10, 20]);
// Fetch multiple individual resources using the default type
pokewrap.getMany(['bulbasaur', 'ivysaur', 'venusaur']);
// Using a different default type
const pokewrap = new Pokewrap({ defaultType: 'berry' });
// Fetches a cheri berry, not bulbasaur
pokewrap.getMany(['bulbasaur', 'charmander', 'squirtle']);
// Mix & Match IDs, names, and objects in the request
const bulbasaur = 1;
const ivysaur = 'ivysaur'
const venusaur = { name: 'venusaur' };
const solarBeam = { type: 'move', name: 'solar-beam' };
pokewrap.getMany([bulbasaur, ivysaur, venusaur, solarBeam]);
// Pass options to the Request object
pokewrap.getMany(
'item',
[1, 2, 3],
{ cache: 'no-cache' }
);
pokewrap.getMany(
['bulbasaur', 'charmander', 'squirtle'],
{ cache: 'no-cache' }
);
// With a callback
pokewrap.getMany(
'item',
[1, 2, 3],
(items) => items.forEach((item) => console.log(item.name));
);
pokewrap.getMany(
['bulbasaur', 'charmander', 'squirtle'],
(pokemonList) => {
pokemonList.forEach((pokemon) => console.log(pokemon.id));
}
);
getByUrl(url, ?opts, ?callback)
url: String,
?opts: Object,
?callback: Function
Note: You can also call this method using
getByURL
.
pokewrap.getByUrl('http://pokewrap.co/api/v2/pokemon/farfetchd')
.then((farfetchd) => console.log(farfetched));
The following npm
scripts are available for use during development:
Command | Use to... |
---|---|
npm run build |
Transpile & bundle (no minification) |
npm run build:production |
Transpile & bundle (w/ minification) |
npm run clean |
Remove the dist/ files |
npm run compile |
Transpile src/ to dist/ |
npm run lint |
Lint the files in src/ |
For tests with pretty output via faucet
, run:
$ npm test
For raw TAP-formatted output, run:
$ npm run test:tap
pokedex-promise
- A lightweight Pokemon API wrapper for node
Pokewrap is licensed under the ISC License.
For details, please see the LICENSE
file.