aboutsummaryrefslogtreecommitdiffstats
path: root/packages/api/routes/highlights.ts
blob: d381f7e290ef5d896d1d874c9b40040db0ad6242 (plain) (blame)
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
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";

import {
  zNewHighlightSchema,
  zUpdateHighlightSchema,
} from "@karakeep/shared/types/highlights";

import { authMiddleware } from "../middlewares/auth";
import { adaptPagination, zPagination } from "../utils/pagination";

const app = new Hono()
  .use(authMiddleware)
  .get("/", zValidator("query", zPagination), async (c) => {
    const searchParams = c.req.valid("query");
    const resp = await c.var.api.highlights.getAll({
      ...searchParams,
    });
    return c.json(adaptPagination(resp));
  })
  .post("/", zValidator("json", zNewHighlightSchema), async (c) => {
    const body = c.req.valid("json");
    const resp = await c.var.api.highlights.create(body);
    return c.json(resp, 201);
  })
  .get("/:highlightId", async (c) => {
    const highlightId = c.req.param("highlightId");
    const highlight = await c.var.api.highlights.get({
      highlightId,
    });
    return c.json(highlight, 200);
  })
  .patch(
    "/:highlightId",
    zValidator("json", zUpdateHighlightSchema.omit({ highlightId: true })),
    async (c) => {
      const highlightId = c.req.param("highlightId");
      const body = c.req.valid("json");
      const highlight = await c.var.api.highlights.update({
        highlightId,
        ...body,
      });
      return c.json(highlight, 200);
    },
  )
  .delete("/:highlightId", async (c) => {
    const highlightId = c.req.param("highlightId");
    const highlight = await c.var.api.highlights.delete({
      highlightId,
    });
    return c.json(highlight, 200);
  });

export default app;