-
Notifications
You must be signed in to change notification settings - Fork 5
/
uptime.data.js
39 lines (34 loc) · 1.01 KB
/
uptime.data.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
// @ts-check
import { getEndpointUrls } from "./urlSource";
export default {
async load() {
const endpoints = await getEndpointUrls();
const items = endpoints.map((endpoint) => {
return {
url: endpoint.url,
title: endpoint.title,
status: "unknown",
error: null,
};
});
// check if the endpoints are online
// if the endpoint is online, set the status to "online", otherwise set it to "offline"
for (let ix = 0; ix < items.length; ix++) {
const url = items[ix].url;
try {
const response = await fetch(url, { method: "GET" });
if (response.ok) {
items[ix].status = "online";
} else {
items[ix].status = "offline";
items[ix].error = `${response.status} ${response.statusText}`;
}
} catch (error) {
items[ix].status = "offline";
items[ix].error = `${error.cause} ${error.message}`;
}
}
const moment = Date();
return { endpoints: items, moment };
},
};