1.2.7 • Published 3 months ago

pokeapi-js-wrapper v1.2.7

Weekly downloads
57
License
MPL-2.0
Repository
github
Last release
3 months ago

pokeapi-js-wrapper

npm Tests Mocha browser tests codecov

Maintainer: Naramsim

A PokeAPI wrapper intended for browsers only. Comes fully asynchronous (with localForage) and built-in cache. Offers also Image Caching through the inclusion of a Service Worker. For a Node (server-side) wrapper see: pokedex-promise-v2

Install

npm install pokeapi-js-wrapper --save
<script src="https://unpkg.com/pokeapi-js-wrapper/dist/index.js"></script>

Usage

const Pokedex = require("pokeapi-js-wrapper")
const P = new Pokedex.Pokedex()
<script>
  const P = new Pokedex.Pokedex()
</script>

Example requests

// with await, be sure to be in an async function (and in a try/catch)
(async () => {
  const golduck = await P.getPokemonByName("golduck")
  console.log(golduck)
})()

// or with Promises
P.getPokemonByName("eevee")
  .then(function(response) {
    console.log(response)
  })

P.resource([
  "/api/v2/pokemon/36",
  "api/v2/berry/8",
  "https://pokeapi.co/api/v2/ability/9/",
]).then( response => {
  console.log(response)
})

Configuration

Pass an Object to Pokedex() in order to configure it. Available options: protocol, hostName, versionPath, cache, timeout(ms), and cacheImages. Any option is optional :smile:. All the default values can be found here

const Pokedex = require("pokeapi-js-wrapper")
const customOptions = {
  protocol: "https",
  hostName: "localhost:443",
  versionPath: "/api/v2/",
  cache: true,
  timeout: 5 * 1000, // 5s
  cacheImages: true
}
const P = new Pokedex.Pokedex(customOptions)

Caching images

Pokeapi.co serves its Pokemon images through Github. For example, the front default DreamWorld image of Pikachu is found at this URL: https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/dream-world/25.svg.

pokeapi-js-wrapper enables browsers to cache all these images by:

  1. enabling the config parameter cacheImages
  2. serving our service worker from the root of your project

In this way when pokeapi-js-wrapper's Pokedex is created it will install and start the Service Worker you are serving at the root of your server. The Service Worker will intercept all the calls your HTML/CSS/JS are making to get PokeAPI's images and will cache them.

It's fundamental that you download the Service Worker we provide(Right Click + Save As) and you serve it from the root of your project/server. Service Workers in fact cannot be installed from a domain different than yours.

A basic example is hosted here.

Tests

pokeapi-js-wrapper can be tested using two strategies. One is with Node, since this package works with Node (although not recommended), and the other with a browser.

npm test

Or open /test/test.html in your browser. A live version can be found at gh-pages

Endpoints

All the endpoints and the functions to access PokeAPI's endpoints are listed in the long table below. Each function .ByName(name) accepts names and ids. The only 5 functions .ById(id) only accept ids. You can also pass an array to each function, it will retrieve the data for each element asynchronously.

P.getPokemonByName("eevee").then(function(response) {
  console.log(response)
})

P.getPokemonSpeciesByName(25).then(function(response) {
  console.log(response)
})

P.getBerryByName(["cheri", "chesto", 5]).then(function(response) {
  // `response` will be an Array containing 3 Objects
  // response.forEach((item) => {console.log(item.size)}) // 80,50,20
  console.log(response)
})

P.getMachineById(3).then(function(response) {
  console.log(response)
})
FunctionMapped PokeAPI endpointDocumentation
getBerryByName(name)/berry/:nameDocumentation
getBerryFirmnessByName(name)/berry-firmness/:nameDocumentation
getBerryFlavorByName(name)/berry-flavor/:nameDocumentation
getContestTypeByName(name)/contest-type/:nameDocumentation
getContestEffectById(id)/contest-effect/:idDocumentation
getSuperContestEffectById(id)/super-contest-effect/:idDocumentation
getEncounterMethodByName(name)/encounter-method/:nameDocumentation
getEncounterConditionByName(name)/encounter-condition/:nameDocumentation
getEncounterConditionValueByName(name)/encounter-condition-value/:nam/Documentation
getEvolutionChainById(id)/evolution-chain/:idDocumentation
getEvolutionTriggerByName(name)/evolution-trigger/:nameDocumentation
getGenerationByName(name)/generation/:nameDocumentation
getPokedexByName(name)/pokedex/:nameDocumentation
getVersionByName(name)/version/:nameDocumentation
getVersionGroupByName(name)/version-group/:nameDocumentation
getItemByName(name)/item/:nameDocumentation
getItemAttributeByName(name)/item-attribute/:nameDocumentation
getItemCategoryByName(name)/item-category/:nameDocumentation
getItemFlingEffectByName(name)/item-fling-effect/:nameDocumentation
getItemPocketByName(name)/item-pocket/:nameDocumentation
getMachineById(id)/machine/:idDocumentation
getMoveByName(name)/move/:nameDocumentation
getMoveAilmentByName(name)/move-ailment/:nameDocumentation
getMoveBattleStyleByName(name)/move-battle-style/:nameDocumentation
getMoveCategoryByName(name)/move-category/:nameDocumentation
getMoveDamageClassByName(name)/move-damage-class/:nameDocumentation
getMoveLearnMethodByName(name)/move-learn-method/:nameDocumentation
getMoveTargetByName(name)/move-target/:nameDocumentation
getLocationByName(name)/location/:nameDocumentation
getLocationAreaByName(name)/location-area/:nameDocumentation
getPalParkAreaByName(name)/pal-park-area/:nameDocumentation
getRegionByName(name)/region/:nameDocumentation
getAbilityByName(name)/ability/:nameDocumentation
getCharacteristicById(id)/characteristic/:idDocumentation
getEggGroupByName(name)/egg-group/:nameDocumentation
getGenderByName(name)/gender/:nameDocumentation
getGrowthRateByName(name)/growth-rate/:nameDocumentation
getNatureByName(name)/nature/:nameDocumentation
getPokeathlonStatByName(name)/pokeathlon-stat/:nameDocumentation
getPokemonByName(name)/pokemon/:nameDocumentation
getPokemonEncounterAreasByName(name)/pokemon/:name/encountersDocumentation
getPokemonColorByName(name)/pokemon-color/:nameDocumentation
getPokemonFormByName(name)/pokemon-form/:nameDocumentation
getPokemonHabitatByName(name)/pokemon-habitat/:nameDocumentation
getPokemonShapeByName(name)/pokemon-shape/:nameDocumentation
getPokemonSpeciesByName(name)/pokemon-species/:nameDocumentation
getStatByName(name)/stat/:nameDocumentation
getTypeByName(name)/type/:nameDocumentation
getLanguageByName(name)/language/:nameDocumentation

