-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.js
73 lines (66 loc) · 2.07 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const {logger} = require('../config');
const chalk = require('chalk');
const rp = require('request-promise');
const _pokemonIdRegEx = /^http:\/\/pokeapi\.co\/api\/v2\/pokemon\/([0-9]*)\/$/;
const _constructOptions = (endpoint, qs) => {
let options = {
uri: endpoint,
json: true,
method: 'get',
timeout: 25000
};
if (!!qs) {
options.qs = qs;
}
return options;
};
const _execute = async (rp, options) => {
let time = new Date().getTime();
try {
return await rp(options);
} finally {
time = new Date().getTime() - time;
logger.info(`(${chalk.green('API')}) ${chalk.blue(options.method.toUpperCase())} ${chalk.yellow(options.uri)} ${chalk.blue(`${time} ms.`)}`);
}
};
/**
* Pokemon API Client
*/
class PokeApi {
/**
* @param {String} baseEndpoint base endpoint of the pokemon API
*/
constructor(baseEndpoint) {
this.baseEndpoint = baseEndpoint;
}
/**
* Collect all available pokemon from the given offset.
* @param {int} offset the paging offset
* @return {Promise} Pokemon API List Response
*/
async list(offset) {
const result = await _execute(rp, _constructOptions(`${this.baseEndpoint}pokemon`, {offset: offset || 0}));
return result.results.map((p) => {
return {
id: _pokemonIdRegEx.test(p.url) ? _pokemonIdRegEx.exec(p.url)[1] : null,
name: p.name
};
});
}
/**
* Retrieve a specific Pokemon and all of its details
* @param {int} id The ID of the pokemon to be retrieved
* @return {Promise} Pokemon API Get Response
*/
async get(id) {
const result = await _execute(rp, _constructOptions(`${this.baseEndpoint}pokemon/${id}`));
return {
name: result.name,
weight: result.weight,
base_experience: result.base_experience,
moves: result.moves.map((m) => m.move.name),
abilities: result.abilities.map((a) => a.ability.name)
};
}
}
module.exports = {PokeApi};