-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtypes.js
47 lines (44 loc) · 1.83 KB
/
types.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
const express = require('express');
const router = express.Router();
const fetchUrl = require("fetch").fetchUrl;
const pokedex = "https://raw.githubusercontent.com/Purukitto/pokemon-data.json/master/types.json"
router.get('/:typeName', (req, res) => {
const typeName = req.params.typeName.toLowerCase().replace(/\s/g, '');
fetchUrl(pokedex, function (error, meta, body) {
if (!error) {
var data = new String();
data = JSON.parse(body);
if (typeName == "all") {
return res.status(200).json(data);
} else if (typeName == "random") {
var randID = Math.floor(Math.random() * (data.length));
return res.status(200).json(data[randID]);
} else {
if (parseInt(typeName)) {
if (typeName <= data.length) {
//TODO: check this logic for other endpoints
return res.status(200).json(data[typeName - 1]);
}
// TODO: Add status code for not found
return res.status(200).json({
message: "Please check the name/ID again"
});
} else {
var i;
for (i = 0; i < data.length; i++) {
if (data[i].english)
if (data[i].english.toLowerCase().replace(/\s/g, '') == typeName) {
return res.status(200).json(data[i]);
}
}
return res.status(200).json({
message: "Please check the name again"
});
}
}
} else {
return res.status(500).json(error);
}
});
});
module.exports = router;