-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (80 loc) · 2.35 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
const http = require('http'); // server doesn't support https
const URLS = Object.freeze({
ASTROS: "http://api.open-notify.org/astros.json",
ISS_NOW: "http://api.open-notify.org/iss-now.json",
})
class OpenNotify {
static #get = async (url, toJson) => new Promise((resolve, reject) => {
http.get(url, async res => {
if (res.statusCode !== 200) {
reject(new Error(`Request Failed.\nStatus Code: ${statusCode}`))
}
let data = ""
res.on('data', buffer => data += buffer)
res.on('end', () => resolve(toJson(data)))
res.on('error', reject)
}).on('error', reject)
})
/** Gets the current location of ISS (International Space Station)
* @example
* ```javascript
const iss_location = await OpenNotify.getISSLocation()
console.log(
'ISS location:\n' +
`latitude: ${iss_location.latitude}\n` +
`longitude: ${iss_location.longitude}`
)
* ```
* @returns {Promise}
* ```json
* {
* message: {string},
* latitude: {number},
* longitude: {number},
* date_time: {Date},
* }
* ```
*/
static getISSLocation = async () => this.#get(URLS.ISS_NOW, data => {
data = JSON.parse(data)
return Object.freeze({
message: data['message'],
latitude: data['iss_position']['latitude'],
longitude: data['iss_position']['longitude'],
date_time: new Date(data['timestamp'] * 1000)
})
})
/** Get the current number of people in space. It also returns the names and spacecraft those people are on.
* @example
* ```javascript
const peopleInSpace = await OpenNotify.getPeopleInSpace()
console.log(`Printing ${peopleInSpace.number} people in space:`);
for (const { name, craft } of peopleInSpace.people) {
console.log(`name: ${name}, craft: ${craft}`);
}
* ```
* @returns {Promise}
* ```json
* {
* number: {number},
* message: {string},
* people: [
* {
* name: {string},
* craft: {string},
* },
* ...
* ]
* }
* ```
*/
static getPeopleInSpace = async () => this.#get(URLS.ASTROS, data => {
data = JSON.parse(data)
return Object.freeze({
message: data['message'],
number: data['number'],
people: data['people']
})
})
}
module.exports = OpenNotify