Pokewrap
A JavaScript wrapper for the Pokemon API
Because Snorlax ain't the only RESTful thing in the Pokemon Storage System!
Table of Contents
Features
- Clean, flexible interface to the RESTful Pokemon API
- Supports promises and callbacks
- Works in both node and the browser
Installation & Setup
Download the files:
$ npm install --save pokewrap
Then include it in your source:
// ES6+;const pokewrap = ;
// CommonJSconst Pokewrap = default;const pokewrap = ;
// Browservar pokewrap = ;
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.
Examples
Basic Usage
// What can Magikarp do, anyway?pokewrap ;
'bounce' 'splash' 'tackle' 'flail'
Fetch multiple resources in a single call
// Uno, dos, tres!pokewrap ; { return pokemon;}
name: 'articuno' height: 17 weight: 554 name: 'zapdos' height: 16 weight: 526 name: 'moltres' height: 20 weight: 600
Fetch any type of resource from the Pokemon API
// How big is a pinap berry?pokewrap ;
A pinap berry is 80mm in size.
Support for callbacks
// Which pokemon loves asynchronous programming?pokewrap;);
'dialga'
API
Pokewrap()
Signature
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()
Signature
getOne(type, id, ?opts, ?callback) => Promise<Resource>
type: ResourceType
id: NameOrId
opts: ?Object
callback: ?Function
getOne(opts, ?callback) => Promise<Resource>
opts: Object
callback: ?Function
Examples
// These calls are identicalpokewrap;pokewrap;pokewrap;
// Pass options to the Request objectpokewrap;
// With a callbackpokewrap;
// With callback and optionspokewrap;
getOneById()
Signature
getOneById(id, ?opts, ?callback) => Promise<Resource>
id: NameOrId
opts: ?Object
callback: ?Function
Note: You can also call this method using
getOneByID
orgetOneByName
.
Examples
pokewrap;pokewrap;pokewrap;
// Using a different default typeconst pokewrap = defaultType: 'berry' ; // Fetches a cheri berry, not bulbasaurpokewrap;
// Pass options to the Request objectpokewrap;
// With a callbackpokewrap;
// With callback and optionspokewrap;
getOneByName()
This is an alias for getOneById()
, for when it seems strange to fetch a
pokemon by name
using a method called getOneById
.
getMany()
Signature
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
Examples
// Fetch a paginated list of resources in the given typepokewrap;pokewrap;
// Fetch an array of cherri, sitrus, and pinap berriespokewrap;
// Fetch multiple individual resources using the default typepokewrap;
// Using a different default typeconst pokewrap = defaultType: 'berry' ; // Fetches a cheri berry, not bulbasaurpokewrap;
// Mix & Match IDs, names, and objects in the requestconst bulbasaur = 1;const ivysaur = 'ivysaur'const venusaur = name: 'venusaur' ;const solarBeam = type: 'move' name: 'solar-beam' ; pokewrap;
// Pass options to the Request objectpokewrap; pokewrap;
// With a callbackpokewrap; pokewrap;
// With callback and optionspokewrap;
getByUrl()
Signature
getByUrl(url, ?opts, ?callback)
url: String,
?opts: Object,
?callback: Function
Note: You can also call this method using
getByURL
.
Examples
Contributing
Building
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/ |
Testing
For tests with pretty output via faucet
, run:
$ npm test
For raw TAP-formatted output, run:
$ npm run test:tap
Other Great Wrappers
pokedex-promise
- A lightweight Pokemon API wrapper for node
License
Pokewrap is licensed under the ISC License.
For details, please see the LICENSE
file.