Skip to content

Commit c55592b

Browse files
committed
refactor: change config name
1 parent dd2631b commit c55592b

File tree

9 files changed

+95
-101
lines changed

9 files changed

+95
-101
lines changed

dist/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pokeapi-js-wrapper-sw.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ self.addEventListener('fetch', function (event) {
1111
return fetch(event.request).then(function (response) {
1212
if (event.request.url.match(imgRe)) {
1313
caches.open("pokeapi-js-wrapper-images-" + version).then(function (cache) {
14-
// The response is opaque, if it fails the cache.add() will reject it
14+
// The response is opaque, if it fails cache.add() will reject it
1515
cache.add(event.request.url)
1616
})
1717
}
1818
return response;
1919
}).catch(function (error) {
20-
console.log(error)
20+
console.error(error)
2121
})
2222
}))
2323
}

src/config.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Config {
2+
constructor(config={}) {
3+
this.protocol = 'https'
4+
this.hostName = 'pokeapi.co'
5+
this.versionPath = '/api/v2/'
6+
this.offset = 0
7+
this.limit = 100000
8+
this.timeout = 10 * 1000 // 2 seconds
9+
this.cache = true
10+
this.cacheImages = false
11+
12+
if (config.hasOwnProperty('protocol')) {
13+
this.protocol = config.protocol
14+
}
15+
if (config.hasOwnProperty('hostName')) {
16+
this.hostName = config.hostName
17+
}
18+
if (config.hasOwnProperty('versionPath')) {
19+
this.versionPath = config.versionPath
20+
}
21+
if (config.hasOwnProperty('offset')) {
22+
this.offset = config.offset - 1
23+
}
24+
if (config.hasOwnProperty('limit')) {
25+
this.limit = config.limit
26+
}
27+
if (config.hasOwnProperty('timeout')) {
28+
this.timeout = config.timeout
29+
}
30+
if (config.hasOwnProperty('cache')) {
31+
this.cache = config.cache
32+
}
33+
if (config.hasOwnProperty('cacheImages')) {
34+
this.cacheImages = config.cacheImages
35+
}
36+
}
37+
}
38+
export { Config }

src/default.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/getter.js

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,56 @@
1-
import axios from 'axios';
2-
import localForage from "localforage";
1+
import axios from 'axios'
2+
import localForage from "localforage"
33

4-
const CACHE_PREFIX = "pokeapi-js-wrapper-";
4+
const CACHE_PREFIX = "pokeapi-js-wrapper-"
55

6-
function loadResource(values, url) {
6+
function loadResource(config, url) {
77
return new Promise((resolve, reject) => {
88
localForage.ready()
99
.then(() => {
1010
localForage.getItem(`${CACHE_PREFIX}${url}`)
1111
.then(value => {
1212
if (value === null) {
13-
loadUrl(values, url).then(res => {resolve(res)})
14-
.catch(err => {reject(err)});
13+
loadUrl(config, url).then(res => {resolve(res)})
14+
.catch(err => {reject(err)})
1515
} else {
1616
resolve(addCacheMark(value))
1717
}
1818
})
1919
.catch(err => {
20-
loadUrl(values, url).then(res => {resolve(res)})
21-
.catch(err => {reject(err)});
22-
});
20+
loadUrl(config, url).then(res => {resolve(res)})
21+
.catch(err => {reject(err)})
22+
})
2323
})
2424
.catch(err => {
25-
loadUrl(values, url).then(res => {resolve(res)})
26-
.catch(err => {reject(err)});
27-
});
28-
});
29-
};
25+
loadUrl(config, url).then(res => {resolve(res)})
26+
.catch(err => {reject(err)})
27+
})
28+
})
29+
}
3030

31-
function loadUrl(values, url) {
31+
function loadUrl(config, url) {
3232
return new Promise((resolve, reject) => {
3333
let options = {
34-
baseURL: `${values.protocol}://${values.hostName}/`,
35-
timeout: values.timeout
34+
baseURL: `${config.protocol}://${config.hostName}/`,
35+
timeout: config.timeout
3636
}
3737
axios.get(url, options)
3838
.then(response => {
3939
// if there was an error
4040
if (response.status >= 400) {
41-
reject(response);
41+
reject(response)
4242
} else {
4343
// if everything was good
4444
// cache the object in browser memory
4545
// only if cache is true
46-
if (values.cache) {
47-
localForage.setItem(`${CACHE_PREFIX}${url}`, response.data);
46+
if (config.cache) {
47+
localForage.setItem(`${CACHE_PREFIX}${url}`, response.data)
4848
}
49-
resolve(addCacheMark(response.data, 0));
49+
resolve(response.data)
5050
}
5151
})
5252
.catch(err => { reject(err) })
53-
});
54-
}
55-
56-
function addCacheMark(object, cached = 1) {
57-
//object.fromCache = cached;
58-
return object;
53+
})
5954
}
6055

61-
export { loadResource };
56+
export { loadResource }

src/index.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import localForage from "localforage";
1+
import localForage from "localforage"
22

