-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathswapi-node.js
More file actions
109 lines (94 loc) · 2.91 KB
/
Copy pathswapi-node.js
File metadata and controls
109 lines (94 loc) · 2.91 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
const fetch = require('node-fetch');
const util = require('./util');
const PROTOCOL = 'https';
const HOST = 'swapi.dev';
const BASE_URL = `${PROTOCOL}://${HOST}/api/`;
const BASE_URL_REGEX = new RegExp(`http[s]?://${HOST}/api`);
function sendRequest (options) {
let {url, ...otherOptions} = options;
return fetch(url, otherOptions)
.then(response => response.json())
.then(jsonBody => {
['nextPage', 'previousPage'].forEach(value => {
jsonBody[value] = (() => {
return () => {
const url = jsonBody[value.includes('next') ? 'next' : 'previous'];
if (url) {
return makeRequest(url);
}
return Promise.resolve(null);
};
})();
});
Object.keys(jsonBody).forEach(value => {
if (typeof jsonBody[value] !== 'function') {
jsonBody['get' + util.capitaliseFirstLetter(value)] = (() => {
return () => {
if (!Array.isArray(jsonBody[value])) {
if (!!jsonBody[value].match(BASE_URL_REGEX)) {
return makeRequest(jsonBody[value]);
}
return Promise.resolve(jsonBody[value]);
}
const p = jsonBody[value].map(value_ => {
if (!!value_.match(BASE_URL_REGEX)) {
return makeRequest(value_);
}
return Promise.resolve(value_);
});
return Promise.all(p);
};
})();
}
});
return jsonBody;
});
}
function makeRequest (url) {
const options = {
url: (!!url.match(BASE_URL_REGEX)) ? url : `${BASE_URL}${url}`,
headers: {
'User-Agent': 'swapi-node',
'SWAPI-Node-Version': require('../package.json').version
}
};
return sendRequest(options);
}
function parseOptions (options = {}) {
if (typeof options === 'object') {
return options;
}
return {
id: options
};
}
module.exports = {
get (url) {
return makeRequest(url);
},
getPerson (options) {
const options_ = parseOptions(options);
return makeRequest('people' + (options_.id ? '/' + options_.id : '/'));
},
getStarship (options) {
const options_ = parseOptions(options);
return makeRequest('starships' + (options_.id ? '/' + options_.id : '/'));
},
getVehicle (options) {
const options_ = parseOptions(options);
return makeRequest('vehicles' + (options_.id ? '/' + options_.id : '/'));
},
getFilm (options) {
const options_ = parseOptions(options);
return makeRequest('films' + (options_.id ? '/' + options_.id : '/'));
},
getSpecies (options) {
const options_ = parseOptions(options);
return makeRequest('species' + (options_.id ? '/' + options_.id : '/'));
},
getPlanets (options) {
const options_ = parseOptions(options);
return makeRequest('planets' + (options_.id ? '/' + options_.id : '/'));
}
};