From 36f7542bf15b3e8a10416593e185b163f0e68312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Thu, 10 Dec 2020 19:46:14 +0100 Subject: [PATCH] fix: create config and cache per-Pokedex --- src/configurator.js | 32 -------------------------------- src/default.js | 33 --------------------------------- src/getter.js | 24 +++++++++++------------- src/index.js | 39 ++++++++++++++++++++++++++------------- src/values.js | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 91 deletions(-) delete mode 100644 src/configurator.js delete mode 100644 src/default.js create mode 100644 src/values.js diff --git a/src/configurator.js b/src/configurator.js deleted file mode 100644 index 2823f69..0000000 --- a/src/configurator.js +++ /dev/null @@ -1,32 +0,0 @@ -const { values } = require('./default.js') - -exports.setPokedexConfiguration = config => { - if (config) { - if (config.hasOwnProperty('protocol')) { - values.setProtocol(config.protocol) - } - if (config.hasOwnProperty('hostName')) { - values.setHostName(config.hostName) - } - if (config.hasOwnProperty('versionPath')) { - values.setVersionPath(config.versionPath) - } - if (config.hasOwnProperty('timeout')) { - values.setTimeout(config.timeout) - } - if (config.hasOwnProperty('cacheLimit')) { - values.setCacheLimit(config.cacheLimit) - } - } -} - -exports.setRootEndpointConfiguration = config => { - if (config) { - if (config.offset) { - values.setOffset(config.offset) - } - if (config.limit) { - values.setLimit(config.limit) - } - } -} diff --git a/src/default.js b/src/default.js deleted file mode 100644 index fcf48fc..0000000 --- a/src/default.js +++ /dev/null @@ -1,33 +0,0 @@ -const values = {}; - -values.protocol = 'https'; -values.hostName = '://pokeapi.co'; -values.versionPath = '/api/v2/'; -values.offset = 0; -values.limit = 100000; -values.timeout = 20 * 1000; // 20 seconds -values.cacheLimit = 1000000 * 1000; // 11 days - -values.setProtocol = newProtocol => { - values.protocol = newProtocol; -} -values.setHostName = newHostName => { - values.hostName = `://${newHostName}`; -} -values.setVersionPath = newVersionPath => { - values.versionPath = newVersionPath; -} -values.setOffset = newOffset => { - values.offset = newOffset - 1; -} -values.setLimit = newLimit => { - values.limit = newLimit + 1; -} -values.setTimeout = newTimeout => { - values.timeout = newTimeout; -} -values.setCacheLimit = newCacheLimit => { - values.cacheLimit = newCacheLimit; -} - -exports.values = values diff --git a/src/getter.js b/src/getter.js index abcb04f..a55a68f 100644 --- a/src/getter.js +++ b/src/getter.js @@ -1,18 +1,16 @@ const axios = require('axios') -const cache = require('memory-cache') -const { values } = require('./default.js') const { handleError } = require('./error.js') -let options = { - baseURL: `${values.protocol}${values.hostName}/`, - timeout: values.timeout -} -exports.getJSON = async (url, cb) => { +exports.getJSON = async (values, url, cb) => { + let options = { + baseURL: `${values.protocol}${values.hostName}/`, + timeout: values.timeout + } try { - // retrive possible content from volatile memory - const cachedResult = cache.get(url); + // retrive possible content from memory-cache + const cachedResult = values.cache.get(url); if (cachedResult !== null) { if (cb) { // call callback without errors @@ -26,14 +24,14 @@ exports.getJSON = async (url, cb) => { handleError(response, cb) } else { // if everything was good - // cache the object in volatile memory + // cache the object in memory-cache // only if cacheLimit > 0 response = response.data - + if (values.cacheLimit > 0) { - cache.put(url, response, values.cacheLimit); + values.cache.put(url, response, values.cacheLimit); } - + // if a callback is present if (cb) { // call it, without errors diff --git a/src/index.js b/src/index.js index 871ecfe..3f2b423 100644 --- a/src/index.js +++ b/src/index.js @@ -4,20 +4,20 @@ const cache = require('memory-cache'); const { endpoints } = require('./endpoints.js') const { rootEndpoints } = require('./rootEndpoints.js') const { getJSON } = require('./getter.js') -const { values } = require('./default.js') -const configurator = require('./configurator.js') +const { Values } = require('./values.js') const { handleError } = require('./error.js') + class Pokedex { constructor(config) { - configurator.setPokedexConfiguration(config); + this.values = new Values(config, new cache.Cache()) // add to Pokedex.prototype all our endpoint functions endpoints.forEach(endpoint => { this[endpoint[0]] = async (input, cb) => { try { const mapper = async name => { - const queryRes = await getJSON(`${values.protocol}${values.hostName}${values.versionPath}${endpoint[1]}/${name}/`) + const queryRes = await getJSON(this.values, `${this.values.protocol}${this.values.hostName}${this.values.versionPath}${endpoint[1]}/${name}/`) return queryRes; }; @@ -25,7 +25,7 @@ class Pokedex { // if the user has submitted a Name or an Id, return the Json promise if (typeof input === 'number' || typeof input === 'string') { - return getJSON(`${values.protocol}${values.hostName}${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 @@ -48,8 +48,17 @@ class Pokedex { rootEndpoints.forEach(rootEndpoint => { this[rootEndpoint[0]] = async (config, cb) => { try { - configurator.setRootEndpointConfiguration(config); - return getJSON(`${values.protocol}${values.hostName}${values.versionPath}${rootEndpoint[1]}?limit=${values.limit}&offset=${values.offset}`, cb) + let limit = this.values.limit + let offset = this.values.offset + if (config) { + if (config.hasOwnProperty('limit')) { + limit = config.limit + } + if (config.hasOwnProperty('offset')) { + offset = config.offset + } + } + return getJSON(this.values, `${this.values.protocol}${this.values.hostName}${this.values.versionPath}${rootEndpoint[1]}?limit=${limit}&offset=${offset}`, cb) } catch (error) { handleError(error, cb) } @@ -61,14 +70,14 @@ class Pokedex { let result try { if (typeof path === 'string') { - result = getJSON(path) + result = getJSON(this.values, path) } else if (typeof path === 'object') { - result = Promise.all(path.map(p => getJSON(p))); + result = Promise.all(path.map(p => getJSON(this.values, p))); } else { throw 'String or Array required' } if (cb) { - cb(result) + cb(result) // TODO: check if this callback is called with a pending Promise or an actual result } return result } catch (error) { @@ -76,15 +85,19 @@ class Pokedex { } } + getConfig = function() { + return this.values + } + cacheSize() { // Retuns the current number of entries in the cache - return cache.size(); + return this.values.cache.size(); } clearCache() { // Deletes all keys in cache - cache.clear(); + this.values.cache.clear(); }; }; -module.exports = Pokedex \ No newline at end of file +module.exports = Pokedex diff --git a/src/values.js b/src/values.js new file mode 100644 index 0000000..fa478d3 --- /dev/null +++ b/src/values.js @@ -0,0 +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; + + if (config.hasOwnProperty("protocol")) { + this.protocol = config.protocol; + } + if (config.hasOwnProperty("hostName")) { + this.hostName = `://${config.hostName}`; + } + if (config.hasOwnProperty("versionPath")) { + this.versionPath = config.versionPath; + } + if (config.hasOwnProperty("offset")) { + this.offset = config.offset - 1; + } + if (config.hasOwnProperty("limit")) { + this.limit = config.limit; + } + if (config.hasOwnProperty("timeout")) { + this.timeout = config.timeout; + } + if (config.hasOwnProperty("cacheLimit")) { + this.cacheLimit = config.cacheLimit; + } + } +} + +exports.Values = Values;