3-
import endpoints from './endpoints.json';
4-
import rootEndpoints from './rootEndpoints.json';
5-
import { loadResource } from './getter.js';
6-
import { installSW } from './installSW.js';
7-
import { Values } from './default.js';
3+
import endpoints from './endpoints.json'
4+
import rootEndpoints from './rootEndpoints.json'
5+
import { loadResource } from './getter.js'
6+
import { installSW } from './installSW.js'
7+
import { Config } from './config.js'
88

99
export class Pokedex {
1010

1111
constructor(config) {
12-
this.values = new Values(config)
12+
this.config = new Config(config)
1313
this.getConfig = function() {
14-
return this.values
14+
return this.config
1515
}
1616

1717
// add to Pokedex.prototype all our endpoint functions
@@ -21,22 +21,22 @@ export class Pokedex {
2121

2222
// if the user has submitted a Name or an ID, return the JSON promise
2323
if (typeof input === 'number' || typeof input === 'string') {
24-
return loadResource(this.values, `${this.values.versionPath}${endpoint[1]}/${input}/`);
24+
return loadResource(this.config, `${this.config.versionPath}${endpoint[1]}/${input}/`)
2525
}
2626

2727
// if the user has submitted an Array
2828
// return a new promise which will resolve when all loadResource calls are ended
2929
else if (typeof input === 'object') {
30-
return Promise.all(mapResources(this.values, endpoint, input));
30+
return Promise.all(mapResources(this.config, endpoint, input))
3131
}
3232
}
3333
}
34-
});
34+
})
3535

3636
rootEndpoints.forEach(rootEndpoint => {
3737
this[rootEndpoint[0]] = config => {
38-
var limit = this.values.limit
39-
var offset = this.values.offset
38+
var limit = this.config.limit
39+
var offset = this.config.offset
4040
if (config) {
4141
if (config.hasOwnProperty('offset')) {
4242
offset = config.offset
@@ -45,32 +45,32 @@ export class Pokedex {
4545
limit = config.limit
4646
}
4747
}
48-
return loadResource(this.values, `${this.values.versionPath}${rootEndpoint[1]}?limit=${limit}&offset=${offset}`);
48+
return loadResource(this.config, `${this.config.versionPath}${rootEndpoint[1]}?limit=${limit}&offset=${offset}`)
4949
}
50-
});
50+
})
5151

5252
localForage.config({
5353
name: 'pokeapi-js-wrapper'
54-
});
54+
})
5555

56-
if (this.values.cacheImages) {
56+
if (this.config.cacheImages) {
5757
installSW()
5858
}
5959
}
6060

6161
resource(path) {
6262
if (typeof path === 'string') {
63-
return loadResource(this.values, path)
63+
return loadResource(this.config, path)
6464
} else if (typeof path === 'object') {
65-
return Promise.all(path.map(p => loadResource(this.values, p)));
65+
return Promise.all(path.map(p => loadResource(this.config, p)))
6666
} else {
6767
return 'String or Array is required'
6868
}
6969
}
70-
};
70+
}
7171

72-
function mapResources(values, endpoint, input) {
72+
function mapResources(config, endpoint, input) {
7373
return input.map(res => {
74-
return loadResource(values, `${values.versionPath}${endpoint[1]}/${res}/`);
75-
});
74+
return loadResource(config, `${config.versionPath}${endpoint[1]}/${res}/`)
75+
})
7676
}

src/pokeapi-js-wrapper-sw.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ self.addEventListener('fetch', function (event) {
1111
return fetch(event.request).then(function (response) {
1212
if (event.request.url.match(imgRe)) {
1313
caches.open("pokeapi-js-wrapper-images-" + version).then(function (cache) {
14-
// The response is opaque, if it fails the cache.add() will reject it
14+
// The response is opaque, if it fails cache.add() will reject it
1515
cache.add(event.request.url)
1616
})
1717
}
1818
return response;
1919
}).catch(function (error) {
20-
console.log(error)
20+
console.error(error)
2121
})
2222
}))
2323
}

test/pokeapi-js-wrapper-sw.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ self.addEventListener('fetch', function (event) {
1111
return fetch(event.request).then(function (response) {
1212
if (event.request.url.match(imgRe)) {
1313
caches.open("pokeapi-js-wrapper-images-" + version).then(function (cache) {
14-
// The response is opaque, if it fails the cache.add() will reject it
14+
// The response is opaque, if it fails cache.add() will reject it
1515
cache.add(event.request.url)
1616
})
1717
}
1818
return response;
1919
}).catch(function (error) {
20-
console.log(error)
20+
console.error(error)
2121
})
2222
}))
2323
}

test/test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ describe("pokedex", function () {
1919
id = 2,
2020
path = '/api/v2/pokemon/34',
2121
url = 'https://pokeapi.co/api/v2/pokemon/35',
22-
secureP = new Pokedex.Pokedex(),
22+
secureP = new Pokedex.Pokedex({cacheImages: true}),
2323
P = new Pokedex.Pokedex({
24-
protocol: 'http',
24+
protocol: 'http',
2525
offset: 10,
2626
limit: 1,
2727
timeout: 10000,
28-
cache: false,
29-
cacheImages: false
28+
cache: false
3029
}),
3130
interval = {
3231
limit: 10,

0 commit comments

Comments
 (0)