diff --git a/src/endpoints.js b/src/endpoints.js index eb8be1b..6728c76 100644 --- a/src/endpoints.js +++ b/src/endpoints.js @@ -1,50 +1,50 @@ exports.endpoints = [ - ["getBerryByName", "berry"], - ["getBerryFirmnessByName", "berry-firmness"], - ["getBerryFlavorByName", "berry-flavor"], - ["getContestTypeByName", "contest-type"], - ["getContestEffectById", "contest-effect"], - ["getSuperContestEffectById", "super-contest-effect"], - ["getEncounterMethodByName", "encounter-method"], - ["getEncounterConditionByName", "encounter-condition"], - ["getEncounterConditionValueByName", "encounter-condition-value"], - ["getEvolutionChainById", "evolution-chain"], - ["getEvolutionTriggerByName", "evolution-trigger"], - ["getGenerationByName", "generation"], - ["getPokedexByName", "pokedex"], - ["getVersionByName", "version"], - ["getVersionGroupByName", "version-group"], - ["getItemByName", "item"], - ["getItemAttributeByName", "item-attribute"], - ["getItemCategoryByName", "item-category"], - ["getItemFlingEffectByName", "item-fling-effect"], - ["getItemPocketByName", "item-pocket"], + ["getBerryByName", "berry"], + ["getBerryFirmnessByName", "berry-firmness"], + ["getBerryFlavorByName", "berry-flavor"], + ["getContestTypeByName", "contest-type"], + ["getContestEffectById", "contest-effect"], + ["getSuperContestEffectById", "super-contest-effect"], + ["getEncounterMethodByName", "encounter-method"], + ["getEncounterConditionByName", "encounter-condition"], + ["getEncounterConditionValueByName", "encounter-condition-value"], + ["getEvolutionChainById", "evolution-chain"], + ["getEvolutionTriggerByName", "evolution-trigger"], + ["getGenerationByName", "generation"], + ["getPokedexByName", "pokedex"], + ["getVersionByName", "version"], + ["getVersionGroupByName", "version-group"], + ["getItemByName", "item"], + ["getItemAttributeByName", "item-attribute"], + ["getItemCategoryByName", "item-category"], + ["getItemFlingEffectByName", "item-fling-effect"], + ["getItemPocketByName", "item-pocket"], ["getMachineById", "machine"], - ["getMoveByName", "move"], - ["getMoveAilmentByName", "move-ailment"], - ["getMoveBattleStyleByName", "move-battle-style"], - ["getMoveCategoryByName", "move-category"], - ["getMoveDamageClassByName", "move-damage-class"], - ["getMoveLearnMethodByName", "move-learn-method"], - ["getMoveTargetByName", "move-target"], - ["getLocationByName", "location"], - ["getLocationAreaByName", "location-area"], - ["getPalParkAreaByName", "pal-park-area"], - ["getRegionByName", "region"], - ["getAbilityByName", "ability"], - ["getCharacteristicById", "characteristic"], - ["getEggGroupByName", "egg-group"], - ["getGenderByName", "gender"], - ["getGrowthRateByName", "growth-rate"], - ["getNatureByName", "nature"], - ["getPokeathlonStatByName", "pokeathlon-stat"], - ["getPokemonByName", "pokemon"], - ["getPokemonColorByName", "pokemon-color"], - ["getPokemonFormByName", "pokemon-form"], - ["getPokemonHabitatByName", "pokemon-habitat"], - ["getPokemonShapeByName", "pokemon-shape"], - ["getPokemonSpeciesByName", "pokemon-species"], - ["getStatByName", "stat"], - ["getTypeByName", "type"], + ["getMoveByName", "move"], + ["getMoveAilmentByName", "move-ailment"], + ["getMoveBattleStyleByName", "move-battle-style"], + ["getMoveCategoryByName", "move-category"], + ["getMoveDamageClassByName", "move-damage-class"], + ["getMoveLearnMethodByName", "move-learn-method"], + ["getMoveTargetByName", "move-target"], + ["getLocationByName", "location"], + ["getLocationAreaByName", "location-area"], + ["getPalParkAreaByName", "pal-park-area"], + ["getRegionByName", "region"], + ["getAbilityByName", "ability"], + ["getCharacteristicById", "characteristic"], + ["getEggGroupByName", "egg-group"], + ["getGenderByName", "gender"], + ["getGrowthRateByName", "growth-rate"], + ["getNatureByName", "nature"], + ["getPokeathlonStatByName", "pokeathlon-stat"], + ["getPokemonByName", "pokemon"], + ["getPokemonColorByName", "pokemon-color"], + ["getPokemonFormByName", "pokemon-form"], + ["getPokemonHabitatByName", "pokemon-habitat"], + ["getPokemonShapeByName", "pokemon-shape"], + ["getPokemonSpeciesByName", "pokemon-species"], + ["getStatByName", "stat"], + ["getTypeByName", "type"], ["getLanguageByName", "language"] ] \ No newline at end of file diff --git a/src/error.js b/src/error.js index dd0323c..9f8ce7c 100644 --- a/src/error.js +++ b/src/error.js @@ -1,6 +1,6 @@ exports.handleError = function (error, cb) { if (cb) { - cb('Pokedex-promise-v2 error', error); + cb('Pokedex-promise-v2 error', error) } else { throw error } diff --git a/src/getter.js b/src/getter.js index a55a68f..b06f054 100644 --- a/src/getter.js +++ b/src/getter.js @@ -10,11 +10,11 @@ exports.getJSON = async (values, url, cb) => { } try { // retrive possible content from memory-cache - const cachedResult = values.cache.get(url); + const cachedResult = values.cache.get(url) if (cachedResult !== null) { if (cb) { // call callback without errors - cb(cachedResult, false); + cb(cachedResult, false) } return cachedResult } else { @@ -29,15 +29,15 @@ exports.getJSON = async (values, url, cb) => { response = response.data if (values.cacheLimit > 0) { - values.cache.put(url, response, values.cacheLimit); + values.cache.put(url, response, values.cacheLimit) } // if a callback is present if (cb) { // call it, without errors - cb(response, false); + cb(response, false) } else { - return response; + return response } } } diff --git a/src/index.js b/src/index.js index 3f2b423..e480672 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ -const pMap = require('p-map'); -const cache = require('memory-cache'); +const pMap = require('p-map') +const cache = require('memory-cache') const { endpoints } = require('./endpoints.js') const { rootEndpoints } = require('./rootEndpoints.js') @@ -18,14 +18,14 @@ class Pokedex { try { const mapper = async name => { const queryRes = await getJSON(this.values, `${this.values.protocol}${this.values.hostName}${this.values.versionPath}${endpoint[1]}/${name}/`) - return queryRes; - }; + return queryRes + } if (input) { // if the user has submitted a Name or an Id, return the Json promise if (typeof input === 'number' || typeof input === 'string') { - return getJSON(this.values, `${this.values.protocol}${this.values.hostName}${this.values.versionPath}${endpoint[1]}/${input}/`, cb); + return getJSON(this.values, `${this.values.protocol}${this.values.hostName}${this.values.versionPath}${endpoint[1]}/${input}/`, cb) } // if the user has submitted an Array @@ -34,16 +34,16 @@ class Pokedex { // fetch data asynchronously to be faster const mappedResults = await pMap(input, mapper, {concurrency: 4}) if (cb) { - cb(mappedResults); + cb(mappedResults) } - return mappedResults; + return mappedResults } } } catch (error) { handleError(error, cb) } } - }); + }) rootEndpoints.forEach(rootEndpoint => { this[rootEndpoint[0]] = async (config, cb) => { @@ -63,7 +63,7 @@ class Pokedex { handleError(error, cb) } } - }); + }) } async resource(path, cb) { @@ -72,7 +72,7 @@ class Pokedex { if (typeof path === 'string') { result = getJSON(this.values, path) } else if (typeof path === 'object') { - result = Promise.all(path.map(p => getJSON(this.values, p))); + result = Promise.all(path.map(p => getJSON(this.values, p))) } else { throw 'String or Array required' } @@ -85,19 +85,19 @@ class Pokedex { } } - getConfig = function() { + getConfig() { return this.values } cacheSize() { // Retuns the current number of entries in the cache - return this.values.cache.size(); + return this.values.cache.size() } clearCache() { // Deletes all keys in cache - this.values.cache.clear(); - }; -}; + this.values.cache.clear() + } +} module.exports = Pokedex diff --git a/src/rootEndpoints.js b/src/rootEndpoints.js index ec6477e..861196b 100644 --- a/src/rootEndpoints.js +++ b/src/rootEndpoints.js @@ -1,51 +1,51 @@ exports.rootEndpoints = [ ["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/"], + ["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/"] ] \ No newline at end of file diff --git a/src/values.js b/src/values.js index fa478d3..ef30b6d 100644 --- a/src/values.js +++ b/src/values.js @@ -1,36 +1,36 @@ class Values { constructor(config={}, cache) { - this.protocol = 'https'; - this.hostName = '://pokeapi.co'; - this.versionPath = '/api/v2/'; - this.offset = 0; - this.limit = 100000; - this.timeout = 20 * 1000; // 20 seconds - this.cacheLimit = 1000000 * 1000; // 11 days - this.cache = cache; + this.protocol = 'https' + this.hostName = '://pokeapi.co' + this.versionPath = '/api/v2/' + this.offset = 0 + this.limit = 100000 + this.timeout = 20 * 1000 // 20 seconds + this.cacheLimit = 1000000 * 1000 // 11 days + this.cache = cache if (config.hasOwnProperty("protocol")) { - this.protocol = config.protocol; + this.protocol = config.protocol } if (config.hasOwnProperty("hostName")) { - this.hostName = `://${config.hostName}`; + this.hostName = `://${config.hostName}` } if (config.hasOwnProperty("versionPath")) { - this.versionPath = config.versionPath; + this.versionPath = config.versionPath } if (config.hasOwnProperty("offset")) { - this.offset = config.offset - 1; + this.offset = config.offset - 1 } if (config.hasOwnProperty("limit")) { - this.limit = config.limit; + this.limit = config.limit } if (config.hasOwnProperty("timeout")) { - this.timeout = config.timeout; + this.timeout = config.timeout } if (config.hasOwnProperty("cacheLimit")) { - this.cacheLimit = config.cacheLimit; + this.cacheLimit = config.cacheLimit } } } -exports.Values = Values; +exports.Values = Values