Root Endpoints list

For each PokeAPI's root endpoint we provide a method to get all the items served by that endpoint. By default the method will return every item in the endpoint. If needed an offset and a limit can be configured.

  • offset is where to start. The first item that you will get. Default 0
  • limit is how many items you want to list. Default 100000

TIP: Do not pass any config Object to your call, since you will get every item and everything will be cached

The following snippet will get the list of Pokémon between ID 34 and ID 44

const interval = {
  offset: 34,
  limit: 10,
}
P.getPokemonsList(interval).then(function(response) {
  console.log(response)
})
FunctionMapped PokeAPI endpoint
getEndpointsList()/
getBerriesList()/berry
getBerriesFirmnesssList()/berry-firmness
getBerriesFlavorsList()/berry-flavor
getContestTypesList()/contest-type
getContestEffectsList()/contest-effect
getSuperContestEffectsList()/super-contest-effect
getEncounterMethodsList()/encounter-method
getEncounterConditionsList()/encounter-condition
getEncounterConditionValuesList()/encounter-condition-value
getEvolutionChainsList()/evolution-chain
getEvolutionTriggersList()/evolution-trigger
getGenerationsList()/generation
getPokedexsList()/pokedex
getVersionsList()/version
getVersionGroupsList()/version-group
getItemsList()/item
getItemAttributesList()/item-attribute
getItemCategoriesList()/item-category
getItemFlingEffectsList()/item-fling-effect
getItemPocketsList()/item-pocket
getMachinesList()/machine
getMovesList()/move
getMoveAilmentsList()/move-ailment
getMoveBattleStylesList()/move-battle-style
getMoveCategoriesList()/move-category
getMoveDamageClassesList()/move-damage-class
getMoveLearnMethodsList()/move-learn-method
getMoveTargetsList()/move-target
getLocationsList()/location
getLocationAreasList()/location-area
getPalParkAreasList()/pal-park-area
getRegionsList()/region
getAbilitiesList()/ability
getCharacteristicsList()/characteristic
getEggGroupsList()/egg-group
getGendersList()/gender
getGrowthRatesList()/growth-rate
getNaturesList()/nature
getPokeathlonStatsList()/pokeathlon-stat
getPokemonsList()/pokemon
getPokemonColorsList()/pokemon-color
getPokemonFormsList()/pokemon-form
getPokemonHabitatsList()/pokemon-habitat
getPokemonShapesList()/pokemon-shape
getPokemonSpeciesList()/pokemon-species
getStatsList()/stat
getTypesList()/type
getLanguagesList()/language

Custom URLs and paths

Use .resource() to query any URL or path. Also this function accepts both single values and Arrays.

P.resource([
  "/api/v2/pokemon/36",
  "api/v2/berry/8",
  "https://pokeapi.co/api/v2/ability/9/",
]).then(function(response) {
  console.log(response)
})

P.resource("api/v2/berry/5").then(function(response) {
  console.log(response)
})

Internet Explorer 8

In order to target this browser you must load a Promise polyfill before pokeapi-js-wrapper. You can choose one of your choice, we recommend jakearchibald/es6-promise or stefanpenner/es6-promise

1.2.7

3 months ago

1.2.6

4 months ago

1.2.5

4 months ago

1.2.4

9 months ago

1.2.3

1 year ago

1.2.2

3 years ago

1.2.0

4 years ago

1.2.0-beta.0

4 years ago

1.1.2

6 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.2

8 years ago

1.0.1

8 years ago

0.0.0

8 years ago