Skip to content

Commit fcb538d

Browse files
committed
Fixes #1
1 parent 2086fff commit fcb538d

5 files changed

Lines changed: 66 additions & 13 deletions

File tree

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.

src/default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const values = {};
22

33
values.protocol = 'http';
4-
values.hostName = '://pokeapi.co';
4+
values.hostName = 'pokeapi.co';
55
values.versionPath = '/api/v2/';
66
values.offset = 0;
77
values.limit = 100000;

src/getter.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { values } from './default.js';
55

66
const CACHE_PREFIX = "pokeapi-js-wrapper-";
77

8+
var instance = axios.create({
9+
baseURL: `${values.protocol}://${values.hostName}`,
10+
timeout: values.timeout
11+
});
12+
813
function loadResource(url) {
914
return new Promise((resolve, reject) => {
1015
localForage.ready()
@@ -32,10 +37,7 @@ function loadResource(url) {
3237

3338
function loadUrl(url) {
3439
return new Promise((resolve, reject) => {
35-
const options = {
36-
timeout: values.timeout
37-
};
38-
axios.get(url, options)
40+
instance.get(url)
3941
.then(response => {
4042
// if there was an error
4143
if (response.status >= 400) {
@@ -50,7 +52,7 @@ function loadUrl(url) {
5052
resolve(addCacheMark(response.data, 0));
5153
}
5254
})
53-
.catch(err => {reject(err)})
55+
.catch(err => { reject(err) })
5456
});
5557
}
5658

src/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class Pokedex {
1515

1616
// if the user has submitted a Name or an ID, return the JSON promise
1717
if (typeof input === 'number' || typeof input === 'string') {
18-
return loadResource(`${values.protocol}${values.hostName}${values.versionPath}${endpoint[1]}/${input}/`);
18+
return loadResource(`${values.versionPath}${endpoint[1]}/${input}/`);
1919
}
2020

2121
// if the user has submitted an Array
@@ -30,14 +30,24 @@ export class Pokedex {
3030
rootEndpoints.forEach(rootEndpoint => {
3131
this[rootEndpoint[0]] = config => {
3232
configurator.setRootEndpointConfiguration(config);
33-
return loadResource(`${values.protocol}${values.hostName}${values.versionPath}${rootEndpoint[1]}?limit=${values.limit}&offset=${values.offset}`);
33+
return loadResource(`${values.versionPath}${rootEndpoint[1]}?limit=${values.limit}&offset=${values.offset}`);
3434
}
3535
});
3636
}
37+
38+
resource(path) {
39+
if (typeof path === 'string') {
40+
return loadResource(path)
41+
} else if (typeof path === 'object') {
42+
return Promise.all(path.map(p => loadResource(p)));
43+
} else {
44+
return 'String or Array is required'
45+
}
46+
}
3747
};
3848

3949
function mapResources(endpoint, input) {
4050
return input.map(res => {
41-
return loadResource(`${values.protocol}${values.hostName}${values.versionPath}${endpoint[1]}/${res}/`);
51+
return loadResource(`${values.versionPath}${endpoint[1]}/${res}/`);
4252
});
4353
}

test/test.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,52 @@ chai.use(require("chai-as-promised"));
1212
describe("pokedex", function () {
1313
var promise,
1414
id = 2,
15+
path = '/api/v2/pokemon/34',
16+
url = 'https://pokeapi.co/api/v2/pokemon/35',
1517
P = new Pokedex.Pokedex()
16-
secureP = new Pokedex.Pokedex({protocol: 'https'});
18+
secureP = new Pokedex.Pokedex({ protocol: 'https' });
1719

1820
this.timeout(21000);
1921

22+
describe(".resource(Mixed: array)", function () {
23+
before(function () {
24+
promise = P.resource(['/api/v2/pokemon/36', 'api/v2/berry/8', 'https://pokeapi.co/api/v2/ability/9/']);
25+
});
26+
it("should succeed", function () {
27+
return promise;
28+
});
29+
it("should have length 3", function() {
30+
return expect(promise).to.eventually.have.length(3);
31+
});
32+
it("should have property name", function () {
33+
return expect(promise).to.eventually.all.have.property('name');
34+
});
35+
});
36+
37+
describe(".resource(Path: string)", function () {
38+
before(function () {
39+
promise = P.resource(path);
40+
});
41+
it("should succeed", function () {
42+
return promise;
43+
});
44+
it("should have property height", function () {
45+
return expect(promise).to.eventually.have.property('height');
46+
});
47+
});
48+
49+
describe(".resource(Url: string)", function () {
50+
before(function () {
51+
promise = P.resource(url);
52+
});
53+
it("should succeed", function () {
54+
return promise;
55+
});
56+
it("should have property height", function () {
57+
return expect(promise).to.eventually.have.property('height')
58+
});
59+
});
60+
2061
describe(".getVersionByName(Id: int)", function () {
2162
before(function () {
2263
promise = P.getVersionByName(id);
@@ -25,7 +66,7 @@ describe("pokedex", function () {
2566
return promise;
2667
});
2768
it("should have property name", function () {
28-
return expect(P.getVersionByName(id)).to.eventually.have.property("name");
69+
return expect(promise).to.eventually.have.property("name");
2970
});
3071
});
3172

0 commit comments

Comments
 (0)