Skip to content

Commit 3114cac

Browse files
authored
Merge pull request #16 from PokeAPI/beta
2 parents 0381e74 + c55592b commit 3114cac

18 files changed

+3102
-2463
lines changed

README.md

Lines changed: 236 additions & 258 deletions
Large diffs are not rendered by default.

dist/index.js

Lines changed: 3 additions & 9 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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const imgRe = /https:\/\/raw\.githubusercontent\.com\/PokeAPI\/sprites\/[\/-\w\d]+\/[\d\w-]+\.(?:png|svg|gif)/
2+
const version = 1
3+
4+
self.addEventListener('fetch', function (event) {
5+
if (event.request.url.match(imgRe)) {
6+
event.respondWith(caches.match(event.request).then(function (response) {
7+
if (response) {
8+
return response
9+
}
10+
11+
return fetch(event.request).then(function (response) {
12+
if (event.request.url.match(imgRe)) {
13+
caches.open("pokeapi-js-wrapper-images-" + version).then(function (cache) {
14+
// The response is opaque, if it fails cache.add() will reject it
15+
cache.add(event.request.url)
16+
})
17+
}
18+
return response;
19+
}).catch(function (error) {
20+
console.error(error)
21+
})
22+
}))
23+
}
24+
})
25+
26+
self.addEventListener('install', function(event) {
27+
self.skipWaiting()
28+
})

package-lock.json

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

package.json

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
{
22
"name": "pokeapi-js-wrapper",
3-
"version": "1.1.2",
4-
"description": "An API wrapper for PokeAPI - browser use only",
3+
"version": "1.2.0-beta.0",
4+
"description": "An API wrapper for PokeAPI",
55
"main": "dist/index.js",
66
"module": "src/index.js",
7+
"files": [
8+
"dist/index.js",
9+
"dist/pokeapi-js-wrapper-sw.js",
10+
"src/*"
11+
],
712
"scripts": {
813
"analyze": "webpack --json | webpack-bundle-size-analyzer",
914
"build": "webpack -p",
1015
"build:watch": "webpack --watch",
1116
"doctoc": "doctoc .",
1217
"pretest": "npm run build",
1318
"test": "nyc mocha -r mock-local-storage -r source-map-support/register ./test/test.js",
14-
"prepublish": "npm run build"
19+
"prepublish": "npm run build",
20+
"serve": "http-server ."
1521
},
1622
"repository": {
1723
"type": "git",
@@ -34,8 +40,8 @@
3440
},
3541
"homepage": "https://github.com/PokeAPI/pokeapi-js-wrapper#readme",
3642
"dependencies": {
37-
"axios": "^0.19.0",
38-
"localforage": "^1.7.3"
43+
"axios": "^0.21.0",
44+
"localforage": "^1.9.0"
3945
},
4046
"devDependencies": {
4147
"babel-core": "^6.26.3",
@@ -45,12 +51,14 @@
4551
"chai": "^4.2.0",
4652
"chai-as-promised": "^7.1.1",
4753
"chai-things": "^0.2.0",
54+
"copy-webpack-plugin": "^4.6.0",
55+
"http-server": "^0.12.3",
4856
"json-loader": "^0.5.7",
4957
"mocha": "^4.1.0",
50-
"mock-local-storage": "^1.1.8",
51-
"nyc": "^14.1.1",
52-
"source-map-support": "^0.5.12",
58+
"mock-local-storage": "^1.1.15",
59+
"nyc": "^15.1.0",
60+
"source-map-support": "^0.5.19",
5361
"webpack": "^3.12.0",
5462
"webpack-bundle-size-analyzer": "^2.7.0"
5563
}
56-
}
64+
}

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/configurator.js

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

src/default.js

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

src/getter.js

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

4-
import { values } from './default.js';
4+
const CACHE_PREFIX = "pokeapi-js-wrapper-"
55

