-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (146 loc) · 4.66 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
async function getAPIs() {
const url = "https://api.publicapis.org/entries";
const apiRequest = await fetch(url);
const data = await apiRequest.json();
const {
entries
} = data;
entries.sort((api1, api2) => {
var apiName1 = api1.API.toUpperCase();
var apiName2 = api2.API.toUpperCase();
// order remains unchanged.
if (apiName1 < apiName2) {
return -1;
}
// sorts api2 before api1. changed.
if (apiName1 > apiName2) {
return 1;
}
// names must be equal, remain unchanged.
return 0;
});
// returning a sorted array of objects.
return entries;
}
// creates component UI for each API to be displayed.
function getAPIhtml(myAPI, idNum) {
let {
API,
Description,
Auth,
HTTPS,
Cors,
Link,
Category
} = myAPI;
let httpsElem = `<span class="https">${HTTPS}</span>;`;
let authElem = `<span>${Auth}</span>`;
let corsElem = `<span class="cors">${Cors}</span>`;
if (!Description) {
Description = `No description provided.
Click API name for more information.`;
}
// adds emojies to the UI to help the user quickly see whether or not,
// a given feature is supported. For example CORS (thumb up) means supported.
if (!Auth) {
Auth = "👎🏿 N/A";
authElem = `<span class="auth notavail">${Auth}</span>`;
} else {
authElem = `<span class="auth available">👍🏾 ${Auth}</span>`;
}
if (!HTTPS) {
HTTPS = "👎🏿 N/A";
httpsElem = `<span class="https notavail">${HTTPS}</span>`;
} else {
HTTPS = "Available";
httpsElem = `<span class="https available">👍🏾 ${HTTPS}</span>`;
}
if (!Cors || Cors === "unknown" || Cors === "no") {
Cors = "👎🏿 N/A";
corsElem = `<span class="cors notavail">${Cors}</span>`;
} else {
corsElem = `<span class="cors available">👍🏿 ${Cors}</span>`;
}
if (!Category) {
Category = "";
}
const component = `
<div class="api">
<h4 class="name">
<a class="link" href=${Link} target="_blank">${API}
<br><span class="category">${Category}</span></a></h4>
<p class="description">${Description}</p>
<p class="auth">Auth: ${authElem}</p>
<p class="https">HTTPS: ${httpsElem}</p>
<p class="cors">CORS: ${corsElem}</p>
<p class="counter"><span>${idNum}</span></p>
</div>`;
return component;
}
function appHeader() {
const header = `
<header class="header container">
<a href="/explorapi/" class="brand">explorapi</a>
<nav class="nav">
<ul class="container">
<li class="nav-links"><a href="/explorapi/">home</a></li>
<li class="nav-links"><a href="https://github.com/iamserda/publicapis" target="_blank">onGH</a></li>
<li class="nav-links"><a href="https://api.publicapis.org" target="_blank">PublicAPIs</a></li>
<li class="nav-links"><a href="https://iamserda.github.io" target="_blank">iamserda</a></li>
</ul>
</nav>
</header>
`;
return header;
}
function appFooter() {
// using HTML inside of JS is awful. I know.
// Creating DOM elements in JS can become unrully, so I do this.
const footer = `
<footer class="footer">
<p>Visit the project's repo
<a href="https://github.com/davemachado/public-api">here</a>.
Many thanks to <a href="https://github.com/davemachado"
target="_blank">Dave Machado</a> and the contributers at:
<a href="https://api.publicapis.org"
target="_blank">publicapis.org</a></p>
<p> Made with ❤️ in Inwood, NYC. 🇭🇹</p>
</footer>
`;
return footer;
}
function newSearchBar() {
const search = document.createElement("input");
search.id = "searchBar";
search.className.add("search");
return search;
}
// displays all object within the myAPIs array.
// this could be decoupled further.
function displayAPIs(entries) {
let counter = 0;
const root = document.getElementById("root");
const app = document.createElement("div");
let components = "";
app.id = "app";
app.classList.add("app-grid");
//adding a header to app
root.innerHTML += appHeader();
root.appendChild(app);
document
.getElementsByTagName("header")
.item(0)
.insertAdjacentElement("afterend", app);
entries.forEach((item) => {
// to limit strain on the browser and DOM rendering
// I have accumlated the components here.
// Below, I will make a single DOM insertion.
// console.log(item, ++counter);
components += getAPIhtml(item, ++counter);
});
app.innerHTML += components;
root.innerHTML += appFooter();
}
getAPIs()
.then((data) => displayAPIs(data))
.catch((e) => console.log(`Error: ${e}`));