api/src/api.ts

84 lines · 2839 bytes

 1import { Hono } from 'hono';
 2import { cors } from 'hono/cors';
 3import Fuse from 'fuse.js';
 4
 5import {
 6  fetchAllServices,
 7  fetchAwesomePrivacyData,
 8  findBySlug,
 9  slugify,
10  renderRemoteIndex,
11} from './utils';
12
13import index from '../public/index.html'
14
15/* Create new Hono app */
16const app = new Hono({ strict: false });
17
18/* Enable CORS for all routes */
19app.use('*', cors());
20
21/* Serve the index page (which contains the Swagger Docs) */
22app.get("/", async (ctx) => {
23  if (index) {
24    return ctx.html(index)
25  } else {
26    return renderRemoteIndex(ctx);
27  }
28});
29
30/* Returns an array all service objects */
31app.get('/services', async (c) => {
32  return c.json(await fetchAllServices());
33});
34
35/* Returns an array of all categories IDs */
36app.get('/categories', async (c) => {
37  const { categories } = await fetchAwesomePrivacyData();
38  return c.json(categories.map(({ name }) => slugify(name)));
39});
40
41app.get('/search', async (c) => {
42  return c.json({ error: 'Search term not specified' }, 404)
43});
44
45/* Search results, returns an array of listing which match a given search term  */
46app.get('/search/:searchTerm', async (c) => {
47  const services = await fetchAllServices();
48  const options = {
49    includeScore: true,
50    keys: ['name', 'description', 'followWith']
51  };
52  const fuse = new Fuse(services, options);
53  const searchTerm = c.req.param('searchTerm');
54  const result = fuse.search(searchTerm);
55  const mappedResults = result.map(({ item, score }) => ({ ...item, score }));
56  return c.json(mappedResults);
57});
58
59/* Returns an array of all section IDs within a given category */
60app.get('/:category', async (c) => {
61  const { categories } = await fetchAwesomePrivacyData();
62  const category = findBySlug(categories, c.req.param('category'));
63  return category ? c.json(category.sections.map(({ name }) => slugify(name)))
64    : c.json({ error: 'Category not found' }, 404);
65});
66
67/* Returns an array of all service objects within a given section */
68app.get('/:category/:section', async (c) => {
69  const { categories } = await fetchAwesomePrivacyData();
70  const category = findBySlug(categories, c.req.param('category'));
71  const section = findBySlug(category?.sections || [], c.req.param('section'));
72  return section ? c.json(section.services || []) : c.json({ error: 'Section not found' }, 404);
73});
74
75/* Returns a single service object, for a given category, section and service */
76app.get('/:category/:section/:service', async (c) => {
77  const { categories } = await fetchAwesomePrivacyData();
78  const category = findBySlug(categories, c.req.param('category'));
79  const section = findBySlug(category?.sections || [], c.req.param('section'));
80  const service = findBySlug(section?.services || [], c.req.param('service'));
81  return service ? c.json(service) : c.json({ error: 'Service not found' }, 404);
82});
83
84export default app;