-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchFromAPI.js
More file actions
108 lines (91 loc) · 2.71 KB
/
fetchFromAPI.js
File metadata and controls
108 lines (91 loc) · 2.71 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
const fetch = require("axios");
async function getLaunchData(url, params) {
let body = await fetch(
decodeURIComponent(
url ||
`https://lldev.thespacedevs.com/2.2.0/launch/?mode=list&limit=3&${params}&format=json`
)
);
let json = await body.data;
let rocketData = [];
if (json.results.length === 0) return false;
json.results.forEach((result) => {
let date = new Date(result.net);
let formattedDate =
[
new String(date.getUTCDate()).padStart(2, "0"),
new String(date.getUTCMonth() + 1).padStart(2, "0"),
date.getUTCFullYear(),
].join("/") +
" " +
[
new String(date.getUTCHours()).padStart(2, "0"),
new String(date.getUTCMinutes()).padStart(2, "0"),
new String(date.getUTCSeconds()).padStart(2, "0"),
].join(":");
rocketData.push({
count: json.count,
currentUrl: encodeURIComponent(
url ||
`https://lldev.thespacedevs.com/2.2.0/launch/?mode=list&limit=3&${params}&format=json`
),
previous: encodeURIComponent(json.previous),
next: encodeURIComponent(json.next),
name: result.name,
date: formattedDate,
lsp: result.lsp_name,
location: result.location,
image: result.image || "/assets/imageNotFound.png",
status: result.status.name,
statusDescription: result.status.description,
});
});
return rocketData;
}
async function getEventData(url, params) {
let body = await fetch(
decodeURIComponent(
url ||
`https://lldev.thespacedevs.com/2.2.0/event/?mode=list&limit=3&${params}&format=json`
)
);
let json = await body.data;
let eventData = [];
if (json.results.length === 0) return false;
json.results.forEach((result) => {
let date = new Date(result.date);
let formattedDate =
[
new String(date.getUTCDate()).padStart(2, "0"),
new String(date.getUTCMonth() + 1).padStart(2, "0"),
date.getUTCFullYear(),
].join("/") +
" " +
[
new String(date.getUTCHours()).padStart(2, "0"),
new String(date.getUTCMinutes()).padStart(2, "0"),
new String(date.getUTCSeconds()).padStart(2, "0"),
].join(":");
eventData.push({
count: json.count,
currentUrl: encodeURIComponent(
url ||
`https://lldev.thespacedevs.com/2.2.0/event/?mode=list&limit=3&${params}&format=json`
),
previous: encodeURIComponent(json.previous),
next: encodeURIComponent(json.next),
name: result.name,
type: result.type.name,
date: formattedDate,
location: result.location,
description: result.description,
launches: result.launches.map((launch) => launch.name),
newsUrl: result.news_url,
videoUrl: result.video_url,
live: result.webcast_live,
image: result.feature_image || "/assets/imageNotFound.png",
});
});
return eventData;
}
module.exports = { getLaunchData, getEventData };