-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
296 lines (273 loc) · 8.42 KB
/
Copy pathtest.html
File metadata and controls
296 lines (273 loc) · 8.42 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<!doctype html>
<html lang="en" data-theme="dark">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Real Data API - Test Page</title>
<!-- DAISYUI -->
<link
href="https://cdn.jsdelivr.net/npm/daisyui@5"
rel="stylesheet"
type="text/css"
/>
<!-- TAILWIND -->
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<style>
.endpoint-section {
scroll-margin-top: 80px;
}
img.item-img {
object-fit: cover;
}
</style>
</head>
<body class="bg-base-200 min-h-screen">
<!-- Navbar -->
<div class="navbar bg-base-100 shadow-sm sticky top-0 z-50">
<div class="navbar-start">
<h1 class="text-xl font-bold px-4">
<span class="text-primary">Real</span> Data API
</h1>
</div>
<div class="navbar-center hidden lg:flex">
<p class="text-sm opacity-60">Test Page - All Endpoints</p>
</div>
<div class="navbar-end">
<span id="statusBadge" class="badge badge-outline badge-sm mr-4"
>Loading...</span
>
</div>
</div>
<div class="max-w-4xl mx-auto p-4 pb-20">
<!-- Endpoint Navigation -->
<div class="flex flex-wrap gap-2 my-6" id="navTabs"></div>
<!-- Loading -->
<div id="loading" class="flex justify-center py-20">
<span class="loading loading-spinner loading-lg text-primary"></span>
</div>
<!-- Content -->
<div id="content" class="space-y-8"></div>
</div>
<script>
const API = "http://localhost:4000";
const META = {
movies: {
label: "title",
sub: "director",
detail: "year",
badge: "genre",
},
books: {
label: "title",
sub: "author",
detail: "year",
badge: "genre",
},
artists: {
label: "name",
sub: "nationality",
detail: "era",
badge: "knownFor",
},
musicians: {
label: "name",
sub: "genre",
detail: "country",
badge: "activeYears",
},
countries: {
label: "name",
sub: "capital",
detail: "continent",
badge: "population",
},
sports: {
label: "name",
sub: "sport",
detail: "nationality",
badge: "team",
},
games: {
label: "title",
sub: "developer",
detail: "year",
badge: "genre",
},
recipes: {
label: "name",
sub: "cuisine",
detail: "difficulty",
badge: "prepTime",
},
planets: {
label: "name",
sub: "type",
detail: "diameter",
badge: "distanceFromSun",
},
programming: {
label: "name",
sub: "creator",
detail: "year",
badge: "paradigm",
},
pokemons: { label: "name", sub: "type", detail: "hp", badge: "attack" },
superheroes: {
label: "name",
sub: "realName",
detail: "publisher",
badge: "power",
},
cars: {
label: ["brand", "model"],
sub: "year",
detail: "hp",
badge: "engine",
},
tvShows: {
label: "title",
sub: "creator",
detail: "years",
badge: "genre",
},
anime: {
label: "title",
sub: "studio",
detail: "year",
badge: "genre",
},
footballTeams: {
label: "name",
sub: "league",
detail: "country",
badge: "founded",
},
instruments: {
label: "name",
sub: "family",
detail: "origin",
badge: "year",
},
dinosaurs: {
label: "name",
sub: "period",
detail: "diet",
badge: "length",
},
mythologies: {
label: "name",
sub: "mythology",
detail: "role",
badge: "domain",
},
inventions: {
label: "name",
sub: "inventor",
detail: "year",
badge: "country",
},
coffees: {
label: "name",
sub: "origin",
detail: "type",
badge: "caffeine",
},
users: {
label: ["firstName", "lastName"],
sub: "email",
detail: null,
badge: null,
},
};
function getLabel(item, field) {
if (Array.isArray(field))
return field.map((f) => item[f] || "").join(" ");
return item[field] || "";
}
function formatValue(val) {
if (val == null) return "";
if (typeof val === "number" && val > 100000)
return val.toLocaleString();
if (Array.isArray(val)) return val.join(", ");
return String(val);
}
async function loadAll() {
const endpoints = Object.keys(META);
const navTabs = document.getElementById("navTabs");
const content = document.getElementById("content");
const loading = document.getElementById("loading");
const statusBadge = document.getElementById("statusBadge");
// Create nav tabs
endpoints.forEach((ep) => {
const btn = document.createElement("a");
btn.href = `#${ep}`;
btn.className =
"badge badge-outline badge-primary cursor-pointer hover:badge-primary hover:text-primary-content transition-all";
btn.textContent = `/${ep}`;
navTabs.appendChild(btn);
});
let loaded = 0;
let failed = 0;
const results = await Promise.allSettled(
endpoints.map((ep) =>
fetch(`${API}/${ep}`)
.then((r) => r.json())
.then((data) => ({ ep, data })),
),
);
loading.style.display = "none";
for (const result of results) {
if (result.status === "rejected") {
failed++;
continue;
}
const { ep, data } = result.value;
const meta = META[ep];
loaded++;
const section = document.createElement("div");
section.id = ep;
section.className = "endpoint-section";
const listItems = data
.map((item, i) => {
const label = getLabel(item, meta.label);
const sub = meta.sub ? formatValue(item[meta.sub]) : "";
const detail = meta.detail ? formatValue(item[meta.detail]) : "";
const badge = meta.badge ? formatValue(item[meta.badge]) : "";
const hasImage = !!item.image;
const imgHtml = hasImage
? `<div><img class="item-img size-10 rounded-box" src="${item.image}" alt="${label}" onerror="this.style.display='none';this.parentElement.innerHTML='<div class=\\'size-10 rounded-box bg-base-300 flex items-center justify-center text-xs opacity-40\\'>?</div>'"/></div>`
: `<div class="size-10 rounded-box bg-base-300 flex items-center justify-center text-lg font-bold opacity-30">${label.charAt(0)}</div>`;
const badgeHtml = badge
? `<span class="badge badge-sm badge-ghost">${badge}</span>`
: "";
return `
<li class="list-row">
${imgHtml}
<div class="list-col-grow">
<div class="font-medium">${label}</div>
<div class="text-xs opacity-60">${sub}${detail ? " · " + detail : ""}</div>
</div>
${badgeHtml}
</li>`;
})
.join("");
section.innerHTML = `
<ul class="list bg-base-100 rounded-box shadow-md">
<li class="p-4 pb-2 text-xs opacity-60 tracking-wide flex justify-between items-center">
<span>/${ep}</span>
<span class="badge badge-xs badge-primary">${data.length} items</span>
</li>
${listItems}
</ul>`;
content.appendChild(section);
}
statusBadge.textContent =
`${loaded} OK` + (failed ? ` / ${failed} failed` : "");
statusBadge.className = failed
? "badge badge-warning badge-sm mr-4"
: "badge badge-success badge-sm mr-4";
}
loadAll();
</script>
</body>
</html>