tercul-frontend/server/routes/bookmark.ts
google-labs-jules[bot] ea15477b86 feat: Fix TypeScript errors and improve type safety
This commit addresses 275 TypeScript compilation errors and improves type safety, code quality, and maintainability across the frontend codebase.

The following issues have been resolved:
- Standardized `translationId` to `number`
- Fixed missing properties on annotation types
- Resolved `tags` type mismatch
- Corrected `country` type mismatch
- Addressed date vs. string mismatches
- Fixed variable hoisting issues
- Improved server-side type safety
- Added missing null/undefined checks
- Fixed arithmetic operations on non-numbers
- Resolved `RefObject` type issues

Note: I was unable to verify the frontend changes due to local setup issues with the development server. The server would not start, and I was unable to run the Playwright tests.
2025-11-27 17:48:31 +00:00

89 lines
2.4 KiB
TypeScript

import { Router } from "express";
import type { Request } from "express";
import { graphqlClient } from "../lib/graphqlClient";
import { respondWithError } from "../lib/error";
import {
GetBookmarkDocument,
BookmarksDocument,
CreateBookmarkDocument,
DeleteBookmarkDocument,
type GetBookmarkQuery,
type BookmarksQuery,
type CreateBookmarkMutation,
type DeleteBookmarkMutation,
} from "../../shared/generated/graphql";
interface GqlRequest extends Request {
gql?: typeof graphqlClient;
}
const router = Router();
// GET /api/bookmarks/:id
router.get("/:id", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { bookmark } = await client.request<GetBookmarkQuery>(
GetBookmarkDocument,
{
id: req.params.id,
}
);
if (!bookmark)
return res.status(404).json({ message: "Bookmark not found" });
res.json(bookmark);
} catch (error) {
respondWithError(res, error, "Failed to fetch bookmark");
}
});
// GET /api/bookmarks
router.get("/", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const data = await client.request<BookmarksQuery>(BookmarksDocument, {
userId: req.query.userId as string | undefined,
workId: req.query.workId as string | undefined,
limit: req.query.limit ? Number(req.query.limit) : undefined,
offset: req.query.offset ? Number(req.query.offset) : undefined,
});
res.json(data.bookmarks);
} catch (error) {
respondWithError(res, error, "Failed to fetch bookmarks");
}
});
// POST /api/bookmarks
router.post("/", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const data = await client.request<CreateBookmarkMutation>(
CreateBookmarkDocument,
{
input: req.body,
}
);
res.status(201).json(data.createBookmark);
} catch (error) {
respondWithError(res, error, "Failed to create bookmark");
}
});
// DELETE /api/bookmarks/:id
router.delete("/:id", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const data = await client.request<DeleteBookmarkMutation>(
DeleteBookmarkDocument,
{
id: req.params.id,
}
);
res.json({ success: data.deleteBookmark });
} catch (error) {
respondWithError(res, error, "Failed to delete bookmark");
}
});
export default router;