Skip to content

Commit

Permalink
fix: create config and cache per-Pokedex
Browse files Browse the repository at this point in the history
  • Loading branch information
Naramsim committed Dec 10, 2020
1 parent 7f6e574 commit 36f7542
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 91 deletions.
32 changes: 0 additions & 32 deletions src/configurator.js

This file was deleted.

33 changes: 0 additions & 33 deletions src/default.js

This file was deleted.

24 changes: 11 additions & 13 deletions src/getter.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
39 changes: 26 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ 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;
};

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(`${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
Expand All @@ -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)
}
Expand All @@ -61,30 +70,34 @@ 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) {
throw new Error(error)
}
}

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
module.exports = Pokedex
36 changes: 36 additions & 0 deletions src/values.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 36f7542

Please sign in to comment.