api/src/api.ts

85 lines · 2840 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
60/* Returns an array of all section IDs within a given category */
61app.get('/:category', async (c) => {
62  const { categories } = await fetchAwesomePrivacyData();
63  const category = findBySlug(categories, c.req.param('category'));
64  return category ? c.json(category.sections.map(({ name }) => slugify(name)))
65    : c.json({ error: 'Category not found' }, 404);
66});
67
68/* Returns an array of all service objects within a given section */
69app.get('/:category/:section', async (c) => {
70  const { categories } = await fetchAwesomePrivacyData();
71  const category = findBySlug(categories, c.req.param('category'));
72  const section = findBySlug(category?.sections || [], c.req.param('section'));
73  return section ? c.json(section.services || []) : c.json({ error: 'Section not found' }, 404);
74});
75
76/* Returns a single service object, for a given category, section and service */
77app.get('/:category/:section/:service', async (c) => {
78  const { categories } = await fetchAwesomePrivacyData();
79  const category = findBySlug(categories, c.req.param('category'));
80  const section = findBySlug(category?.sections || [], c.req.param('section'));
81  const service = findBySlug(section?.services || [], c.req.param('service'));
82  return service ? c.json(service) : c.json({ error: 'Service not found' }, 404);
83});
84
85export default app;