-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository
/
Copy pathindex.tsx
More file actions
More file actions
64 lines (49 loc) · 1.64 KB
/
index.tsx
File metadata and controls
64 lines (49 loc) · 1.64 KB
You must be signed in to make or propose changes
More edit options
Edit and raw actions
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
/**
* Data - A web service that provides data in various categories
*/
import { Hono } from 'hono';
import { jsxRenderer } from 'hono/jsx-renderer';
import { appendTrailingSlash, trimTrailingSlash } from 'hono/trailing-slash';
// Import JSX components
import { Home } from './components/Home';
import { Data } from './components/Data';
// Import functions to handle data for each category
import { DATA_HANDLERS } from './routes';
// Canonical URL of the home page
const SITE_URL = new URL('https://data.wikinder.org/');
const app = new Hono();
app.notFound((c) => c.text('', 404));
app.get('*', jsxRenderer());
// Route the home page
app.get('/', (c) => {
const categories = Object.fromEntries(
Object.keys(DATA_HANDLERS).map((key) => [`${key}/`, capitalize(key)])
);
return c.render(<Home categories={categories} canonicalUrl={SITE_URL} />);
});
// Route the data pages (like "/date/1970-01-01" or "/date/")
app.get('/:categoryKey', appendTrailingSlash());
app.get('/:categoryKey/:input/', trimTrailingSlash());
app.on('GET', ['/:categoryKey/', '/:categoryKey/:input'], (c) => {
const { categoryKey, input } = c.req.param();
if (!Object.hasOwn(DATA_HANDLERS, categoryKey)) {
return c.notFound();
}
// Get the output
const output = DATA_HANDLERS[categoryKey](input);
if (output == null) {
return c.notFound();
}
output.categoryLabel ??= capitalize(categoryKey);
return c.render(
<Data
isIndex={input == null}
canonicalUrl={new URL(c.req.path, SITE_URL)}
{...output}
/>
);
});
function capitalize(str: string): string {
return str.replace(/^./, (first) => first.toUpperCase());
}
export default app;