6-
const CACHE_PREFIX = "pokeapi-js-wrapper-";
7-
8-
function loadResource(url) {
6+
function loadResource(config, url) {
97
return new Promise((resolve, reject) => {
108
localForage.ready()
119
.then(() => {
1210
localForage.getItem(`${CACHE_PREFIX}${url}`)
1311
.then(value => {
1412
if (value === null) {
15-
loadUrl(url).then(res => {resolve(res)})
16-
.catch(err => {reject(err)});
13+
loadUrl(config, url).then(res => {resolve(res)})
14+
.catch(err => {reject(err)})
1715
} else {
1816
resolve(addCacheMark(value))
1917
}
2018
})
21-
.catch(error => {
22-
loadUrl(url).then(res => {resolve(res)})
23-
.catch(err => {reject(err)});
24-
});
19+
.catch(err => {
20+
loadUrl(config, url).then(res => {resolve(res)})
21+
.catch(err => {reject(err)})
22+
})
2523
})
2624
.catch(err => {
27-
loadUrl(url).then(res => {resolve(res)})
28-
.catch(err => {reject(err)});
29-
});
30-
});
31-
};
25+
loadUrl(config, url).then(res => {resolve(res)})
26+
.catch(err => {reject(err)})
27+
})
28+
})
29+
}
3230

33-
function loadUrl(url) {
31+
function loadUrl(config, url) {
3432
return new Promise((resolve, reject) => {
3533
let options = {
36-
baseURL: `${values.protocol}://${values.hostName}/`,
37-
timeout: values.timeout
34+
baseURL: `${config.protocol}://${config.hostName}/`,
35+
timeout: config.timeout
3836
}
3937
axios.get(url, options)
4038
.then(response => {
4139
// if there was an error
4240
if (response.status >= 400) {
43-
reject(response);
41+
reject(response)
4442
} else {
4543
// if everything was good
4644
// cache the object in browser memory
4745
// only if cache is true
48-
if (values.cache) {
49-
localForage.setItem(`${CACHE_PREFIX}${url}`, response.data);
46+
if (config.cache) {
47+
localForage.setItem(`${CACHE_PREFIX}${url}`, response.data)
5048
}
51-
resolve(addCacheMark(response.data, 0));
49+
resolve(response.data)
5250
}
5351
})
5452
.catch(err => { reject(err) })
55-
});
56-
}
57-
58-
function addCacheMark(object, cached = 1) {
59-
//object.fromCache = cached;
60-
return object;
53+
})
6154
}
6255

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

src/index.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
1-
import endpoints from './endpoints.json';
2-
import rootEndpoints from './rootEndpoints.json';
3-
import { loadResource } from './getter.js';
4-
import { values } from './default.js';
5-
import { configurator } from './configurator.js';
1+
import localForage from "localforage"
2+
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'
68

79
export class Pokedex {
10+
811
constructor(config) {
9-
configurator.setPokedexConfiguration(config);
10-
configurator.setRootEndpointConfiguration(config);
11-
12+
this.config = new Config(config)
13+
this.getConfig = function() {
14+
return this.config
15+
}
16+
1217
// add to Pokedex.prototype all our endpoint functions
1318
endpoints.forEach(endpoint => {
1419
this[endpoint[0]] = input => {
1520
if (input) {
1621

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

2227
// if the user has submitted an Array
2328
// return a new promise which will resolve when all loadResource calls are ended
2429
else if (typeof input === 'object') {
25-
return Promise.all(mapResources(endpoint, input));
30+
return Promise.all(mapResources(this.config, endpoint, input))
2631
}
2732
}
2833
}
29-
});
34+
})
3035

3136
rootEndpoints.forEach(rootEndpoint => {
3237
this[rootEndpoint[0]] = config => {
33-
var limit = values.limit
34-
var offset = values.offset
38+
var limit = this.config.limit
39+
var offset = this.config.offset
3540
if (config) {
3641
if (config.hasOwnProperty('offset')) {
3742
offset = config.offset
@@ -40,24 +45,32 @@ export class Pokedex {
4045
limit = config.limit
4146
}
4247
}
43-
return loadResource(`${values.versionPath}${rootEndpoint[1]}?limit=${limit}&offset=${offset}`);
48+
return loadResource(this.config, `${this.config.versionPath}${rootEndpoint[1]}?limit=${limit}&offset=${offset}`)
4449
}
45-
});
50+
})
51+
52+
localForage.config({
53+
name: 'pokeapi-js-wrapper'
54+
})
55+
56+
if (this.config.cacheImages) {
57+
installSW()
58+
}
4659
}
4760

4861
resource(path) {
4962
if (typeof path === 'string') {
50-
return loadResource(path)
63+
return loadResource(this.config, path)
5164
} else if (typeof path === 'object') {
52-
return Promise.all(path.map(p => loadResource(p)));
65+
return Promise.all(path.map(p => loadResource(this.config, p)))
5366
} else {
5467
return 'String or Array is required'
5568
}
5669
}
57-
};
70+
}
5871

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

0 commit comments

Comments
 (0)